Finding largest contour - OpenCV - Java

2.1k views Asked by At

I am trying to find the largest countour in an image after performing some per-processing steps such as: 1) GrayScale 2) Threshold - binarization 3) Canny

Then code for finding contour: List contours = new ArrayList();

        Imgproc.findContours(img_gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        double maxArea = -1;
        int maxAreaIdx = -1;
        MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint largest_contour = contours.get(0);
        //largest_contour.ge
        List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
        //Imgproc.drawContours(imgSource,contours, -1, new Scalar(0, 255, 0), 1);

        for (int idx = 0; idx < contours.size(); idx++) {
            temp_contour = contours.get(idx);
            double contourarea = Imgproc.contourArea(temp_contour);
            //compare this contour to the previous largest contour found
            if (contourarea > maxArea) {
                //check if this contour is a square
                MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
                int contourSize = (int)temp_contour.total();
                MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
                Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize*0.05, true);
                if (approxCurve_temp.total() == 4) {
                    maxArea = contourarea;
                    maxAreaIdx = idx;
                    approxCurve=approxCurve_temp;
                    largest_contour = temp_contour;
                }
            }
        }

       Imgproc.cvtColor(img_gray, img_gray, Imgproc.COLOR_BayerBG2RGB);
       Mat sourceImage =Highgui.imread(filePath + filename);
       double[] temp_double;
       temp_double = approxCurve.get(0,0);       
       Point p1 = new Point(temp_double[0], temp_double[1]);

       //Core.circle(imgSource,p1,55,new Scalar(0,0,255));
       //Imgproc.warpAffine(sourceImage, dummy, rotImage,sourceImage.size());
       temp_double = approxCurve.get(1,0);       
       Point p2 = new Point(temp_double[0], temp_double[1]);           

      // Core.circle(imgSource,p2,150,new Scalar(255,255,255));
       temp_double = approxCurve.get(2,0);       
       Point p3 = new Point(temp_double[0], temp_double[1]);
       //Core.circle(imgSource,p3,200,new Scalar(255,0,0));
       temp_double = approxCurve.get(3,0);       
       Point p4 = new Point(temp_double[0], temp_double[1]);
      // Core.circle(imgSource,p4,100,new Scalar(0,0,255));

As you can see that I am trying to get the four corner points namely, P1, P2, P3, P4.

The issue is that I am not getting the correct points when I click an image from my phone camera but it works well when I take an image from the internet.

For example, I take the below given image from my phone: Camera Image

The corner points detected are:

p1 = 1485.0-922.0
p2 = 1469.0-922.0
p3 = 1485.0-906.0
p4 = 1469.0-906.0

Which is disgusting. How can the x-y value be same for 2 points.

perspective transform from these points: Perspective transform

When I take an image from internet all the points are calculated with perfection.

Its clearly something from the background.

Can someone help me to sort this out?

0

There are 0 answers