的白色斑點我已成功地從該圖像檢測出藍色正方形的像素位置:如何找到像素
面具剛剛黑色和白色。我想知道白塊的位置,即它的中間點。
我的問題是:如何檢測圖片中藍色方塊的中點?
我從網上得到下面的代碼:
# 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)
謝謝你的幫助
有趣的方法,我可以看到你在說什麼。儘管圓形形狀效果最好。 –