<p>一种方法是使用概率Hough线方法检测线</p>
<p>结果将是:</p>
<p><a href="https://i.stack.imgur.com/ulR3K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ulR3K.png" alt="enter image description here"/></a></p>
<p>现在问题是检测到多条线。我们知道他们的坐标。如果我们将它们全部打印出来:</p>
<pre><code>Pixel Length: 296
Pixel Length: 197
Pixel Length: 308
Pixel Length: 175
Pixel Length: 292
Pixel Length: 229
Pixel Length: 103
Pixel Length: 109
</code></pre>
<p>由于检测到的长度很多,可能找到其平均值是有意义的:</p>
<pre><code>Average Pixel Length: 109 pixel
</code></pre>
<p>虽然我不知道如何将109像素转换为100nm</p>
<pre><code>import cv2
import numpy as np
img = cv2.imread("4HQtp.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
base = cv2.HoughLinesP(edges, 1, np.pi / 180, 80, minLineLength=1, maxLineGap=6)
pixel_array = []
pixel_length = 0
if base is not None:
for line in base:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
pixel_length = np.abs(x2 - x1)
pixel_array.append(pixel_length)
print("Pixel Length: {}".format(pixel_length))
cv2.imshow("img", img)
cv2.imwrite("hough_img.png", img)
cv2.waitKey(0)
print("Average Pixel Length: {:.0f} pixel".format(np.average(pixel_array)))
</code></pre>