码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Hough Line Transform

时间:2014-11-18 23:57:43      阅读:580      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   color   os   sp   for   strong   

霍夫直线变换 —— 用于检测图像中的直线

利用图像空间和Hough参数空间的点——直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线

Standard and Probabilistic Hough Line Transform

OpenCV implements two kind of Hough Line Transforms:

  1. The Standard Hough Transform
  • It consists in pretty much what we just explained in the previous section. It gives you as result a vector of couples bubuko.com,布布扣
  • In OpenCV it is implemented with the function HoughLines
  1. The Probabilistic Hough Line Transform
  • A more efficient implementation of the Hough Line Transform. It gives as output the extremes of the detected lines bubuko.com,布布扣
  • In OpenCV it is implemented with the function HoughLinesP

 

 

vector<Vec2f> lines;
HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );

with the following arguments:

  • dst: Output of the edge detector. It should be a grayscale image (although in fact it is a binary one)
  • lines: A vector that will store the parameters bubuko.com,布布扣 of the detected lines
  • rho : The resolution of the parameter bubuko.com,布布扣 in pixels. We use 1 pixel.
  • theta: The resolution of the parameter bubuko.com,布布扣 in radians. We use 1 degree (CV_PI/180)
  • threshold: The minimum number of intersections to “detect” a line
  • srn and stn: Default parameters to zero. Check OpenCV reference for more info.

标准霍夫变换

 

vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );

with the arguments:

  • dst: Output of the edge detector. It should be a grayscale image (although in fact it is a binary one)
  • lines: A vector that will store the parameters bubuko.com,布布扣 of the detected lines
  • rho : The resolution of the parameter bubuko.com,布布扣 in pixels. We use 1 pixel.
  • theta: The resolution of the parameter bubuko.com,布布扣 in radians. We use 1 degree (CV_PI/180)
  • threshold: The minimum number of intersections to “detect” a line
  • minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
  • maxLineGap: The maximum gap between two points to be considered in the same line.

 

概率霍夫变换

 

Code

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

void help()
{
	cout << "\nThis program demonstrates line finding with the Hough transform.\n"
		"Usage:\n"
		"./houghlines <image_name>, Default is pic1.jpg\n" << endl;
}

int main(int argc, char** argv)
{
	const char* filename = "xingxing.jpg";

	Mat src = imread(filename, 0);
	if(src.empty())
	{
		help();
		cout << "can not open " << filename << endl;
		return -1;
	}

	Mat dst, cdst;
	Canny(src, dst, 50, 200, 3);
	cvtColor(dst, cdst, CV_GRAY2BGR);

#if 0
	vector<Vec2f> lines;
	HoughLines(dst, lines, 1, CV_PI/180, 100, 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, CV_AA);
	}
#else
	vector<Vec4i> lines;
	HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
	for( size_t i = 0; i < lines.size(); i++ )
	{
		Vec4i l = lines[i];
		line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
	}
#endif
	imshow("source", src);
	imshow("detected lines", cdst);

	waitKey();

	return 0;
}

OpenCV Tutorials —— Hough Line Transform

标签:style   http   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/sprint1989/p/4106786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!