直线检测 case2

HoughLinesP参数分析

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

image为输入图像,要求是单通道,8位图像

lines为输出参数,4个元素表示,即直线的起始和终止端点的4个坐标(x1,y1),(x2,y2)

rho为距离分辨率,一般为1

heta为角度的分辨率,一般CV_PI/180

threshold为阈值,hough变换图像空间值最大点,大于则执行

minLineLength为最小直线长度(像素),即如果小于该值,则不被认为是一条直线

maxLineGap为直线间隙最大值,如果两条直线间隙大于该值,则被认为是两条线段,否则是一条。


#include <opencv2/opencv.hpp>

#include <iostream>
#include <math.h>


using namespace cv;
using namespace std;


int max_count = 255;
int threshold_value = 100;
const char* output_lines = "Hough Lines";
Mat src, roiImage, dst;
void detectLines(int, void*);
void morhpologyLines(int, void*);
int main(int argc, char** argv) {
src = imread("D:/gloomyfish/case2.png", IMREAD_GRAYSCALE);
if (src.empty()) {
printf("could not load image...\n");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
namedWindow(output_lines, CV_WINDOW_AUTOSIZE);
Rect roi = Rect(10, 10, src.cols - 20, src.rows - 20);
roiImage = src(roi);
imshow("ROI image", roiImage);
// createTrackbar("threshold:", output_lines, &threshold_value, max_count, detectLines);
// detectLines(0, 0);
morhpologyLines(0, 0);


waitKey(0);
return 0;
}


void detectLines(int, void*) {
Canny(roiImage, dst, threshold_value, threshold_value * 2, 3, false);
//threshold(roiImage, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, CV_PI / 180.0, 30, 30.0, 0);
cvtColor(dst, dst, COLOR_GRAY2BGR);
for (size_t t = 0; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(dst, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
}
imshow(output_lines, dst);
}


void morhpologyLines(int, void*) {
// binary image
Mat binaryImage, morhpImage;
threshold(roiImage, binaryImage, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
imshow("binary", binaryImage);


// morphology operation
Mat kernel = getStructuringElement(MORPH_RECT, Size(20, 1), Point(-1, -1));
morphologyEx(binaryImage, morhpImage, MORPH_OPEN, kernel, Point(-1, -1));
imshow("morphology result", morhpImage);


// dilate image
kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
dilate(morhpImage, morhpImage, kernel);
imshow("morphology lines", morhpImage);


// hough lines
vector<Vec4i> lines;
HoughLinesP(morhpImage, lines, 1, CV_PI / 180.0, 30, 20.0, 0);
Mat resultImage = roiImage.clone();
cvtColor(resultImage, resultImage, COLOR_GRAY2BGR);
for (size_t t = 0; t < lines.size(); t++) {
Vec4i ln = lines[t];
line(resultImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);
}
imshow(output_lines, resultImage);
return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值