ich es geschafft haben, ein blaues Quadrat von diesem Bild zu erkennen:Wie Pixelposition eines weißen Klecks von Pixeln finden
Die Maske nur schwarz und weiß hat. Ich möchte die Position des weißen Blocks kennen, d. H. Seinen Mittelpunkt.
Meine Frage ist: Wie erkenne ich den Mittelpunkt des blauen Quadrats im Bild?
Ich habe den folgenden Code aus dem 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)
Danke an Ihre Hilfe
Interessanter Ansatz, ich kann sehen, was Sie sagen. Funktioniert am besten mit kreisförmigen Formen. –