如何使用opencv Hough line算法得到线的交点?
这是我的代码:
import cv2
import numpy as np
import imutils
im = cv2.imread('../data/test1.jpg')
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 60, 150, apertureSize=3)
img = im.copy()
lines = cv2.HoughLines(edges,1,np.pi/180,200)
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 + 3000*(-b))
y1 = int(y0 + 3000*(a))
x2 = int(x0 - 3000*(-b))
y2 = int(y0 - 3000*(a))
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),10)
cv2.imshow('houghlines',imutils.resize(img, height=650))
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
我希望获得所有的交叉点。
答案 0 :(得分:28)
你不想得到平行线的交叉点;只有垂直线与水平线的交点。此外,由于您有垂直线,计算斜率可能会导致爆炸或inf斜率,因此您不应使用y = mx+b
方程。你需要做两件事:
使用HoughLines
,您已将结果设为rho, theta
,因此您可以轻松地使用theta
细分为两类角度。您可以使用例如cv2.kmeans()
theta
作为您要拆分的数据。
然后,要计算交叉点,可以使用calculating intersections given two points from each line的公式。您已经计算了每一行中的两个点:(x1, y1), (x2, y2)
因此您只需存储它们并使用它们即可。编辑:实际上,如下面的代码所示,您可以使用一个公式来计算rho, theta
形式HoughLines
给出的行的交叉点。
我之前已经回答了a similar question一些你可以查看的python代码;请注意,这是使用HoughLinesP
,它只为您提供线段。
您没有提供原始图片,因此无法使用。相反,我将使用OpenCV在其Hough变换和阈值教程中使用的标准数独图像:
首先,我们只是阅读此图片并使用自适应阈值进行二值化,就像this OpenCV tutorial中使用的那样:
import cv2
import numpy as np
img = cv2.imread('sudoku.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 5)
adapt_type = cv2.ADAPTIVE_THRESH_GAUSSIAN_C
thresh_type = cv2.THRESH_BINARY_INV
bin_img = cv2.adaptiveThreshold(blur, 255, adapt_type, thresh_type, 11, 2)
然后我们会找到带有cv2.HoughLines()
rho, theta, thresh = 2, np.pi/180, 400
lines = cv2.HoughLines(bin_img, rho, theta, thresh)
现在,如果我们想找到交叉点,我们真的想找到垂直线的交点。我们不希望大多数平行线的交叉点。所以我们需要分割我们的线。在这个特定的例子中,您可以根据简单的测试轻松检查线条是水平线还是垂直线;垂直线的theta
大约为0或大约180;水平线的theta
大约为90.但是,如果你想根据任意数量的角度对它们进行分割,而不是你定义这些角度,我认为最好的想法是使用{{1 }}。
有一个棘手的事情要做对。 cv2.kmeans()
以[{1}}形式(Hesse normal form)返回行,返回的HoughLines
介于0和180度之间,180和0度左右的行相似(它们都是接近水平线),所以我们需要一些方法来在rho, theta
中获得这种周期性。
如果我们在单位圆上绘制角度,但是将角度乘以 2 ,那么最初大约180度的角度将接近360度,因此将具有theta
个值在单位圆圈附近相同的角度为0.所以我们可以得到一些不错的"亲密度"这里用kmeans
绘制单位圆上的坐标。然后我们可以在这些点上运行x, y
,并使用我们想要的多个部分自动分段。
因此,让我们构建一个功能来进行细分:
2*angle
现在使用它,我们可以简单地调用:
cv2.kmeans()
这里有什么好处我们可以通过指定可选参数from collections import defaultdict
def segment_by_angle_kmeans(lines, k=2, **kwargs):
"""Groups lines based on angle with k-means.
Uses k-means on the coordinates of the angle on the unit circle
to segment `k` angles inside `lines`.
"""
# Define criteria = (type, max_iter, epsilon)
default_criteria_type = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER
criteria = kwargs.get('criteria', (default_criteria_type, 10, 1.0))
flags = kwargs.get('flags', cv2.KMEANS_RANDOM_CENTERS)
attempts = kwargs.get('attempts', 10)
# returns angles in [0, pi] in radians
angles = np.array([line[0][1] for line in lines])
# multiply the angles by two and find coordinates of that angle
pts = np.array([[np.cos(2*angle), np.sin(2*angle)]
for angle in angles], dtype=np.float32)
# run kmeans on the coords
labels, centers = cv2.kmeans(pts, k, None, criteria, attempts, flags)[1:]
labels = labels.reshape(-1) # transpose to row vec
# segment lines based on their kmeans label
segmented = defaultdict(list)
for i, line in zip(range(len(lines)), lines):
segmented[labels[i]].append(line)
segmented = list(segmented.values())
return segmented
(默认情况下为segmented = segment_by_angle_kmeans(lines)
来指定任意数量的组,因此我没有在此指定)。
如果我们用不同颜色绘制每组的线条:
现在剩下的就是找到第一组中每一条线与第二组中每条线的交点的交点。由于线条是Hesse法线形式,因此有一个很好的线性代数公式,用于计算此形式的线条交点。见here。让我们在这里创建两个函数;一个找到两条线的交集,一个函数循环遍历组中的所有行,并将这个更简单的函数用于两行:
k
然后使用它,它只是:
k = 2
绘制所有交叉点,我们得到:
如上所述,此代码也可以将线段分成两组以上的角度。这是在手绘三角形上运行,并使用def intersection(line1, line2):
"""Finds the intersection of two lines given in Hesse normal form.
Returns closest integer pixel locations.
See https://stackoverflow.com/a/383527/5087436
"""
rho1, theta1 = line1[0]
rho2, theta2 = line2[0]
A = np.array([
[np.cos(theta1), np.sin(theta1)],
[np.cos(theta2), np.sin(theta2)]
])
b = np.array([[rho1], [rho2]])
x0, y0 = np.linalg.solve(A, b)
x0, y0 = int(np.round(x0)), int(np.round(y0))
return [[x0, y0]]
def segmented_intersections(lines):
"""Finds the intersections between groups of lines."""
intersections = []
for i, group in enumerate(lines[:-1]):
for next_group in lines[i+1:]:
for line1 in group:
for line2 in next_group:
intersections.append(intersection(line1, line2))
return intersections
计算检测到的线的交点:
答案 1 :(得分:4)
如果您已经有线段,只需将它们替换为线方程......
x = x1 + u * (x2-x1)
y = y1 + u * (y2-y1)
你可以通过使用以下任何一个来找到......
u = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1))
u = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1))
答案 2 :(得分:1)
首先,您需要细化Hough变换的输出(我通常通过基于某些标准的k均值聚类来实现这一点,例如,段的斜率和/或质心)。例如,在您的问题中,似乎所有线的斜率通常都在0度,180度,90度附近,因此您可以在此基础上进行聚类。
接下来,有两种不同的方法可以获得交叉点(技术上相同):
答案 3 :(得分:0)
我在这里用一些方法处理了我的图片;
1。灰度
2。无论是按位转换还是边缘检测,都取决于我猜的图像,这里我进行了按位转换。 首先将所有检测到的行放入列表中。
listOflines = cv2.HoughLines(mask_inv,1,np.pi/180,200)
我们将获得'rho'和'theta'的值, 我在这里要做的是为垂直线创建两个空列表,为水平线创建一个空列表,并将这两行的值附加到相应的列表中。
rowsValue = []
columnValue = []
这是垂直和水平线的逻辑。
for line in listOflines:
if line[0][1] == 0:
columnValue.append(line[0][0])
else:
rowsValue.append(line[0][0])
现在重要的部分在这里, 当每条线穿过并彼此相交时,它将在特定像素值上相交。 而且我们用“ rho”来表示像素值。
现在,让创建元组作为坐标传递到“ cv2”函数中,即以(x,y)的形式传递。
tupsList = [(r,c) for r in rowsValue for c in columnValue]
for tups in tupsList:
cv2.circle(image, tups, 1,(0,0,255), 2)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
就这样! 现在在此之前和之后的图像。
答案 4 :(得分:0)
这是使用OpenCV 2.4用python 2.7.x编写的完整解决方案。
它在此线程中使用alkasm
中的解决方案,但不完整。 HoughLines()和kmeans()的返回值也从OpenCV 2.x更改为3.x
结果1:桌上的一张纸
https://i.ibb.co/VBSY7V7/paper-on-desk-intersection-points.jpg
这回答了原始问题,但是使用k = 2,3,4的k均值聚类不会分割纸。您需要使用其他方法来查找纸张的角落
例如过滤平行线。
结果2:数独网格
https://i.ibb.co/b6thfgr/sudoku-intersection-points.jpg
代码: https://pastiebin.com/5f36425b7ae3d
"""
Find the intersection points of lines.
"""
import numpy as np
import cv2
from collections import defaultdict
import sys
img = cv2.imread("paper_on_desk.jpg")
#img = cv2.imread("sudoku.jpg")
def segment_by_angle_kmeans(lines, k=2, **kwargs):
"""
Group lines by their angle using k-means clustering.
Code from here:
https://stackoverflow.com/a/46572063/1755401
"""
# Define criteria = (type, max_iter, epsilon)
default_criteria_type = cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER
criteria = kwargs.get('criteria', (default_criteria_type, 10, 1.0))
flags = kwargs.get('flags', cv2.KMEANS_RANDOM_CENTERS)
attempts = kwargs.get('attempts', 10)
# Get angles in [0, pi] radians
angles = np.array([line[0][1] for line in lines])
# Multiply the angles by two and find coordinates of that angle on the Unit Circle
pts = np.array([[np.cos(2*angle), np.sin(2*angle)] for angle in angles], dtype=np.float32)
# Run k-means
if sys.version_info[0] == 2:
# python 2.x
ret, labels, centers = cv2.kmeans(pts, k, criteria, attempts, flags)
else:
# python 3.x, syntax has changed.
labels, centers = cv2.kmeans(pts, k, None, criteria, attempts, flags)[1:]
labels = labels.reshape(-1) # Transpose to row vector
# Segment lines based on their label of 0 or 1
segmented = defaultdict(list)
for i, line in zip(range(len(lines)), lines):
segmented[labels[i]].append(line)
segmented = list(segmented.values())
print("Segmented lines into two groups: %d, %d" % (len(segmented[0]), len(segmented[1])))
return segmented
def intersection(line1, line2):
"""
Find the intersection of two lines
specified in Hesse normal form.
Returns closest integer pixel locations.
See here:
https://stackoverflow.com/a/383527/5087436
"""
rho1, theta1 = line1[0]
rho2, theta2 = line2[0]
A = np.array([[np.cos(theta1), np.sin(theta1)],
[np.cos(theta2), np.sin(theta2)]])
b = np.array([[rho1], [rho2]])
x0, y0 = np.linalg.solve(A, b)
x0, y0 = int(np.round(x0)), int(np.round(y0))
return [[x0, y0]]
def segmented_intersections(lines):
"""
Find the intersection between groups of lines.
"""
intersections = []
for i, group in enumerate(lines[:-1]):
for next_group in lines[i+1:]:
for line1 in group:
for line2 in next_group:
intersections.append(intersection(line1, line2))
return intersections
def drawLines(img, lines, color=(0,0,255)):
"""
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), color, 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 5)
# Make binary image
adapt_type = cv2.ADAPTIVE_THRESH_GAUSSIAN_C
thresh_type = cv2.THRESH_BINARY_INV
bin_img = cv2.adaptiveThreshold(blur, 255, adapt_type, thresh_type, 11, 2)
cv2.imshow("binary", bin_img)
cv2.waitKey()
# Detect lines
rho = 2
theta = np.pi/180
thresh = 350
lines = cv2.HoughLines(bin_img, rho, theta, thresh)
if sys.version_info[0] == 2:
# python 2.x
# Re-shape from 1xNx2 to Nx1x2
temp_lines = []
N = lines.shape[1]
for i in range(N):
rho = lines[0,i,0]
theta = lines[0,i,1]
temp_lines.append( np.array([[rho,theta]]) )
lines = temp_lines
print("Found lines: %d" % (len(lines)))
# Draw all Hough lines in red
img_with_all_lines = np.copy(img)
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)
# Cluster line angles into 2 groups (vertical and horizontal)
segmented = segment_by_angle_kmeans(lines, 2)
# Find the intersections of each vertical line with each horizontal line
intersections = segmented_intersections(segmented)
img_with_segmented_lines = np.copy(img)
# Draw vertical lines in green
vertical_lines = segmented[1]
img_with_vertical_lines = np.copy(img)
drawLines(img_with_segmented_lines, vertical_lines, (0,255,0))
# Draw horizontal lines in yellow
horizontal_lines = segmented[0]
img_with_horizontal_lines = np.copy(img)
drawLines(img_with_segmented_lines, horizontal_lines, (0,255,255))
# Draw intersection points in magenta
for point in intersections:
pt = (point[0][0], point[0][1])
length = 5
cv2.line(img_with_segmented_lines, (pt[0], pt[1]-length), (pt[0], pt[1]+length), (255, 0, 255), 1) # vertical line
cv2.line(img_with_segmented_lines, (pt[0]-length, pt[1]), (pt[0]+length, pt[1]), (255, 0, 255), 1)
cv2.imshow("Segmented lines", img_with_segmented_lines)
cv2.waitKey()
cv2.imwrite("intersection_points.jpg", img_with_segmented_lines)