美文网首页
HoughLinesP(霍夫变换直线检测)

HoughLinesP(霍夫变换直线检测)

作者: itfitness | 来源:发表于2019-08-16 10:35 被阅读0次

    概念

    霍夫变换是一种特征检测(feature extraction),被广泛应用在图像分析(image analysis)、计算机视觉(computer vision)以及数位影像处理(digital image processing)。霍夫变换是用来辨别找出物件中的特征,例如:线条。他的算法流程大致如下,给定一个物件、要辨别的形状的种类,算法会在参数空间(parameter space)中执行投票来决定物体的形状,而这是由累加空间(accumulator space)里的局部最大值(local maximum)来决定。
    现在广泛使用的霍夫变换是由RichardDuda和PeterHart在公元1972年发明,并称之为广义霍夫变换(generalizedHoughtransform),广义霍夫变换和更早前1962年的PaulHough的专利有关。经典的霍夫变换是侦测图片中的直线,之后,霍夫变换不仅能识别直线,也能够识别任何形状,常见的有圆形、椭圆形。1981年,因为DanaH.Ballard的一篇期刊论文"Generalizing the Hough transform to detect arbitrary shapes",让霍夫变换开始流行于计算机视觉界。

    效果图对比

    ●源图像



    ●处理后图像


    函数讲解

    ●函数原型
    ○c++

    void HoughLinesP( InputArray image, OutputArray lines,
                                   double rho, double theta, int threshold,
                                   double minLineLength = 0, double maxLineGap = 0 )
    

    ○Android

    void HoughLinesP(Mat image, Mat lines, double rho, double theta, int threshold, double minLineLength, double maxLineGap)
    

    ●参数解释
    ○image:输入图像:8-bit,灰度图
    ○lines:存储线段极坐标的容器,每一条线由具有四个元素的矢量(x_1,y_1, x_2, y_2) 表示,其中,(x_1, y_1)和(x_2, y_2) 是每个检测到的线段的结束点。
    ○rho:生成极坐标的像素扫描步长。
    ○theta:生成极坐标的角度步长,一般是π/180。
    ○threshold:要”检测” 一条直线所需最少的的曲线交点 。
    ○minLineLength :默认值0,表示最低线段的长度,比这个设定参数短的线段就不能被显现出来。
    ○maxLineGap :默认值0,允许将同一行点与点之间连接起来的最大的距离。

    函数使用

    ●c++中

    #include<opencv2/opencv.hpp>
    #include<iostream>
    using namespace std;
    using namespace cv;
    int main() {
        Mat src = imread("C:/Users/Administrator/Desktop/wan3.png");//引入源图像
        if (src.empty()) {
            return -1;
        }
        Mat canny;
        Canny(src,canny,100,200);//边缘提取
        imshow("canny",canny);//展示提取的边缘
        vector<Vec4f> lines;//存储直线的容器
        Mat dst = Mat(src.size(),src.type(),Scalar(255,255,255));//创建大小和类型都与源图像相同的背景为白色的图像
        HoughLinesP(canny,lines,1,CV_PI/180,3,0,8);//霍夫变换直线检测
        Scalar lineColor = Scalar(255,0,0);//画笔的颜色
        for (int i = 0; i < lines.size();i++) {
            Vec4f l = lines[i];
            line(dst,Point(l[0],l[1]),Point(l[2],l[3]),lineColor,3,LINE_AA);//在创建的白色图像上画直线
        }
        imshow("dst",dst);//展示直线图像
        waitKey(0);
        return 0;
    }
    

    ●Android中

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_relief);
    Mat src = new Mat();
    Mat canny = new Mat();
    Utils.bitmapToMat(bitmap,src);//将Bitmap对象转换为Mat对象
    Imgproc.Canny(src,canny,100,200);//边缘提取
    Mat lines = new Mat();//存储线的容器
    Imgproc.HoughLinesP(canny,lines,1,Math.PI/180,3,0,8);//霍夫直线检测
    Mat dst = new Mat(src.size(),src.type());
    for(int i = 0;i<lines.rows();i++){
         int[] line = new int[4];
         lines.get(i,0,line);//将线对应的极点坐标存到line数组中
         Imgproc.line(dst,new Point(line[0],line[1]),new Point(line[2],line[3]),new Scalar(0,0,255),3,Imgproc.LINE_AA);
    }
    Utils.matToBitmap(dst,bitmap);//将Mat对象转换为Bitmap对象
    

    相关文章

      网友评论

          本文标题:HoughLinesP(霍夫变换直线检测)

          本文链接:https://www.haomeiwen.com/subject/hwuyjctx.html