OpenCV_(HougLines transform)霍夫变换检测直线

来源:互联网 发布:苏亚雷斯巴萨数据 编辑:程序博客网 时间:2024/06/04 18:15

2.hough transform 霍夫变换检测直线


  cv::Mat image = cv::imread("../../aTestImage/road2.jpg", 0);//Buildingcv::Mat contours;cv::Canny(image, contours, 125, 350);//主体为白色   std::vector<cv::Vec2f> lines;//二维向量组cv::HoughLines(contours, lines, 1, PI / 180, 150);//获取hough变换的直线数据存储在lines向量中std::cout << "lines count:" << lines.size() << std::endl;cv::Mat result(contours.rows, contours.cols, CV_8U, cv::Scalar(255));//和轮廓图像大小一致的图像image.copyTo(result);std::vector <cv::Vec2f>::const_iterator it = lines.begin();while (it != lines.end()){float rho = (*it)[0];//半径float theta = (*it)[1];//角度if (theta<PI / 4. || theta>3.*PI / 4.){//垂直线cv::Point  pt1(rho / cos(theta), 0);//与第一行交点cv::Point pt2((rho - result.rows*sin(theta)) / cos(theta), result.rows);//与最后一行交点cv::line(result, pt1, pt2, cv::Scalar(255), 1);}else{//水平线cv::Point  pt1(0, rho / sin(theta));//与第一列交点cv::Point  pt2(result.cols, (rho - result.cols*cos(theta)) / sin(theta));//与最后一列交点cv::line(result, pt1, pt2, cv::Scalar(255), 1);}std::cout << "lines Detail:" <<"("<<rho<<","<<theta <<")"<< std::endl;++it;}cv::namedWindow("resultL", 1);cv::imshow("resultL", result);




0 0
原创粉丝点击