我有大量的图像,如下所示。我正在尝试从透射电子显微镜图像中检测纳米颗粒,这是我使用OpenCV-Python成功实现的。该图像只是较大的透射电子显微镜图像的左下角的一部分。我想提取“100 nm”下白色线段的长度。目前,我正在手动测量这条线的长度,并且我想使它自动化。我认为可以使用OpenCV-Python通过轮廓绘制来完成此操作,但是我还有许多其他功能也在图像中绘制了轮廓。我是Python的新手,我不确定如何实现此功能,或者是否可以完全实现。我对代码应该做什么的想法:
任何建议,方向或“从哪里开始”评论都将受到欢迎和赞赏。
该段线在功能上非常独特,每张图像中只有一条线。
最佳答案
scale = cv2.imread('scalenanometer.png', cv2.IMREAD_COLOR)
scale_gray = cv2.cvtColor(scale, cv2.COLOR_BGR2GRAY)
# adjust the second value of the next line to tune the detection
ret, thresh = cv2.threshold(scale_gray, 210, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# filter noisy detection
contours = [c for c in contours if cv2.contourArea(c) > 100]
# sort from by (y, x)
contours.sort(key=lambda c: (cv2.boundingRect(c)[1], cv2.boundingRect(c)[0]))
# work on the segment
cv2.rectangle(scale, cv2.boundingRect(contours[-1]), (0,255,0), 2)
x,y,w,h = cv2.boundingRect(contours[-1])
print(x,y,w,h) # x,y: (39 152) w,h: [304 21]
如果要检测该值,可以使用tesseract ocr。
关于python - 测量图像中线段的长度(以像素为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63690237/