API

Probabilistic Hough Line变换(概率霍夫线变换)

vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );

dst:边缘检测器的输出。它应该是一个灰度图像(尽管事实上它是二进制的)
行:将存储检测到的行的参数的(xstart,ystart,xend,yend)
rho:参数的分辨率(以像素为单位)。我们使用1像素。r
theta:以弧度表示的参数的分辨率。我们使用1度(CV_PI / 180)θ
threshold:将“* detect *”一行的最小交点数
minLinLength:可以形成一行的最小点数。小于这个点数的行被忽略。
maxLineGap:在同一行中考虑的两点之间的最大差距。

​

代码

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include<vector>
#include <iostream>
using namespace std;
using namespace cv;
Mat src = imread("3.png",IMREAD_GRAYSCALE);
int thresh = 50;
int line_length = 10;
int line_distance = 5;

void on_canny(int, void*)
{
  Mat dst;
  //去模糊
  GaussianBlur(src, src, Size(3, 3), 0, 0);
  Canny(src, dst, thresh, 2*thresh, 3);
  vector<Vec4i>lines;
  HoughLinesP(dst, lines, 1, CV_PI / 180, 50, line_length, line_distance);
  for (int i = 0; i < lines.size(); i++)
  {
      Vec4i l = lines[i];
      line(dst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 1, 8, 0);
  }
  imshow("dst", dst);
}

int main() 
{
    
    if (!src.data)
    {
        cout << "图片读入失败" << endl;
        return -1;
    }
    namedWindow("原图", WINDOW_FREERATIO);
    imshow("原图",src);
    createTrackbar("阈值", "原图",&thresh, 120, on_canny);
    createTrackbar("规定线长", "原图", &line_length, 60, on_canny);
    createTrackbar("规定两线距离", "原图", &line_distance, 15, on_canny);
    on_canny(0, 0);
    waitKey(0);
    return 0;
}

 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐