gpt4 book ai didi

opencv - 使用 canny/hough 对非常小的线进行边缘检测

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:55 24 4
gpt4 key购买 nike

我是 opencv 的新手。我想检测图像中非常小的线条并将它们存储为线条数组。我的图像中不会有超过 10 条线(对齐标记)。我已经使用 opencv 示例教程使用 canny 和 Hough 检测线条。

为了测试,我的输入图像有一组大小不同的线条:15、30、50、75 和 100 像素

应用程序不检测 15 和 30 像素线。它检测 4 条 50 像素线中的 2 条。75 和 100 像素线检测良好。

如何检测 15 和 30 像素线?这是我的 Input image这是我的 output image这是我的来源

/*
* https://github.com/opencv/opencv/blob/master/samples/cpp/tutorial_code/ImgTrans/HoughLines_Demo.cpp
*/

/**
* @file HoughLines_Demo.cpp
* @brief Demo code for Hough Transform
* @author OpenCV team
*/

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

/// Global variables

/** General variables */
Mat src, edges;
Mat src_gray;
Mat standard_hough, probabilistic_hough;
int min_threshold = 50;
int max_trackbar = 150;

const char* standard_name = "Standard Hough Lines Demo";
const char* probabilistic_name = "Probabilistic Hough Lines Demo";

int s_trackbar = max_trackbar;
int p_trackbar = max_trackbar;

/// Function Headers
void help();
void Standard_Hough( int, void* );
void Probabilistic_Hough( int, void* );

/**
* @function main
*/
int main( int argc, char** argv )
{
// Read the image
String imageName("../data/building.jpg"); // by default
if (argc > 1)
{
imageName = argv[1];
}
src = imread( imageName, IMREAD_COLOR );

if( src.empty() )
{ help();
return -1;
}

/// Pass the image to gray
cvtColor( src, src_gray, COLOR_RGB2GRAY );

/// Apply Canny edge detector
Canny( src_gray, edges, 50, 200, 3 );

/// Create Trackbars for Thresholds
char thresh_label[50];
sprintf( thresh_label, "Thres: %d + input", min_threshold );

namedWindow( standard_name, WINDOW_AUTOSIZE );
createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);

namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);

/// Initialize
Standard_Hough(0, 0);
Probabilistic_Hough(0, 0);
waitKey(0);
return 0;
}

/**
* @function help
* @brief Indications of how to run this program and why is it for
*/
void help()
{
printf("\t Hough Transform to detect lines \n ");
printf("\t---------------------------------\n ");
printf(" Usage: ./HoughLines_Demo <image_name> \n");
}

/**
* @function Standard_Hough
*/
void Standard_Hough( int, void* )
{
vector<Vec2f> s_lines;
cvtColor( edges, standard_hough, COLOR_GRAY2BGR );

/// 1. Use Standard Hough Transform
HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );

/// Show the result
for( size_t i = 0; i < s_lines.size(); i++ )
{
float r = s_lines[i][0], t = s_lines[i][1];
double cos_t = cos(t), sin_t = sin(t);
double x0 = r*cos_t, y0 = r*sin_t;
double alpha = 1000;

Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
line( standard_hough, pt1, pt2, Scalar(255,0,0), 3, LINE_AA);
}

imshow( standard_name, standard_hough );
}

/**
* @function Probabilistic_Hough
*/
void Probabilistic_Hough( int, void* )
{
vector<Vec4i> p_lines;
cvtColor( edges, probabilistic_hough, COLOR_GRAY2BGR );

/// 2. Use Probabilistic Hough Transform
HoughLinesP( edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );

/// Show the result
for( size_t i = 0; i < p_lines.size(); i++ )
{
Vec4i l = p_lines[i];
line( probabilistic_hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, LINE_AA);
}

imshow( probabilistic_name, probabilistic_hough );
}

TIA

最佳答案

代码中的变量 min_threshold 已设置为 50,因此它不会考虑任何小于该值的候选线段。有一点噪声,50 像素的线段很容易被跳过。

您可以将此变量设置为小于 15 的值,以便考虑所有线段。

关于opencv - 使用 canny/hough 对非常小的线进行边缘检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com