Ill*_*lSc 7 opencv computer-vision hough-transform
我正在使用Hough Lines对此图像进行角点检测.我计划找到线的交叉点作为角落.这是图像.

不幸的是,霍夫为我期望的每一行返回了很多行

如何调整霍夫线,使每条只有四条线对应图像上的实际线?
Hug*_*une 10
OpenCVs霍夫变换确实可以使用更好的非最大抑制.没有它,你会得到重复线条的这种现象.不幸的是,除了重新实现你自己的霍夫变换之外,我知道没有简单的方法来调整它.(这是一个有效的选项.霍夫变换相当简单)
幸运的是,在后处理中很容易修复:
对于非概率性霍夫变换,OpenCv将按其置信度顺序返回行,最强行首先.因此,简单地选择rho或theta中强烈不同的前四行.
我实现了HugoRune描述的方法,虽然我会分享我的代码作为我如何实现它的一个例子.我使用了5度和10像素的容差.
strong_lines = np.zeros([4,1,2])
minLineLength = 2
maxLineGap = 10
lines = cv2.HoughLines(edged,1,np.pi/180,10, minLineLength, maxLineGap)
n2 = 0
for n1 in range(0,len(lines)):
for rho,theta in lines[n1]:
if n1 == 0:
strong_lines[n2] = lines[n1]
n2 = n2 + 1
else:
if rho < 0:
rho*=-1
theta-=np.pi
closeness_rho = np.isclose(rho,strong_lines[0:n2,0,0],atol = 10)
closeness_theta = np.isclose(theta,strong_lines[0:n2,0,1],atol = np.pi/36)
closeness = np.all([closeness_rho,closeness_theta],axis=0)
if not any(closeness) and n2 < 4:
strong_lines[n2] = lines[n1]
n2 = n2 + 1
Run Code Online (Sandbox Code Playgroud)
编辑:代码已更新,以反映有关负rho值的评论
收集所有直线的交点
for (int i = 0; i < lines.size(); i++)
{
for (int j = i + 1; j < lines.size(); j++)
{
cv::Point2f pt = computeIntersectionOfTwoLine(lines[i], lines[j]);
if (pt.x >= 0 && pt.y >= 0 && pt.x < image.cols && pt.y < image.rows)
{
corners.push_back(pt);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过谷歌搜索该算法来找到两条线的交点。一旦收集了所有交点,您就可以轻松确定最小最大点,这将为您提供左上角和右下角点。从这两点就可以轻松得到矩形了。
在这里对 2d 点数组进行排序以找出四个角 & http://opencv-code.com/tutorials/automatic-perspective- Correction-for-quadrilateral-objects/ 请参阅这两个链接。
| 归档时间: |
|
| 查看次数: |
7395 次 |
| 最近记录: |