【问题标题】:How to find pixel location of a white blob of pixels如何找到白色像素块的像素位置
【发布时间】:2023-03-08 08:09:01
【问题描述】:

我已经设法从这张图片中检测到一个蓝色方块:

面具只有黑色和白色。我想知道白块的位置,即它的中点。

我的问题是:如何检测图片中蓝色方块的中点?

我从网上得到以下代码:

# import the necessary packages
import numpy as np
import cv2
def detectColouredObject(FILENAME):
    # load the image
    image = cv2.imread(FILENAME)

    # THE COLOURS ARE IN RGB
    lower_blue = np.array([50, 0, 0])
    upper_blue = np.array([255, 50, 50])

    # loop over the boundaries
    #    for (lower, upper) in boundaries:
        # create NumPy arrays from the boundaries
    lower = np.array(lower_blue, dtype = "uint8")
    upper = np.array(upper_blue, dtype = "uint8")

    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(image, lower, upper)
    maskWidth, maskHeight = mask.shape[:2]

    cv2.imshow("mask ", mask)
    npImg = np.asarray( mask )  # No copying takes place

    coordList = np.argwhere( npImg == 255 )
    cv2.imshow("mask1 ", coordList)
    print coordList
    xmin = np.amin(coordList,axis=0)
    xmax = np.amax(coordList,axis=0)
    ymax = np.amax(coordList,axis=1)
    xStart = xmin[0]
    xEnd = xmax[0]

    output = cv2.bitwise_and(image, image, mask = mask)
    width, height = output.shape[:2]
    midpoint = width / 2


    # show the images
    cv2.imshow("images", np.hstack([image, output]))
    cv2.waitKey(0)

感谢您的帮助

【问题讨论】:

    标签: python opencv numpy image-processing


    【解决方案1】:

    通过阈值化并提出漂亮的白色斑点,您的想法是正确的,下一步是使用contours,然后使用image moment analysis

    系统将像素视为具有“质量” - 即白色比黑色重。

    有趣的事实:它实际上是直接平行在固体中寻找(平面)质心的机械过程 - 但在像素上离散(即求和,而不是积分)

    【讨论】:

      【解决方案2】:

      上面的答案很好而且完全正确,但为了简化,你可能会喜欢:imerode,只要图片上有白色的东西就应用。

      【讨论】:

      • 有趣的方法,我明白你在说什么。不过最适合圆形。
      猜你喜欢
      • 2012-07-29
      • 2018-04-08
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多