简体   繁体   English

如何找到像素白色斑点的像素位置

[英]How to find pixel location of a white blob of pixels

I have managed to detect a blue square from this image : 我已经设法从这张图片中发现一个蓝色方块:

在此处输入图片说明

The mask has just black and white. 面具只有黑色和白色。 I want to know the position of the white block, ie its middle point. 我想知道白色块的位置,即中间点。

My question is : How do I detect the middle point of the blue square in the picture ? 我的问题是:如何检测图片中蓝色正方形的中间点?

I got the following code from the internet : 我从互联网上获得了以下代码:

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

Thank you to your help 谢谢你的帮助

You're on to the right idea by thresholding and coming up with a nice white blob, the next step is to use contours and then image moment analysis . 通过阈值处理并提出一个漂亮的白色斑点,您将找到正确的想法,下一步是使用轮廓 ,然后进行图像矩分析

The system treats pixels as having 'mass' - ie white is heavier than black. 系统将像素视为具有“质量”,即白色比黑色重。

Fun fact: It's actually a direct parallel to the mechanical process of finding a (planar) centre of mass in a solid - but discretised over pixels (ie summation, not integration) 有趣的事实:这实际上在实体中找到(平面)质心的机械过程直接平行 -但离散化在像素上(即求和,而不是积分)

上面的答案很好并且完全正确,但是为简单起见,您可能会很满意: imerode ,只要图片上有白色,就可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM