作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最佳答案
第一种方法是使用line-detector
。
img = cv2.imread('calculate_width.png')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
canny_img = cv2.Canny(gray_img, threshold1=150, threshold2=200)
lines = createFastLineDetector(_length_threshold=20).detect(canny_img)
for cur in lines:
(x1, y1, x2, y2) = cur[0]
dist = math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
print("Distance between ({:.2f}, {:.2f}) - ({:.2f}, {:.2f}) = {:.2f}"
.format(x1, y1, x2, y2, dist))
cv2.line(img, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0),
thickness=2)
cv2.imshow("detected", img)
cv2.waitKey(0)
Distance between (46.98, 884.00) - (50.37, 905.10) = 21.37 pixel
image-segmentation
应用于以下区域:
Distance between (118.06, 868.42) - (96.92, 871.40)
Distance between (95.94, 872.67) - (75.85, 876.11)
Distance between (74.88, 877.33) - (24.85, 886.16)
Distance between (23.96, 887.62) - (0.01, 890.06)
下层:
Distance between (79.07, 894.60) - (99.02, 892.15)
Distance between (104.01, 886.54) - (125.99, 887.20)
Distance between (40.93, 901.45) - (66.05, 898.40)
Distance between (0.00, 906.02) - (33.99, 905.52)
如果您随机选择两个点:
(66.05, 898.40)
和
(24.85 - 886.16)
,则距离为:
41.23
解决方案不是完美的,但是它可能会给人一个更好的主意的直觉。因此,我将其发布为答案。
import cv2
import math
import numpy as np
from cv2.ximgproc import createFastLineDetector
img = cv2.imread('calculate_width.png')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray_img, 150, 255,
cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
sure_bg = cv2.dilate(opening, kernel)
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
_, sure_fg = cv2.threshold(dist_transform, 0.3*dist_transform.max(),
255, 0)
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg, sure_fg)
lines = createFastLineDetector(_length_threshold=20).detect(unknown)
for cur in lines:
(x1, y1, x2, y2) = cur[0]
dist = math.sqrt(((x2 - x1) ** 2) + ((y2 - y1) ** 2))
print("Distance between ({:.2f}, {:.2f}) - ({:.2f}, {:.2f})"
.format(x1, y1, x2, y2))
cv2.line(img, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0),
thickness=2)
cv2.imshow("detected", img)
cv2.waitKey(0)
关于python-3.x - 计算特定暗区的平均宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63963060/
我是一名优秀的程序员,十分优秀!