gpt4 book ai didi

python-3.x - 计算特定暗区的平均宽度

转载 作者:行者123 更新时间:2023-12-02 17:17:36 25 4
gpt4 key购买 nike

我正在处理与以下图像相似的大部分图像。我希望能够计算黑线(图像中大致为黄线)的平均宽度,但是不确定如何在python中最好地做到这一点。黄线本身不出现在图像上,仅出现黑带。
enter image description here
原始图像:
enter image description here

最佳答案

第一种方法是使用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)
  • enter image description here

  • 检测行

  • 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)

  • 结果
  • enter image description here

  • Distance between (46.98, 884.00) - (50.37, 905.10) = 21.37 pixel


  • 更新

    如果我们将 image-segmentation应用于以下区域:
    enter image description here
  • 我们想使用公式找到红线的长度并找到红线之间的距离。

  • 上层:
    enter image description here
    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)
    下层:
    enter image description here
    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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com