gpt4 book ai didi

python - 根据像素颜色的变化找到坐标

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

我正在尝试在图像中定位特定的坐标。我有一个仅包含两种颜色的图像,如图所示,粉红色和黑色。如果我知道粉红色区域中的(x,y)坐标(在中心用黄点标记),如何找到在粉红色区域边界中的坐标(如边界处的黄点所示)。
enter image description here
注意:黄点不是图像的一部分,我只是用来表示感兴趣的区域。
除了嵌套for循环外,我只想知道是否有任何快速,更好的方法来完成此操作,因为这可能会减慢该过程,因为我必须在图像的多个区域中找到边界坐标。
谢谢!

最佳答案

这是使用Python / OpenCV和Numpy的一种方法。

  • 读取输入的
  • 转换为灰色
  • Otsu阈值
  • 裁剪包含中心
  • 的行
  • 获取该行中所有白色
  • 的坐标
  • 打印第一个和最后一个坐标
  • 在输入
  • 上画线
  • 保存结果

  • 输入:
    enter image description here
    import cv2
    import numpy as np

    # load image
    img = cv2.imread("pink_blob.png")
    hh, ww = img.shape[:2]

    # convert to gray
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # threshold
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

    # center coords
    center = (115,82)
    cy = center[1]

    # crop row at y center
    row = thresh[cy:cy+1, 0:ww]

    # get coordinates along row where it is white
    # swap x and y between numpy and opencv coords
    coords = np.argwhere(row==255)
    num_coords = len(coords)
    start = coords[0]
    end = coords[num_coords-1]
    start_pt = (start[1],cy)
    end_pt = (end[1],cy)

    print(start_pt)
    print(end_pt)

    # draw line from start to end coordinates on input
    result = img.copy()
    cv2.line(result, start_pt, end_pt, (255,255,255), 1)

    # save result
    cv2.imwrite('pink_blob_line.png', result)

    # show result
    cv2.imshow('result', result)
    cv2.waitKey(0)

    Start and End Coordinates:

    (67, 82)
    (160, 82)

    输入图像上的行:
    enter image description here

    关于python - 根据像素颜色的变化找到坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62980197/

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