【发布时间】: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