如何删除精明图像中的直线或非曲线

2024-03-03

I have a canny edge image enter image description here

我想删除除了看起来像半圆/椭圆或“C”的线之外的所有线。尝试过霍夫圆变换,它检测所有曲线。不需要。


一个简单的方法是:

  1. 查找连接的组件
  2. 找到最小定向边界框
  3. 计算盒子的长宽比,并检查是否太大拉长的.

在你的图片上,我用红色标记了几乎直线,绿色为曲线。您可以使用宽高比的阈值:

Code:

#include<opencv2/opencv.hpp>
using namespace cv;


int main()
{
    // Load image
    Mat1b img = imread("path_to_img", IMREAD_GRAYSCALE);

    // Create output image
    Mat3b out;
    cvtColor(img, out, COLOR_GRAY2BGR);

    // Find contours
    vector<vector<Point>> contours;
    findContours(img.clone(), contours, RETR_LIST, CHAIN_APPROX_NONE);

    for (const auto& contour : contours)
    {
        // Find minimum area rectangle
        RotatedRect rr = minAreaRect(contour);

        // Compute aspect ratio
        float aspect_ratio = min(rr.size.width, rr.size.height) / max(rr.size.width, rr.size.height);

        // Define a threshold on the aspect ratio in [0, 1]
        float thresh = 0.2f;

        Vec3b color;
        if (aspect_ratio < thresh) { 
            // Almost straight line
            color = Vec3b(0,0,255); // RED
        }
        else {
            // Curved line
            color = Vec3b(0, 255, 0); // GREEN
        }

        // Color output image
        for (const auto& pt : contour) {
            out(pt) = color;
        }
    }

    imshow("Out", out);
    waitKey();

    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何删除精明图像中的直线或非曲线 的相关文章

随机推荐