【问题标题】:OpenCV detecting largest rectangle yields puzzling resultsOpenCV 检测最大矩形会产生令人费解的结果
【发布时间】:2016-10-12 12:21:35
【问题描述】:

我的目标是检测图像中最大的矩形,无论它是否倾斜。经过一些研究和谷歌搜索,我想出了一个理论上应该可以工作的代码,但是在一半的情况下,我看到了令人费解的结果。

我在 Android 上使用过 OpenCV,代码如下:

private void find_parallels() {
    Utils.bitmapToMat(selectedPicture,img);
    Mat temp = new Mat();
    Imgproc.resize(img,temp,new Size(640,480));
    img = temp.clone();

    Mat imgGray = new Mat();
    Imgproc.cvtColor(img,imgGray,Imgproc.COLOR_BGR2GRAY);

    Imgproc.GaussianBlur(imgGray,imgGray,new Size(5,5),0);

    Mat threshedImg = new Mat();
    Imgproc.adaptiveThreshold(imgGray,threshedImg,255,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,11,2);

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Mat imageContours = imgGray.clone();
    Imgproc.cvtColor(imageContours,imageContours,Imgproc.COLOR_GRAY2BGR);

    Imgproc.findContours(threshedImg,contours,hierarchy,Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
    max_area = 0;
    int num = 0;

    for (int i = 0; i < contours.size(); i++) {
        area = Imgproc.contourArea(contours.get(i));

        if (area > 100) {
            MatOfPoint2f mop = new MatOfPoint2f(contours.get(i).toArray());
            peri = Imgproc.arcLength(mop, true);
            Imgproc.approxPolyDP(mop, approx, 0.02 * peri, true);

            if(area > max_area && approx.toArray().length == 4) {
                biggest = approx;
                num = i;
                max_area = area;
            }

        }

    }

    selectedPicture = Bitmap.createBitmap(640,480, Bitmap.Config.ARGB_8888) ;
    Imgproc.drawContours(img,contours,num,new Scalar(0,0,255));
    Utils.matToBitmap(img, selectedPicture);

    imageView1.setImageBitmap(selectedPicture);}

在某些情况下,它的效果非常好,如图所示(请参阅显示器边框和屏幕之间的白线.. 抱歉颜色): 有效的例子:

但是,在此图像中,以及大多数屏幕为灰色的图像中,它会产生疯狂的结果。 不起作用的示例:

【问题讨论】:

    标签: java android opencv detection opencv-contour


    【解决方案1】:

    尝试使用形态学,扩张然后腐蚀相同的内核应该会更好。 或者使用 pyrDown + pyrUp,或者只是模糊它。

    简而言之,使用低通滤波器类的方法,因为您感兴趣的对象比噪声大得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      • 2021-09-17
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多