【问题标题】:Detecting small squares attached to something else on the image检测附加到图像上其他东西的小方块
【发布时间】:2020-05-11 22:30:53
【问题描述】:

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

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

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

【问题讨论】:

  • 你可以在黑白图像上尝试 distanceTransform 并在那里找到局部最大值。
  • 我是 OpenCV 的新手。你能告诉我一些关于距离变换的事情吗?就像它实际做的那样。
  • 对于所有非黑色像素:计算到最近黑色像素的距离。
  • 何为霍夫?在侵蚀之后,您已经检测到了正方形。为什么检测不够好?
  • 这不是一个可行的解决方案,因为会在白线上放置一个机器人,这会使检测白圈变得更加困难。那么有什么方法可以将正方形与白线分开?

标签: python opencv image-processing opencv-contour opencv-python


【解决方案1】:

这是带有 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);
}

这是输入:

这是距离变换后的 squaresMask

这就是结果

【讨论】:

  • 感谢您的解决方案。有没有其他方法可以通过将正方形与白线分开来解决这个问题?因为我知道白圈上会有一个机器人!!
  • 对不起,我不明白。你能用这样的机器人展示一张图片吗?你也许可以检测到白色圆圈被打断的​​位置。
  • 参考本网站上提出的问题。我在那里提供了一张图片。 stackoverflow.com/questions/59954111/…
  • 也许你可以尝试用不同的方法检测机器人,而不是找到方块
  • 这对检测正方形有何帮助?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 2011-02-23
  • 2020-12-23
  • 2018-05-02
  • 1970-01-01
相关资源
最近更新 更多