Search code examples
androidopencvcomputer-visionobject-detection

OpenCV haarCascade whit Optical flow tracking


i'm trying to track objects with Optical flow in android after using a Haar Cascade detection like in the code below and i have this error can anyone help me with this

E/cv::error(): OpenCV(3.4.12) Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray), file /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp, line 1259 E/org.opencv.video: video::calcOpticalFlowPyrLK_15() caught cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)' E/AndroidRuntime: FATAL EXCEPTION: Thread-2 Process: opencv.org, PID: 31380 CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.12) /build/3_4_pack-android/opencv/modules/video/src/lkpyramid.cpp:1259: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'virtual void cv::{anonymous}::SparsePyrLKOpticalFlowImpl::calc(cv::InputArray, cv::InputArray, cv::InputArray, cv::InputOutputArray, cv::OutputArray, cv::OutputArray)' ]

enter code hereMatOfRect cars = new MatOfRect();

    if (mDetectorType == JAVA_DETECTOR) {
        if (mJavaDetector != null) {
            mJavaDetector.detectMultiScale(matGray, cars,1.1, 2, 2,
                    // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                    new Size(mAbsoluteCarSize, mAbsoluteCarSize), new Size());

        }

    }
    else {
        Log.e(TAG, "Detection method is not selected!");
    }

    Rect[] carsArray = cars.toArray();
    ArrayList<Point> CurrentCars = new ArrayList<>();

    for (int i = 0; i < carsArray.length; i++)
    {
        Imgproc.rectangle(matRGB, carsArray[i].tl(), carsArray[i].br(),
                CAR_RECT_COLOR, 3);
        xCenter = (carsArray[i].x + carsArray[i].width + carsArray[i].x) / 2;
        yCenter = (carsArray[i].y + carsArray[i].y + carsArray[i].height) / 2;
        Point center = new Point(xCenter, yCenter);

        CurrentCars.add(center);

        Imgproc.putText(matRGB, "[" + center.x + "," + center.y + "]",
                new Point(center.x + 20, center.y + 20),
                Core.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255,
                        255));

    }

    if (previousPoints.empty()){
        previousPoints.fromList(CurrentCars);
        matGray.copyTo(matPrevGray);
    }

    currentPoints = new MatOfPoint2f();
    MatOfByte status = new MatOfByte();
    MatOfFloat err = new MatOfFloat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT + TermCriteria.EPS,10,0.03);
    Video.calcOpticalFlowPyrLK(matPrevGray,matGray,previousPoints,currentPoints,status,err);

    byte StatusArr[] = status.toArray();
    Point p0Arr[] = previousPoints.toArray();
    Point p1Arr[] = currentPoints.toArray();
    ArrayList<Point> good_new = new ArrayList<>();
    for (int i = 0; i<StatusArr.length ; i++ ) {
        if (StatusArr[i] == 1) {
            good_new.add(p1Arr[i]);
            Imgproc.line(matRGB, p1Arr[i], p0Arr[i], new Scalar(255,255,0,0),2);
            Imgproc.circle(matRGB, p1Arr[i],5, new Scalar(255,255,0,0),-1);
        }
    }

    currentPoints.copyTo(previousPoints);
    matGray.copyTo(matPrevGray);

Solution

  • matPrevGray is empty. that's what it's saying.