【问题标题】:Detecting largest polygons in an image?检测图像中最大的多边形?
【发布时间】:2019-03-06 10:03:56
【问题描述】:

我正在尝试检测图像中面积最大的多边形,但图像包含一些噪声和小多边形。 有人做过这方面的工作吗? 我现在正在使用 open-cv。

【问题讨论】:

  • 您能否提供一个minimal example 您已经尝试过的内容,以及一些示例图像和所需的输出?

标签: python image opencv polygon


【解决方案1】:

首先我写这个作为答案,因为我不能写 cmets 由于声誉数。

你应该附上一些你尝试过的代码和一些问题的截图。

我会在java中链接一个解决方案,转换成python并不难

  Mat mGray = new Mat();

    MatOfDouble mu = new MatOfDouble();

    MatOfDouble stddev = new MatOfDouble();

    Imgproc.cvtColor(origMat, origMat, Imgproc.COLOR_BGRA2BGR);

    Imgproc.cvtColor(origMat, mGray, Imgproc.COLOR_BGR2GRAY);

    Core.meanStdDev(mGray, mu, stddev);

    Imgproc.GaussianBlur(mGray, mGray, new Size(5, 5), 5);

    //Imgproc.Canny(mGray, mGray, 30, 80, 3, false);   //FOR HIGH BRIGHTNESS
    //Imgproc.Canny(mGray, mGray, 50, 130, 3, false);    // FOR LOW BRIGHTNESS

    Imgproc.Canny(mGray, mGray, mu.get(0, 0)[0], stddev.get(0, 0)[0], 3, false);

    Mat kernell = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 9));

    Imgproc.morphologyEx(mGray, mGray, Imgproc.MORPH_CLOSE, kernell);

    Imgproc.dilate(mGray, mGray, Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3)));

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

    Mat hierarchy = new Mat();

    Imgproc.findContours(mGray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    double largest_area = 0;

    Rect rect = new Rect();
    int largest_idx = 0;

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

        double a = Imgproc.contourArea(contours.get(idx));  //Find the area of contour

        if (a > largest_area) {

            largest_area = a;

            // rect = Imgproc.boundingRect(contours.get(idx));

            largest_idx = idx;
        }
    }

    MatOfPoint2f new_mat = new MatOfPoint2f(contours.get(largest_idx).toArray());

    RotatedRect rbox = Imgproc.minAreaRect(new_mat);

    Point vertices[] = new Point[4];

    rbox.points(vertices);

    List<MatOfPoint> boxContours = new ArrayList<>();

    boxContours.add(new MatOfPoint(vertices));

    //Imgproc.drawContours(origMat, boxContours, 0, new Scalar(255, 255, 0), 2);

    /* *******************************************************************************************
     * draw the detected rotated rect in the original image
     *******************************************************************************************/
    for (int i = 0; i < 4; ++i) {
        Imgproc.line(origMat, vertices[i], vertices[(i + 1) % 4], new Scalar(255, 0, 0));
    }

主要是,我将输入图像转换为灰度,使用高斯滤波器降低噪声,使用图像均值和标准差值计算 Canny 最小和最大阈值,这将根据环境亮度相应地调整阈值。

然后应用 findContours 函数并遍历找到的轮廓并绘制最大的轮廓。

我希望这个解决方案对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 2023-03-20
    • 2016-12-14
    • 1970-01-01
    • 2018-07-07
    • 2020-04-21
    • 2014-03-25
    • 1970-01-01
    相关资源
    最近更新 更多