参考:https://docs.opencv.org/3.4.5/d9/db0/tutorial_hough_lines.html
参考:https://docs.opencv.org/3.4.5/d4/d70/tutorial_hough_circle.html
若有表达不当或错误欢迎留言指正,互相交流学习,共同进步,目前还在学习,没有过多纠结于原理问题,日后有机会补充
在本教程中,您将学习如何:
参考官方文档:https://docs.opencv.org/3.4.5/d9/db0/tutorial_hough_lines.html
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{ //
VideoCapture capture(0); //
//【2】循环显示每一帧 //
while (1) //
{ //
Mat image; //定义一个Mat变量,用于存储每一帧的图像 //
capture >> image; //读取当前帧 //
//================================================================
Mat gray = image.clone();
Mat dst = image.clone();
Mat cdst = image.clone();
Mat cdstP;
int kernel_size = 3;
int ratio = 4;
double lowThreshold = 50;
cvtColor(image, gray, CV_BGR2GRAY);//转化成灰度图像
Canny(gray, dst, lowThreshold, lowThreshold*ratio, kernel_size);
cvtColor(dst, cdst, COLOR_GRAY2BGR);
cdstP = cdst.clone();
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI / 180, 150, 0, 0);
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(cdst, pt1, pt2, Scalar(0, 0, 255), 3, LINE_AA);
}
vector<Vec4i> linesP;
HoughLinesP(dst, linesP, 1, CV_PI / 180, 50, 10, 10);
for (size_t i = 0; i < linesP.size(); i++)
{
Vec4i l = linesP[i];
line(cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
}
imshow("Source", gray);
imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
//================================================================
//
//
waitKey(30); //延时30ms //
} //
return 0; //
} //
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{ //
VideoCapture capture(0); //
//【2】循环显示每一帧 //
while (1) //
{ //
Mat image; //定义一个Mat变量,用于存储每一帧的图像 //
capture >> image; //读取当前帧 //
//================================================================
Mat gray = image.clone();
Mat dst = image.clone();
Mat cdst = image.clone();
Mat cdstP;
cvtColor(image, gray, CV_BGR2GRAY);//转化成灰度图像
medianBlur(gray, gray, 5);
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows / 16, // change this value to detect circles with different distances to each other
100, 30, 1, 30 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
for (size_t i = 0; i < circles.size(); i++)
{
Vec3i c = circles[i];
Point center = Point(c[0], c[1]);
// circle center
circle(image, center, 1, Scalar(0, 100, 100), 3, LINE_AA);
// circle outline
int radius = c[2];
circle(image, center, radius, Scalar(255, 0, 255), 3, LINE_AA);
}
imshow("detected circles", image);
//================================================================
//
//
waitKey(30); //延时30ms //
} //
return 0; //
} //
int main()
{
VideoCapture capture(0);
//【2】循环显示每一帧
while (1) //
{ //
Mat image; //定义一个Mat变量,用于存储每一帧的图像 //
capture >> image; //读取当前帧 //
//================================================================
Mat gray = image.clone();
Mat dst = image.clone();
Mat cdst = image.clone();
Mat cdstP;
int kernel_size = 3;
int ratio = 4;
double lowThreshold = 50;
cvtColor(image, gray, CV_BGR2GRAY);//转化成灰度图像
Canny(gray, dst, lowThreshold, lowThreshold*ratio, kernel_size);
cvtColor(dst, cdst, COLOR_GRAY2BGR);
cdstP = cdst.clone();
vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI / 180, 150, 0, 0);
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(cdst, pt1, pt2, Scalar(0, 0, 255), 3, LINE_AA);
}
vector<Vec4i> linesP;
HoughLinesP(dst, linesP, 1, CV_PI / 180, 50, 10, 10);
for (size_t i = 0; i < linesP.size(); i++)
{
Vec4i l = linesP[i];
line(cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 3, LINE_AA);
}
imshow("Source", gray);
imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);