我使用Hough Lines对此图像进行角点检测。我计划找到线的交叉点作为角落。 这是图像。
不幸的是,Hough为我期望的每一行返回了很多行
如何调整霍夫线以使每条线只有四条线对应图像上的实际线?
答案 0 :(得分:9)
OpenCVs霍夫变换确实可以使用更好的非最大抑制。没有它,你会得到重复线条的这种现象。不幸的是,除了重新实现你自己的霍夫变换之外,我知道没有简单的方法来调整它。 (这是一个有效的选项。霍夫变换相当简单)
幸运的是,在后期处理中很容易解决:
对于非概率性霍夫变换,OpenCv将按其置信度顺序返回行,最强行首先。因此,只需采用rho或theta中强烈不同的前四行。
答案 1 :(得分:6)
我实现了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
编辑:代码已更新,以反映有关负rho值的评论
答案 2 :(得分:2)
收集所有行的交集
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);
}
}
}
你可以谷歌算法找到两条线的交集。 收集所有交叉点后,您可以轻松确定最小最大值,它将为您提供左上角和右下角点。从这两点可以轻松获得矩形。
此处Sorting 2d point array to find out four corners&amp; http://opencv-code.com/tutorials/automatic-perspective-correction-for-quadrilateral-objects/请参阅这两个链接。
答案 3 :(得分:2)
这是使用OpenCV 2.4用python 2.7.x编写的完整解决方案。 它基于该主题的想法。
方法:检测所有行。假设Hough函数首先返回排名最高的行。过滤线以使那些线以最小的距离和/或角度分开。
所有霍夫线的图像: https://i.ibb.co/t3JFncJ/all-lines.jpg
已过滤的行: https://i.ibb.co/yQLNxXT/filtered-lines.jpg
代码: http://codepad.org/J57oVIzs
"""
Detect the best 4 lines for a rounded rectangle.
"""
import numpy as np
import cv2
input_image = cv2.imread("image.jpg")
def drawLines(img, lines):
"""
Draw lines on an image
"""
for line in lines:
for rho,theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 1)
input_image_grey = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
edged = input_image_grey
rho = 1 # 1 pixel
theta = 1.0*0.017 # 1 degree
threshold = 100
lines = cv2.HoughLines(edged, rho, theta, threshold)
# Fix negative angles
num_lines = lines.shape[1]
for i in range(0, num_lines):
line = lines[0,i,:]
rho = line[0]
theta = line[1]
if rho < 0:
rho *= -1.0
theta -= np.pi
line[0] = rho
line[1] = theta
# Draw all Hough lines in red
img_with_all_lines = np.copy(input_image)
drawLines(img_with_all_lines, lines)
cv2.imshow("Hough lines", img_with_all_lines)
cv2.waitKey()
cv2.imwrite("all_lines.jpg", img_with_all_lines)
# Find 4 lines with unique rho & theta:
num_lines_to_find = 4
filtered_lines = np.zeros([1, num_lines_to_find, 2])
if lines.shape[1] < num_lines_to_find:
print("ERROR: Not enough lines detected!")
# Save the first line
filtered_lines[0,0,:] = lines[0,0,:]
print("Line 1: rho = %.1f theta = %.3f" % (filtered_lines[0,0,0], filtered_lines[0,0,1]))
idx = 1 # Index to store the next unique line
# Initialize all rows the same
for i in range(1,num_lines_to_find):
filtered_lines[0,i,:] = filtered_lines[0,0,:]
# Filter the lines
num_lines = lines.shape[1]
for i in range(0, num_lines):
line = lines[0,i,:]
rho = line[0]
theta = line[1]
# For this line, check which of the existing 4 it is similar to.
closeness_rho = np.isclose(rho, filtered_lines[0,:,0], atol = 10.0) # 10 pixels
closeness_theta = np.isclose(theta, filtered_lines[0,:,1], atol = np.pi/36.0) # 10 degrees
similar_rho = np.any(closeness_rho)
similar_theta = np.any(closeness_theta)
similar = (similar_rho and similar_theta)
if not similar:
print("Found a unique line: %d rho = %.1f theta = %.3f" % (i, rho, theta))
filtered_lines[0,idx,:] = lines[0,i,:]
idx += 1
if idx >= num_lines_to_find:
print("Found %d unique lines!" % (num_lines_to_find))
break
# Draw filtered lines
img_with_filtered_lines = np.copy(input_image)
drawLines(img_with_filtered_lines, filtered_lines)
cv2.imshow("Filtered lines", img_with_filtered_lines)
cv2.waitKey()
cv2.imwrite("filtered_lines.jpg", img_with_filtered_lines)
答案 4 :(得分:0)
上述方法(由@ HugoRune提出并由@ Onamission21实施)是正确的,但有一点错误。 cv2.HoughLines
可能会将负rho和theta返回到pi。例如,注意线(r0,0)非常接近线(-r0,pi-epsilon),但在上面的接近度测试中找不到它们。
我只是在亲密度计算之前应用rho*=-1, theta-=pi
来处理负rhos。