<p>这是我的管道,也许它能给你一些帮助。</p>
<p><strong>首先,获取灰度图像并处理高斯模糊。</strong></p>
<pre><code>img = cv2.imread('src.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
</code></pre>
<p><strong>其次,使用Canny进行边缘检测。</strong></p>
<pre><code>low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
</code></pre>
<p><strong>然后,使用HoughLinesP获取行。</strong>您可以调整参数以获得更好的性能。</p>
<pre><code>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)
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
</code></pre>
<p><strong>最后,在srcImage上画线。</strong></p>
<pre><code># Draw the lines on the image
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
</code></pre>
<p>这是我最后的表演。</p>
<p>最终图像:</p>
<p><a href="https://i.stack.imgur.com/KieFL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KieFL.png" alt="enter image description here"/></a></p>