如何在OpenCV中找到矩形对象的角落?

13
我正在使用openCV库在Android平台上进行开发。我已经成功从图像中检测到最大的矩形,但由于我的应用程序将用于扫描目的,因此我还想具备透视变换功能。
我知道如何应用perspectiveTransform和warpPerspectiveTransform,但为此我需要源点所在矩形的角落位置。
考虑到我们拥有第一个角(左上角)的坐标和Rect对象关联的宽度/高度,找到这些角似乎非常容易,但问题是对于旋转矩形(通常是boundingRect但边缘不平行于轴线),这些值会有所不同。在这种情况下,它存储与另一个边缘平行于轴线且覆盖旋转矩形的矩形相关的值,这使我无法检测实际矩形的角。
我还希望对从图像中检测纸张的这两个算法进行比较:
1. Canny边缘检测->最大轮廓->最大矩形->查找角点->透视变换 2. Canny边缘检测->Hough线->线交点->透视变换
我想问的事情是,如果我们有一个Rect对象,如何获取该矩形的所有角落位置?
谢谢。
2个回答

12

我很兴奋地回答我的问题!这很容易,但当你刚开始使用不太适当的相关文档时,就会发生这种情况。

我努力尝试获取一般矩形的角落,但在openCV实现中没有定义,因此几乎是不可能的。

我遵循stackoverflow上最大正方形检测的标准代码,可以使用approxCurve轻松找到角落。

//将图像转换为黑白

        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

        //convert the image to black and white does (8 bit)
        Imgproc.Canny(imgSource, imgSource, 50, 50);

        //apply gaussian blur to smoothen lines of dots
        Imgproc.GaussianBlur(imgSource, imgSource, new  org.opencv.core.Size(5, 5), 5);

        //find the contours
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        double maxArea = -1;
        int maxAreaIdx = -1;
        Log.d("size",Integer.toString(contours.size()));
        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(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
       sourceImage =Highgui.imread(Environment.getExternalStorageDirectory().
                 getAbsolutePath() +"/scan/p/1.jpg");
       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));
       List<Point> source = new ArrayList<Point>();
       source.add(p1);
       source.add(p2);
       source.add(p3);
       source.add(p4);
       Mat startM = Converters.vector_Point2f_to_Mat(source);
       Mat result=warp(sourceImage,startM);
       return result;

透视变换所使用的函数如下:

 public Mat warp(Mat inputMat,Mat startM) {
            int resultWidth = 1000;
            int resultHeight = 1000;

            Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);



            Point ocvPOut1 = new Point(0, 0);
            Point ocvPOut2 = new Point(0, resultHeight);
            Point ocvPOut3 = new Point(resultWidth, resultHeight);
            Point ocvPOut4 = new Point(resultWidth, 0);
            List<Point> dest = new ArrayList<Point>();
            dest.add(ocvPOut1);
            dest.add(ocvPOut2);
            dest.add(ocvPOut3);
            dest.add(ocvPOut4);
            Mat endM = Converters.vector_Point2f_to_Mat(dest);      

            Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

            Imgproc.warpPerspective(inputMat, 
                                    outputMat,
                                    perspectiveTransform,
                                    new Size(resultWidth, resultHeight), 
                                    Imgproc.INTER_CUBIC);

            return outputMat;
        }

1
这是迄今为止在Java中获取透视图和查找边界的最佳解决方案。感谢您提供的代码。 - Ujju
@AnkurGautam,你能解释一下吗?为什么我使用Imgproc.FindContours方法时,List<MatofPoint>中没有数据呢?出了什么问题。谢谢! - XTL
@AnkurGautam,我怎样可以私下联系你?谢谢! - XTL
你能分享一下你在stackoverflow上关于最大正方形检测的标准代码的链接吗? - Rajan Rawal
我希望你能更准确地回答它。 - Mubashir Murtaza

2
1. find the coordinate of left ,right, top,bottom point of the rect
2. min_x = min(left.x right.x,top.x,bottom.x)
   min_y = min(left.y right.y,top.y,bottom.y)
   max_x = max(left.x right.x,top.x,bottom.x)
   max_y = max(left.y right.y,top.y,bottom.y)
3. the corners's coordinate is the point with the min_x, min_y ,max_x, max_y

我需要每个角落的坐标,我不认为这会有帮助! - Ankur Gautam
当你找到min_x时,你可以记录坐标吗? - michaeltang
不理解,请解释?如果矩形旋转了一个负角度会怎样? - Ankur Gautam
角落总是满足以下条件:所有点中,至少有一个点的坐标是最小或最大值。 - michaeltang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接