我写了一个简单的代码来执行Hough变换并显示线条。代码如下,
#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("%d\n",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 = %ld\n",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”窗口中的输出图像中获得红线(或任何颜色)。我只获得了白色图像。我还在Hough变换之前运行了简单的Canny边缘检测。这会导致问题吗?
有什么建议,我可以得到一个颜色线上显示?
2条答案
按热度按时间jdgnovmf1#
你是在精明的灰色形象上画霍夫线。
mrzz3bfm2#
您需要确保将彩色图像放入Hough变换。