-
霍夫变换简介:
霍夫变换是一种特征检测(feature extraction),被广泛应用在图像分析(image analysis)、电脑视觉(computer vision)以及数位影像处理(digital image processing)。霍夫变换是用来辨别找出物件中的特征,例如:线条。他的算法流程大致如下,给定一个物件、要辨别的形状的种类,算法会在参数空间(parameter space)中执行投票来决定物体的形状,而这是由累加空间(accumulator space)里的局部最大值(local maximum)来决定。
现在广泛使用的霍夫变换是由RichardDuda和PeterHart在公元1972年发明,并称之为广义霍夫变换(generalizedHoughtransform),广义霍夫变换和更早前1962年的PaulHough的专利有关。经典的霍夫变换是侦测图片中的直线,之后,霍夫变换不仅能识别直线,也能够识别任何形状,常见的有圆形、椭圆形。1981年,因为DanaH.Ballard的一篇期刊论文"Generalizing the Hough transform to detect arbitrary shapes",让霍夫变换开始流行于电脑视觉界。
因此我们首先了解霍夫变换的直线检测 -
霍夫变换(直线检测原理)
霍夫变换(直线检测)的前提是我们已经进行了边缘检测
The simplest case of Hough transform is detecting straight lines. In general, the straight line y = mx + b can be represented as a point (b, m) in the parameter space. However, vertical lines pose a problem. They would give rise to unbounded values of the slope parameter m. Thus, for computational reasons, Duda and Hart[5] proposed the use of the Hesse normal form由上面的叙述我们可以知道,由于直线斜率存在无界值,因而我们要采用极坐标系来进行直线检测。
对于一个像素点其极坐标表示形式为一个r和θ的函数
由此我们可以见到,在r和θ组成的坐标系中,如果两条曲线相交,则证明这两条曲线所表示的像素点在同一直线上,这样子我们就检测出了直线,再通过将极坐标系转化为直角坐标系,这样子我们就完成了直线检测。
-
用到的相关API
①CV_EXPORTS_W void HoughLines( InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn = 0, double stn = 0, double min_theta = 0, double max_theta = CV_PI );
第一个参数为八位的灰度输入图像;
第二个参数为输出的极坐标;
第三个参数为生成极坐标时候的像素扫描步长一般取1;
第四个参数为生成极坐标时候的角度步长,一般取值CV_PI/180;
第五个参数为阈值,只有获得足够多交点的极坐标我们才认为其实直线;
接下来几个参数一般情况均使用默认值
该API最终输出的结果为极坐标空间的结果,还需要转换到平面空间中,一般不常用②
CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength = 0, double maxLineGap = 0 );
该API最终输出的是直线的两个点,较为常用 -
实验代码
#include "iostream" #include "opencv2\opencv.hpp" using namespace std; using namespace cv; int main() { Mat src, gray_src, line_detect; src = imread("C:/Users/he104/Desktop/opencv_images/hvtest.jpg"); if (src.empty()) { cout << "could not load the images" << endl; return -1; } namedWindow("input_images", CV_WINDOW_AUTOSIZE); namedWindow("output_images", CV_WINDOW_AUTOSIZE); cvtColor(src, gray_src, CV_BGR2GRAY); Canny(gray_src,gray_src,150,200); cvtColor(gray_src, line_detect, CV_GRAY2BGR); vector<Vec4f> plines; HoughLinesP(gray_src, plines, 1, CV_PI / 180.0, 10, 0, 10); Scalar color = Scalar(0, 255, 0); for (size_t i = 0; i < plines.size(); i++) { Vec4f hline = plines[i]; line(line_detect, Point(hline[0], hline[1]), Point(hline[2], hline[3]), color, 3, LINE_AA); } imshow("input_images", src); imshow("output_images", line_detect); waitKey(0); destroyAllWindows(); return 0; }
OpenCv学习19——霍夫变换(直线检测)
最新推荐文章于 2024-01-22 18:27:56 发布