opencv 直线检测之HoughLinesP

在这里插入图片描述在这里插入图片描述

#include <QCoreApplication>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;
//直线检测
int main()
{
    src=imread("../image/8.jpg");
    if(src.empty())
    {
        printf("could not load image ...");
        return ;
    }
    imshow("src",src);
   cvtColor(src,src,CV_BGR2GRAY);
    //裁剪图像
   // Rect rect=Rect(10,10,src.cols-20,src.rows-20);
  //  src=src(rect);


    //binary image
    Mat binary,morhpImg;
    //THRESH_BINARY_INV 返色
    //二值化
    threshold(src,binary,0,255,THRESH_BINARY_INV|THRESH_OTSU);

    //morhpology形态学开操作
    Mat kernel=getStructuringElement(MORPH_RECT,Size(20,1),Point(-1,-1));
    morphologyEx(binary,morhpImg,MORPH_OPEN,kernel,Point(-1,-1));

    imshow("output",morhpImg);
    //膨胀操作
    kernel=getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
    dilate(morhpImg,morhpImg,kernel);
    imshow("output",morhpImg);

    vector<Vec4i> lines;
    
    HoughLinesP(morhpImg,lines,1,CV_PI/180.0,30,20.0,0);
    Mat result=src.clone();
    cvtColor(result,result,CV_GRAY2BGR);
    for(int i=0;i<lines.size();i++)
    {
        Vec4i lin=lines[i];

        line(result,Point(lin[0],lin[1]),Point(lin[2],lin[3]),Scalar(0,0,255),2,8,0);
    }
imshow("output",result);

return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值