0
votes

I am working on a program that extracts the stickers on the puzzle and then later on finds the RGB of them. Currently, I am at the point where I want to remove any contours that aren't "square" like. I was wondering how I could do this.

What I do is I load the image, gray it, blur it, canny edge detection, dilate it find contours and draw them.

Is there a way I can draw around the contours instead of filling them in? And remove contours that aren't roughly the same size around or have almost 90 degree angles?

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat capturedFrame = Imgcodecs.imread("first.png");

    //Gray
    Mat gray = new Mat();
    Imgproc.cvtColor(capturedFrame, gray, Imgproc.COLOR_BGR2GRAY);

    //Blur
    Mat blur = new Mat();
    Imgproc.blur(gray, blur, new Size(3,3));
    //Canny image
    Mat canny = new Mat();
    Imgproc.Canny(blur, canny, 20, 40, 3, true);

    Imgcodecs.imwrite("test.png", canny);

    //System.exit(0);
    Mat kernel = Imgproc.getStructuringElement(1, new Size(3,3));
    Mat dilated = new Mat();
    Imgproc.dilate(canny,dilated, kernel);

    List<MatOfPoint> contours = new ArrayList<>();
    //find contours
    Imgproc.findContours(dilated, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
    //draw contours

    Imgproc.cvtColor(capturedFrame, capturedFrame, Imgproc.COLOR_BGR2RGB);
    for(int i = 0; i < contours.size(); i++){
        Imgproc.drawContours(capturedFrame, contours, i, new Scalar(0, 0, 255), -1);
    }

    Imgcodecs.imwrite("after.png", capturedFrame);

    Imshow img = new Imshow("firstImg");    
    img.show(capturedFrame);
}

Here is the initial image:

enter image description here

Here is the image with the contours drawn:

enter image description here

1

1 Answers

1
votes

To draw non filled contours use non negative thickness: Imgproc.drawContours(capturedFrame, contours, i, new Scalar(0, 0, 255), 1); for instance.

To remove unnecessary find contour area, and just skip too large or too small of them when drawing.