Search code examples
androidopencvjava-threads

OpenCV(Android) - remove contours drawn


The apps detect the specific color on the frame and draw the contours using OpenCV. And when the capture button is clicked, the frame image will be used to do some image processing, while the contours drawn is also been captured with the frame which is not what I want it to. My question is how can I remove the contours drawn when I click the capture button. Or is there any method to get the frame without contours drawn?

The method I have tried:

  1. Lock the onCapture() until onCameraFrame is called and return mRbg before drawContour() is called.
  2. Clone mRgba to new Mat and use the new Mat as parameter of subColor

But both of them didn't work.

I'm thinking to pause onCapture() until onCameraFrame called and done several times skipping the lines of drawing contours to make sure nothing has drawn on the frame. But I don't know how to handle two synchronized().

public boolean onTouch(View v, MotionEvent event) {
    lock = true;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        //do something
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        //do something
        //↓to make sure onCameraFrame is pause before the finger left the screen
        lock = false;
        synchronized (locker) { locker.notify(); }
    }

    return true;
} 

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    //Pause until onTouch() is done
    commandLocker();

    //Detect the contour
    mDetector.setHsvColor(txtHsv);
    if (area) {
        nRgba = mRgba.submat(ey, sy, sx, ex);
        mDetector.process(nRgba);
    } else {
        mDetector.process(mRgba);
    }

    //Skip this when onCapture is called
    //Draw the contour on the frame
    if (!capture) {
        List<MatOfPoint> contours = mDetector.getContours();
        if (nRgba != null && area) {
            Imgproc.rectangle(mRgba, new Point(sx, sy), new Point(ex, ey), areaColor, 3);
            Imgproc.drawContours(nRgba, contours, -1, contourColor);
        } else
            Imgproc.drawContours(mRgba, contours, -1, contourColor);
    }

    return mRgba;
}

public void onCapture(View view) throws IOException {
    capture = true;
    //Pause until onCameraFrame() done
    if (!area)
        subColor(mRgba);
    else
        subColor(nRgba);
}

public void subColor (Mat src) throws IOException {
    //do something
}

private void commandLocker() {
    synchronized (locker) {
        while (lock) {
            try {
                locker.wait();
            } catch (InterruptedException e) {
                Log.e("Exception", "InterruptedException");
            }
        }
    }
}

Frame captured without processing Processed image


Solution

  • In your onCameraFrame(CvCameraViewFrame inputFrame) function, if the condition:

    if (nRgba != null)

    is not satisfied, you overwrite your original mat mRgba by doing Imgproc.drawContours(mRgba, contours, -1, contourColor);

    Is that your problem?