如何删除精确图像中的直线或非曲线 [英] how to remove straight lines or non-curvical lines in a canny image

查看:343
本文介绍了如何删除精确图像中的直线或非曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张精巧的边缘图片

I have a canny edge image

我想删除除行之外的所有行看起来像一个半圆/椭圆或'C'。 Tried Hough Circle转换,它检测所有曲线。不需要。

I want to remove all line except the lines that look like a semi-circle/ellipse or a 'C'. Tried Hough Circle transforms, it detects all curves.Don't need that.

推荐答案

一个简单的方法是:


  1. 查找已连接的组件

  2. 查找最小有向边界框

  3. 计算盒子的宽高比,并检查它是否太长拉长

  1. Find connected components
  2. Find the minimum oriented bounding box
  3. Compute the aspect ratio of the box, and check if it's too much elongated.

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

On your image, I marked in red almost straight lines, and in green the curved lines. You can play with the threshold on the aspect ratio:

代码:

#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;
}

这篇关于如何删除精确图像中的直线或非曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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