opencv检测虚线 [英] opencv detect dotted lines
问题描述
我有一张由虚线组成的图像:
I have an image which consists of somehow dotted lines:
注意:打开图像,将其包裹起来并查看所有小点
如何使用openCV检测和参数化这些行?
此图像是机器人上激光测距仪的值,我需要尽可能获得所有线条.
This image are the values of a laser range scanner on a robot and I need to get all the lines as good a possible.
HoughLinesP
功能是否应该对此理想?
The HoughLinesP
function should be ideal for this?
我尝试了以下代码:
//converts laser range readings to a binary image.
cv::Mat binaryImg = laserRangesToBinaryImage();
cv::Mat cdst;
cvtColor(binaryImg, cdst, CV_GRAY2BGR);
std::vector<cv::Vec4i> lines;
HoughLinesP(binaryImg, lines, 2, 5.0*CV_PI/180.0, 1, 2, 20 );
for( size_t i = 0; i < lines.size(); i++ )
{
cv::Vec4i l = lines[i];
line( cdst, cv::Point(l[0], l[1]), cv::Point(l[2], l[3]), cv::Scalar(0,0,255), 3, CV_AA);
}
cv::imshow("Hough output", cdst);
大约产生50至60行(在Ubuntu 14.04上使用openCV 2.8.3):
which results in about 50 to 60 lines (using openCV 2.8.3 on Ubuntu 14.04):
此处最大的问题是,存在多个分隔的线段,应在其中检测整条线.因此,这些网段未正确连接.有些行太短甚至没有被检测到.
The biggest problem here is that there are multiple separated line segments where a full line should be detected. So the segments aren't correctly connected. Some of the lines are too short or not even detected.
最佳结果应如下所示(手动创建),其中包含约20个线段:
The optimal result should look like this (manually created) with about 20 line segments:
如何获得此结果?
推荐答案
If you didn't already, have a look at this tutorial.
Basically, you should act on these three parameters (the last three parameters of the HoughLinesP function), until you reach the right length for the detected lines:
- 阈值:的最小交集(在霍夫空间)检测"一行.
- minLinLength :可以形成一条线的最小点数. 少于此点数的线将被忽略.
- maxLineGap::要考虑的两点之间的最大差距 同一行.
- threshold: The minimum number of intersections (in the Hough space) to "detect" a line.
- minLinLength: The minimum number of points that can form a line. Lines with less than this number of points are disregarded.
- maxLineGap: The maximum gap between two points to be considered in the same line.
数学形态学(结束)也可能会有所帮助,正如在评论.但是,帮助将是有限的:建议将小型3x3内核用于此类任务,因此,如果一行上的某些像素彼此之间相距太远,则无论如何都不会填满间隙.
Mathematical morphology (closing) could help too, as it was mentionned in the comments. However, the help will be limited: a small 3x3 kernel is recommended for such a task, so if some pixels on a line are too far from one another the gap won't be filled up anyway.
这篇关于opencv检测虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!