OpenCV:无法在霍夫变换中获得红线



我写了一个简单的代码来执行霍夫变换并显示行。代码如下,

#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;
Mat img;
Mat display;
Mat temp;
void CannyThreshold()
{
cvtColor(img, display, COLOR_RGB2GRAY);
// GaussianBlur(display,display,Size(7,7),3,3);
GaussianBlur(display, display, Size(1, 1), 1,1);
// printf("%dn",lowThreshold);
Canny(display,display,lowThreshold,3);
imshow("Canny",display);
}
void Hough()
{

Canny(temp,display,50,3);
vector<Vec2f> lines; // will hold the results of the detection
HoughLines(display, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection    
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line(display, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
}
printf("Lines = %ldn",lines.size());
imshow("Hough",display);
}
int main()
{
VideoCapture cap(0);
namedWindow("Canny");
createTrackbar("Min Threshold: ","Canny",&lowThreshold,max_lowThreshold);
while(1)
{
cap.read(img);
temp = img;
CannyThreshold();
Hough();
waitKey(1);
}
cap.release();
return 0;
}

我无法在窗口中的输出图像中获得红线(或任何颜色(;Hough";。我只得到一张黑白照片。我还在霍夫变换之前运行一个简单的Canny边缘检测。这会引起问题吗?

有什么建议可以让我的彩色线条显示出来吗?

您正在canny的灰色图像上绘制Hough线。

最新更新