OpenCV进行Hough直线检测,cv::HoughLines()

环境

vs2019,Opencv4.0

效果

在这里插入图片描述

函数原型

在这里插入图片描述
image:输入图像,必须是单通道二进制图像,比如Canny边缘检测后的输出图像。
lines:检测到的直线,一般类型为std::vector<cv::Vec2f>
rho: 累加器的距离分辨率(以像素为单位)。
theta:以弧度为单位的累加器的角度分辨率。
threshold:累加器阈值
srn:0
stn:0
min_theta:线条的最小角度
max_theta:线条最大角度,默认为PI

示例代码


#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/core/cuda.hpp>
#include<sstream>


int main(int argc, char* argv[])
{

    cv::Mat img, img_clone, img_g, img_c;
    img = cv::imread("E:\\Comm\\demo\\22.jpeg", 1);
    img_clone = img.clone();
    cv::cvtColor(img, img_g, cv::COLOR_BGR2GRAY); //灰度图像
    cv::Canny(img_g, img_c, 100, 300, 3, false);

  
    cv::namedWindow("example", 1); //创建一个窗口用于显示图像,1代表窗口适应图像的分辨率进行拉伸。
    cv::namedWindow("hough", 1); //创建一个窗口用于显示图像,1代表窗口适应图像的分辨率进行拉伸。
    cv::namedWindow("houghP", 1); //创建一个窗口用于显示图像,1代表窗口适应图像的分辨率进行拉伸。

    std::vector<cv::Vec2f> lines; //储存霍夫变换的线
    std::vector<cv::Vec4i> linesP; //渐进概率霍夫变换

    bool hough_mode = true;
    bool houghp_mode = true;
    while (true)
    {
        if (hough_mode == true)
        {
            //霍夫变换检测直线
            cv::HoughLines(img_c, lines, 1, 0.3*CV_PI / 180, 230, 0, 0);

            //画出直线
            for (size_t i = 0; i < lines.size(); ++i)
            {
                float rho = lines[i][0], theta = lines[i][1];
                cv::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));
                cv::line(img, pt1, pt2, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
            }

            if (img.empty() == false) //图像不为空则显示图像
            {
                cv::imshow("example", img_c);
                cv::imshow("hough", img);
            }
        }

        if (houghp_mode == true)
        {
            //渐进概率霍夫变换
            cv::HoughLinesP(img_c, linesP, 3, 0.3*CV_PI / 180, 230, 20, 10);
            
            for (size_t i = 0; i < linesP.size(); i++)
            {
                //取出检测到的线段,从起点到终点(x0, y0, x1, y1)
                cv::Vec4i l = linesP[i]; 
                //画出线段
                cv::line(img_clone, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
            }

            if (img_clone.empty() == false) //图像不为空则显示图像
            {
                if(hough_mode == false) cv::imshow("example", img_c);
                cv::imshow("houghP", img_clone);
            }
        }

       

        
        int  key = cv::waitKey(30); //等待30ms
        if (key ==  int('q')) //按下q退出
        {
            break;
        }

    }
    cv::destroyAllWindows(); //关闭所有窗口
        
    return 0;

}

两种检测方法,第二种检测方法的速度更快,但是会有一定的精度损失。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用OpenCV-Python进行直线检测时,通常会使用Hough直线变换算法。Hough直线变换算法可以检测出图像中所有的直线,不过有时候需要筛选出需要的直线。 以下是使用OpenCV-Python进行直线检测的基本步骤: 1. 读取图像,将其转换为灰度图像。 ```python import cv2 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ``` 2. 对图像进行边缘检测,例如使用Canny算子。 ```python edges = cv2.Canny(gray,50,150,apertureSize = 3) ``` 3. 进行Hough直线变换,检测出所有直线。 ```python lines = cv2.HoughLines(edges,1,np.pi/180,200) ``` 4. 可以根据需要筛选出需要的直线,并将其绘制在原图像上。 ```python for line in lines: rho,theta = line[0] if theta > np.pi/180*30 and theta < np.pi/180*150: a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) ``` 在以上代码中,`lines`是通过Hough直线变换得到的所有直线。对于每一条直线,可以通过其极坐标表示方式中的角度`theta`来判断其是否为需要的直线。例如,可以筛选出与水平方向夹角在30度到150度之间的直线。 对于需要绘制的直线,可以通过其极坐标表示方式计算出其两个端点的坐标,然后使用`cv2.line()`函数在原图像上绘制出来。 以上是使用OpenCV-Python进行直线检测的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值