寻找最大轮廓 - OpenCV - Java

时间:2014-11-15 19:16:08

标签: java opencv contour corner-detection

我尝试在执行一些按处理步骤后找到图像中最大的countour,例如: 1)GrayScale 2)阈值 - 二值化 3)Canny

然后找到轮廓的代码:      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));

你可以看到我试图获得四个角点,即P1,P2,P3,P4。

问题在于,当我从手机摄像头点击图像时,我没有得到正确的分数,但是当我从互联网上拍摄图像时效果很好。

例如,我从手机中拍摄下面给出的图像: Camera Image

检测到的角点是:

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

哪个恶心。如何使2-x的x-y值相同。

从这些观点转变: Perspective transform

当我从互联网上拍摄图像时,所有点都是完美计算的。

显然是背景。

有人可以帮我解决这个问题吗?

0 个答案:

没有答案
相关问题