<p>这里有一些很好的答案来回答你的第一部分问题,但是对于第二部分(寻找直线交点),我没有看到太多。</p>
<p>我建议你看看<a href="https://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm" rel="noreferrer">Bentley-Ottmann</a>算法。</p>
<p>有一些算法<a href="https://github.com/splichte/lsi" rel="noreferrer">here</a>和<a href="https://github.com/ideasman42/isect_segments-bentley_ottmann" rel="noreferrer">here</a>的python实现。</p>
<p>编辑:使用VeraPoseidon的Houghlines实现和链接在这里的第二个库,我成功地获得了以下交叉点检测结果。归功于维拉和图书馆作者的出色工作。绿色方块表示检测到的交叉点。有一些错误,但这似乎是一个很好的起点对我来说。似乎大多数你实际想要检测交叉点的位置都检测到了多个交叉点,因此你可以在图像上运行一个大小合适的窗口,该窗口寻找多个交叉点,并将真正的交叉点视为激活该窗口的交叉点。</p>
<p><a href="https://i.stack.imgur.com/1Tihf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1Tihf.png" alt="Bentley-Ottmann applied to Houghlines"/></a></p>
<p>下面是我用来生成该结果的代码:</p>
<pre><code>import cv2
import numpy as np
import isect_segments_bentley_ottmann.poly_point_isect as bot
img = cv2.imread('parking.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50 # minimum number of pixels making up a line
max_line_gap = 20 # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
print(lines)
points = []
for line in lines:
for x1, y1, x2, y2 in line:
points.append(((x1 + 0.0, y1 + 0.0), (x2 + 0.0, y2 + 0.0)))
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
print(lines_edges.shape)
#cv2.imwrite('line_parking.png', lines_edges)
print points
intersections = bot.isect_segments(points)
print intersections
for inter in intersections:
a, b = inter
for i in range(3):
for j in range(3):
lines_edges[int(b) + i, int(a) + j] = [0, 255, 0]
cv2.imwrite('line_parking.png', lines_edges)
</code></pre>
<p>您可以使用类似于此代码块的方法来删除一个小区域中的多个交叉点:</p>
<pre><code>for idx, inter in enumerate(intersections):
a, b = inter
match = 0
for other_inter in intersections[idx:]:
c, d = other_inter
if abs(c-a) < 15 and abs(d-b) < 15:
match = 1
intersections[idx] = ((c+a)/2, (d+b)/2)
intersections.remove(other_inter)
if match == 0:
intersections.remove(inter)
</code></pre>
<p>输出图像:<a href="https://i.stack.imgur.com/7tuW6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7tuW6.png" alt="Cleaned Output"/></a></p>
<p>不过,你得玩窗口功能。</p>