1
votes

I'm a complete novice when it comes to OpenCV, so this is probably a dumb question.

I'm just trying to get something basic up and running- I want to draw the edges detected by the Canny algorithm directly on the image coming in. I currently have this:

Canny Screenshot

I'm displaying the edge data from Canny directly, but now I want to get rid of the black and just show the white, on the image being processed.

I've tried googling things like "using binary image as alpha mask", but after a day of reading tutorials and trying everything I can find, I'm still not sure I know what's going on. OpenCV seems very powerful, so this is probably a pretty easy thing to do, so I'm hoping somebody can point me in the right direction.

Here's the code I'm using, most of which has been copied from the examples:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    Mat rgba = inputFrame.rgba();
    org.opencv.core.Size sizeRgba = rgba.size();

    Mat rgbaInnerWindow;


    int rows = (int) sizeRgba.height;
    int cols = (int) sizeRgba.width;

    int left = cols / 8;
    int top = rows / 8;

    int width = cols * 3 / 4;
    int height = rows * 3 / 4;

    //get sub-image
    rgbaInnerWindow = rgba.submat(top, top + height, left, left + width);

    //create edgesMat from sub-image
    Imgproc.Canny(rgbaInnerWindow, edgesMat, 100, 100);

    //copy the edgesMat back into the sub-image
   Imgproc.cvtColor(edgesMat, rgbaInnerWindow, Imgproc.COLOR_GRAY2BGRA, 4);

    rgbaInnerWindow.release();

    return rgba;
}

Edit: I've also posted this question on the OpenCV forums here.

1

1 Answers

1
votes

I've not used Java in more than a decade, and have not used Java with OpenCV at all, but I'm going to try to lay out how I would do this. I'm doing my best to write it in this language, but if I'm not quite getting it right I expect you'll be able to make those minor changes to get it working.

As I see it your order of operations after running Canny should be thus:

  1. Copy your edgesMat and convert that to BGRA. ( call it colorEdges)
  2. Replace the white in colorEdges with the color of your choice (the color you want drawn on the video feed) using edgesMat as a mask.
  3. Then drop colorEdges back on rgbaInnerWindow using edgesMat as a mask.

The code:

//step 1
Mat colorEdges;
edgesMat.copyTo(colorEdges);
Imgproc.cvtColor(colorEdges, colorEdges, COLOR_GRAY2BGRA);
//step 2
newColor = new Scalar(0,255,0);    //this will be green
colorEdges.setTo(newColor, edgesMat);
//step 3
colorEdges.copyTo(rgbaInnerWindow, edgesMat);    //this replaces your current cvtColor line, placing your Canny edge lines on the original image

That oughta do it. :)

copyTo (Mat m)
cvtColor (Mat src, Mat dst, int code)
setTo (Mat value, Mat mask)
copyTo (Mat m, Mat mask)