python - 检测图像上其他物体上附着的小方 block

标签 python opencv image-processing contour

我想检测图像上用红色圈出的小方 block 。但问题是他们处在另一条白线上。我想知道如何将这些方 block 与白线分开并检测它们。

Squares to be detected encircled in red

我使用 OpenCV Python 编写代码。到目前为止我所做的是裁剪图像,以便只能访问图像的圆形部分。然后我裁剪图像以获得所需的白线部分。然后我使用腐 eclipse ,使白线消失,正方形保留在图像中。然后使用霍夫圆来检测正方形。这确实适用于某些图像,但不能一概而论。请帮我找到一个通用的代码。让我知道逻辑和Python代码。 Cropped image with the arena(ring)

Cropped image with the white line

Eroded image with remains of those 8 squares after erosion

Final image with detected squares

还有谁能帮我检测图像上的 aruco 标记。它被拒绝了。我不知道为什么。 图片在此链接中。 Detect small squares on an image

最佳答案

这里是带有 distanceTransform 的 C++ 代码。 由于我几乎只使用 openCV 函数,因此您可以轻松地将其转换为 Python 代码。

我手动删除了图像顶部的白色条纹,希望这不是问题。

int main()
{
    cv::Mat input =  cv::imread("C:/StackOverflow/Input/SQUARES.png", cv::IMREAD_GRAYSCALE);
    cv::Mat thres = input > 0; // make binary mas
    cv::Mat dst;
    cv::distanceTransform(thres, dst, CV_DIST_L2, 3);

    double min, max;
    cv::Point minPt, maxPt;
    cv::minMaxLoc(dst, &min, &max, 0, 0);

    double distThres = max*0.65; // a real clustering would be better. This assumes that the white circle thickness is a bout 50% of the square size, so 65% should be ok...

    cv::Mat squaresMask = dst >= distThres;

    cv::imwrite("C:/StackOverflow/Input/SQUARES_mask.png", squaresMask);

    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(squaresMask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);


    cv::Mat output;
    cv::cvtColor(input, output, cv::COLOR_GRAY2BGR);
    for (int i = 0; i < contours.size(); ++i)
    {
        cv::Point2f center;
        float radius;
        cv::minEnclosingCircle(contours[i], center, radius);

        cv::circle(output, center, 5, cv::Scalar(255, 0, 255), -1);
        //cv::circle(output, center, radius, cv::Scalar(255, 0, 255), 1);
    }

    cv::imwrite("C:/StackOverflow/Input/SQUARES_output.png", output);

    cv::imshow("output", output);
    cv::waitKey(0);
}

这是输入:

enter image description here

这是距离变换后的squaresMask

enter image description here

这就是结果

enter image description here

关于python - 检测图像上其他物体上附着的小方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59916048/

相关文章:

Python空构造函数

Python POST 请求未经过登录页面

python - 将文件中的数据插入 SQLite 数据库

java - 如何在NetBeans中打包JavaCV/OpenCV?

Python - 播放带音频的视频(使用 OpenCV?)

javascript - 如何在 JavaScript 中使用 Phonegap 进行图像处理

python - 检查 Python 类中必需参数的最佳方法

c++ - 计算机视觉问题的开源 CRF 实现?

c++ - 尝试在 OpenCv 中使用 sift 匹配两个图像,但匹配太多

优化和正则化