Ask Your Question

thatc0der's profile - activity

2020-10-04 12:06:42 -0600 received badge  Nice Question (source)
2020-04-01 11:53:26 -0600 received badge  Notable Question (source)
2019-08-27 01:12:53 -0600 received badge  Popular Question (source)
2019-07-19 08:21:59 -0600 received badge  Notable Question (source)
2018-11-25 15:54:17 -0600 received badge  Popular Question (source)
2017-08-15 15:13:31 -0600 received badge  Editor (source)
2017-08-15 14:35:30 -0600 asked a question Determining source color space of a Mat

I am working on a program where I have these contours I am looking at and I want to find the RGB values of each specific contour. The current problem I am facing is that I the mat is of CvType CV32_F . I change the images rtype to CV_8UC3. I want to know what color space the Mat is in so that I can convert it to the RGB spectrum. I never set the Mat to a color space so I am not sure which one it is. So I get conversion errors. I have tried BGR, GRAY and RGB2BGR( just to see if it was already in that spectrum and would change). Here is my code:

static List<Mat> foundFrames = new ArrayList<>();
private static Color[] colorArray = new Color[54];
private static int currentIndex = 0;

private static void findRGBs(List<MatOfPoint> squareContours){
    for(int i = 0; i < squareContours.size(); i++){
        squareContours.get(i).convertTo(squareContours.get(i), CvType.CV_8UC3);
        Imgproc.cvtColor(squareContours.get(i), squareContours.get(i), Imgproc.COLOR_BGR2RGB);
         //not sure what color space this image is 
        getPixelValues(squareContours.get(i));
        foundFrames.add(squareContours.get(i));
    }
}
2017-08-14 07:33:06 -0600 commented question How to remove overlapping contours

what would that be like? How would I be able to check to see if one contour is being overlapped by the rest of the other contours? @StevenPuttenmans

2017-08-13 21:58:15 -0600 commented answer How to remove overlapping contours

changing the threasholds and blurring allowed me to get the qualities I wanted. To solidify it I used your groupingrectangles. Thank you so much for the help. I am sure someone else will also find this very helpful.

2017-08-13 13:56:44 -0600 commented answer How to remove overlapping contours

Good news, this helps with detection but the problem is still there, I am currently trying to check if one Rect is in another rectangle and not draw those rects that are inside.

2017-08-12 22:36:36 -0600 commented answer How to remove overlapping contours

Question, why erode the image if I dilate it already to make the contours easier to find?

2017-08-12 22:27:10 -0600 commented answer How to remove overlapping contours

how would I go about doing that?

2017-08-12 21:45:45 -0600 commented answer How to remove overlapping contours

Lol your last link has me in that blog post I contributed to that :). I think I have an idea of how to use the groupRectangles, I will let you know if figure it out. Thanks for all the help.

2017-08-12 21:34:30 -0600 commented answer How to remove overlapping contours

I am pretty close its just this annoying overlapping thing that is giving me problems.

2017-08-12 21:02:39 -0600 commented answer How to remove overlapping contours

I know that however I am writing my own, just for the experience and fun of it.

2017-08-12 20:59:41 -0600 commented answer How to remove overlapping contours

I want to be able to detect the squares, grab their rgb values take that and represent it in a form my solver understands and it gets me the solution for it.

2017-08-12 20:26:26 -0600 commented answer How to remove overlapping contours

thanks but it isn't so much about the rectangles, it is able the overlapping contours. I use the rectangles for debugging, I mean I could use them in the main program but is there a way i can distinctly remove the overlapping contours? It is just that I use the rectangles to show were these overlapping contours are because its much easier to see.

2017-08-12 18:36:21 -0600 asked a question How to remove overlapping contours

I am working on a program where I am trying to extract the colored squares from a puzzle. I take a frame from a video capture then find the all contours. I then remove contours that aren't in the shape of a square (This works alright but looking for a better method). The main problem I am facing is that there are overlapping contours. I use RETR_TREE to get all contours but when using RETR_EXTERNAL The contours become harder to detect. Is there a way I can improve the detection of squares? Or a way that I can remove the overlapping contours in the image.

Here is an image of where there are overlapping contours: There were 11 contours found in this image but I want only 9.(I draw the rects to see the overlapping a little easier) Overlapping image

. How can I remove the inner contours? Here is my code:

public Mat captureFrame(Mat capturedFrame){

    Mat newFrame = new Mat();
    capturedFrame.copyTo(newFrame); 

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

    //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);

    //Dilate image to increase size of lines
    Mat kernel = Imgproc.getStructuringElement(1, new Size(3,3));
    Mat dilated = new Mat();
    Imgproc.dilate(canny,dilated, kernel);


    List<MatOfPoint> contours = new ArrayList<>();
    List<MatOfPoint> squareContours = new ArrayList<>();

    //find contours
    Imgproc.findContours(dilated, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);


    //Remove contours that aren't close to a square shape.
    //Wondering if there is a way I can improve this?
    for(int i = 0; i < contours.size(); i++){

        double area = Imgproc.contourArea( contours.get(i)); 
        MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());

        double perimeter = Imgproc.arcLength(contour2f, true);
        //Found squareness equation on wiki... 
        //https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
        double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);

        //add contour to new List if it has a square shape.
        if(squareness >= 0.7 && squareness <= 0.9 && area >= 3000){
           squareContours.add(contours.get(i));
        }
    }

    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for(int n = 0; n < squareContours.size(); n++){

        //Convert contours(n) from MatOfPoint to MatOfPoint2f
        MatOfPoint2f contour2f = new MatOfPoint2f( squareContours.get(n).toArray());
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint( approxCurve.toArray());

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);
        //length and width should be about the same
        if(rect.height - rect.width < Math.abs(10)){
            System.out.printf("%s , %s \n", rect.height, rect.width);
        }

         // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
        Imgproc.rectangle(newFrame, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar (255, 0, 0, 255), 3); 

        }  

    return newFrame;
}
2017-08-12 08:34:30 -0600 received badge  Enthusiast
2017-08-11 10:07:08 -0600 commented answer Removed contours aren't gone

sorry did do that just now :) the Internet was down thank you for all the help :)

2017-08-11 08:27:16 -0600 commented answer Removed contours aren't gone

Thanks for the help I used your earlier listed method also for any one that ends up looking at this in the future. My condition also wasn't correct. IF I want to add the squareContours between 0.7 and 0.8 the condition would be if(squareness >= 0.7 && squareness <= 0.8) .

2017-08-11 07:01:01 -0600 asked a question Removed contours aren't gone

I am trying to remove any contours that aren't in a square like shape. I check the image before and after to see if any contours have been removed. I use the circularity formula and values between 0.7 and 0.8 are square shaped. I expect to see that some contour lines are removed but none are

Here is what I have done so far.

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);

        //Dilate image to increase size of lines
        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);

        //convert image 
        Imgproc.cvtColor(capturedFrame, capturedFrame, Imgproc.COLOR_BGR2RGB);


        //Draw contours on original image
        for(int n = 0; n < contours.size(); n++){
            Imgproc.drawContours(capturedFrame, contours, n, new Scalar(255, 0 , 0), 1);
        }

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

        //display image with all contours
        Imshow showImg = new Imshow("displayImage");
        showImg.show(capturedFrame);


        //Remove contours that aren't close to a square shape.
        for(int i = 0; i < contours.size(); i++){

            double area = Imgproc.contourArea( contours.get(i)); 
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(i).toArray());
            double perimeter = Imgproc.arcLength(contour2f, true);

            //Found squareness equation on wiki... 
            // https://en.wikipedia.org/wiki/Shape_factor_(image_analysis_and_microscopy)
            double squareness = 4 * Math.PI * area / Math.pow(perimeter, 2);

            System.out.println("Squareness: " + squareness);


            if(squareness <= 0.7 && squareness >=  0.8){
                contours.remove(i);
            }
        }


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

        showImg.show(capturedFrame);
        Imgcodecs.imwrite("remove.png", capturedFrame);

    }

Here is the original image:

enter image description here

Here is the image before any contours are removed:

enter image description here

Here is the image final image where contours some contours should be removed:

enter image description here

2017-08-10 13:04:29 -0600 commented answer eliminate unwanted contours opencv

Thank you for the extra help :) So I tired doing something like this

    for(int i = 0; i < contours.size(); i++){

             double area = Imgproc.contourArea( contours.get(i),false); 
             MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray());

             double arch = Imgproc.arcLength(contour2f, true);

             double squareness = 4 * Math.PI * area / Math.pow(arch,2);

             System.out.println("Squareness: " + squareness);

             if(squareness <= 0.7 && squareness >=  0.8){
                 contours.remove(i);
             }

        }
2017-08-10 10:21:55 -0600 received badge  Scholar (source)
2017-08-10 10:16:15 -0600 received badge  Supporter (source)
2017-08-10 10:11:35 -0600 commented answer eliminate unwanted contours opencv

Thanks this helps, however is there a way I could make it more "flexible" like looking if it has some sort of square shape, like the lengths of the sides are the same and or has almost 90 degrees in the contour? The reason I ask is because the image won't always be this one because it is a captured frame the cube could be captured closer or further away.

2017-08-10 10:11:03 -0600 received badge  Student (source)
2017-08-10 09:51:10 -0600 asked a question eliminate unwanted contours opencv

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