检测附着在图像上其他地方的小方块

问题描述 投票:1回答:1

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

Squares to be detected encircled in red

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

python opencv image-processing opencv-contour opencv-python
1个回答
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);
}

这是输入:

enter image description here

此为距离转换后的squaresMask

enter image description here

这是结果

enter image description here


0
投票

这里是带有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

© www.soinside.com 2019 - 2024. All rights reserved.