OpenCV学习笔记-Hough变换怎么用

文章目录

  • 目标
  • 原理
  • 源码
    • 霍夫线变换
    • 霍夫圆变换
  • 程序说明

参考: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
若有表达不当或错误欢迎留言指正,互相交流学习,共同进步,目前还在学习,没有过多纠结于原理问题,日后有机会补充

目标

在本教程中,您将学习如何:

  • 使用OpenCV函数HoughLines()和HoughLinesP()来检测图像中的线条。
  • 使用OpenCV函数HoughCircles()来检测图像中的圆圈。

原理

参考官方文档: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);
  • 转换为BGR图
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);

你可能感兴趣的:(OpenCV)