C++ 霍夫变换直线检测代码示例

利用霍夫变换进行直线检测,代码经过亲自测试,示例代码如下:

img = imread("test.jpg")
Mat gray, canny;
cvtColor(img, gray, CV_RGB2GRAY);
Canny(gray, canny, 50, 120);

vector<Vec4i> lines;
HoughLinesP(canny, lines, 1, CV_PI / 180, 160, 20, 20);

for (size_t i = 0; i < lines.size(); i++)
{
	Vec4i I = lines[i];
	double x1 = I[0];
	double y1 = I[1];
	double x2 = I[2];
	double y2 = I[3];
	//筛选满足条件的点
	if (abs(x1 - x2) + abs(y1 - y2) > 50)
	{
		# 将满足条件的点画出
		line(img, Point2d(x1, y1), Point2d(x2, y2), Scalar(0, 255, 255), 2);
	}
}

你可能感兴趣的:(图像处理)