检测附着在图像上其他地方的小方块 [英] Detecting small squares attached to something else on the image

查看:148
本文介绍了检测附着在图像上其他地方的小方块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to detect small squares circled in red on the image. But the problem is that they are on another white line. I want to know how to separate those squares from the white line and detect them.

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

I have used OpenCV Python to write the code. What I have done until now is that I cropped the image so that I get access only to the circular part of the image. Then I cropped the image to get the required part that is the white line. Then I used erosion so that the white line will vanish and the squares remain in the image. Then used Hough circles to detect the squares. This does work for some images but it cannot be generalized. Please help me in finding a generalized code for this. Let me know the logic and also the python code.

任何人都可以帮助我在图像上检测到该aruco标记.它被拒绝了.我不知道为什么. 图片在此链接中. 在图像上检测小方块

Also could anyone help me detect that aruco marker on the image. Its getting rejected. I dont know why. Image is in this link. Detect small squares on an image

推荐答案

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

here's C++ code with distanceTransform. Since I nearly only used openCV functions, you can probably easily convert it to Python code.

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

I removed the white stripe at the top of the image manually, hope this isn't a problem.

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

this it the squaresMask after distance transform

这就是结果

这篇关于检测附着在图像上其他地方的小方块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆