2016-04-25 6 views
0

ich es geschafft haben, ein blaues Quadrat von diesem Bild zu erkennen:Wie Pixelposition eines weißen Klecks von Pixeln finden

enter image description here

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

Antwort

1

Sie sind auf die richtige Idee von Thresholding und kommt mit einem schönen weißen Klecks auf, die nächste Schritt ist zu verwenden und dann image moment analysis.

Das System behandelt Pixel als "Masse" - d. H. Weiß ist schwerer als Schwarz.

Fun Tatsache: Es ist eigentlich ein direkt parallel zur mechanischen Verfahren zur Herstellung einer (planar) Mitte der Masse in einem festen zu finden - aber über Pixel diskretisiert (dh Summierung, nicht Integration)

0

Antwort oben ist groß und vollständig richtig, aber um zu vereinfachen, können Sie gut mit sein: imerode, angewendet, solange es etwas Weißes auf dem Bild gibt.

+0

Interessanter Ansatz, ich kann sehen, was Sie sagen. Funktioniert am besten mit kreisförmigen Formen. –

Verwandte Themen