opencv3 直线检测

代码如下:

#include  
#include

using namespace cv;
using namespace std;
int main()
{

	int b = 0, g = 0, r = 255;

	cv::Mat image = cv::imread("F:\\ebook\\opencv\\LearningOpenCV3\\test\\test\\test.png");
	
	cv::resize(image, image, cv::Size(640, 480));

	cv::namedWindow("window1");
	cv::imshow("window1", image);
	cv::waitKey(0);

	cv::Mat gray;
	cv::cvtColor(image, gray, COLOR_BGR2GRAY);

	cv::namedWindow("window1");
	cv::imshow("window1", gray);
	cv::waitKey(0);

	cv::Mat cannyimage;
	Canny(gray, cannyimage, 150, 50, 3, false);
	
	cv::namedWindow("window2");
	cv::imshow("window2", cannyimage);
	cv::waitKey(0);

	vector result;

	cv::HoughLinesP(cannyimage, result, 1, CV_PI / 180, 150, 10, 10);

	//将直线画出
	int linenum = result.size();

	for (int i = 0; i < linenum; i++)
	{
		int startx = result[i][0];
		int starty = result[i][1];
		int endx = result[i][2];
		int endy = result[i][3];

		line(image,Point(startx,starty),Point(endx,endy),Scalar(b,g,r),8,0);		
	}
	cv::namedWindow("window2");
	cv::imshow("window2", image);
	cv::waitKey(0);

	cv::destroyWindow("window1");
	cv::destroyWindow("window2");
	return 0;
}

执行结果如图:
opencv3 直线检测_第1张图片

你可能感兴趣的:(opencv开发)