how to remove straight lines or non-curvical lines in a canny image(如何去除精明图像中的直线或非曲线)
问题描述
我有一个精明的边缘图像
I have a canny edge image
我想删除除看起来像半圆/椭圆或C"的线条之外的所有线条.尝试了 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.
推荐答案
一个简单的方法是:
- 查找连接的组件
- 找到最小的定向边界框
- 计算框的纵横比,并检查它是否被拉长.
在你的图片上,我用红色几乎直线标记,用绿色标记曲线.你可以玩纵横比的阈值:
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;
}
这篇关于如何去除精明图像中的直线或非曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何去除精明图像中的直线或非曲线
 
				
         
 
            
        基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				