Region, Wachsende python

Ich arbeiten auf region-growing-Algorithmus-Implementierung in python. Aber wenn ich diesen code bei der Ausgabe bekomme ich Schwarzes Bild ohne Fehler. Über CV-Schwellenwert-Funktion auf input-Bild und für den seed-Wert, den ich verwenden Sie die Maus, klicken Sie auf zum speichern von x -, y-Werte im Tupel.

def get8n(x, y, shape):
    out = []
    if y-1 > 0 and x-1 > 0:
        out.append( (y-1, x-1) )
    if y-1 > 0 :
        out.append( (y-1, x))
    if y-1 > 0 and x+1 < shape[1]:
        out.append( (y-1, x+1))
    if x-1 > 0:
        out.append( (y, x-1))
    if x+1 < shape[1]:
        out.append( (y, x+1))
    if y+1 < shape[0] and x-1 > 0:
        out.append( ( y+1, x-1))
    if y+1 < shape[0] :
        out.append( (y+1, x))
    if y+1 < shape[0] and x+1 < shape[1]:
       out.append( (y+1, x+1))
    return out

def region_growing(img, seed):
    list = []
    outimg = np.zeros_like(img)

    list.append((seed[0], seed[1]))
    while(len(list)):
        pix = list[0]
        outimg[pix[0], pix[1]] = 255
        for coord in get8n(pix[0], pix[1], img.shape):
            if img[coord[0], coord[1]] > 0:
                outimg[coord[0], coord[1]] = 255
                list.append((coord[0], coord[1]))
        list.pop(0)
    return outimg

def on_mouse(event, x, y, flags, params): 
    if event == cv2.EVENT_LBUTTONDOWN: 
        print 'Seed: ' + str(x) + ', ' + str(y) 
        clicks.append((y,x)) 

clicks = []
image = cv2.imread('lena.jpg', 0) 
ret, img = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY) 
cv2.namedWindow('Input') 
cv2.setMouseCallback('Input', on_mouse, 0, ) 
cv2.imshow('Input', img) 
cv2.waitKey() 
seed = clicks[-1] 
cv2.imshow('Region Growing', region_growing(img, seed)) 
cv2.waitKey() 
cv2.destroyAllWindows()
  • Da fragst du nach Lesen und anzeigen eines Bildes, können Sie fügen Sie den code, wo Sie Lesen/manipulieren der input-Bild und die Ausgabe angezeigt Bild?
  • Es ist der gesamte code für input-Bild def on_mouse(event, x, y, flags, params): if event == cv2.EVENT_LBUTTONDOWN: print 'Seed: ' + str(x) + ', ' + str(y) clicks.append((y,x)) clicks = [] image = cv2.imread('lenna.jpg', 0) ret, img = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY) cv2.namedWindow('Input') cv2.setMouseCallback('Input', on_mouse, 0, ) cv2.imshow('Input', img) cv2.waitKey() seed = clicks[-1] cv2.imshow('Region Growing', region_growing(img, seed)) cv2.waitKey() cv2.destroyAllWindows()
  • Fügen Sie den code hinzu, um die Frage, mit der richtigen Formatierung - es ist nicht lesbar wie dieses.
  • Was erwarten Sie von Ihrem code zu tun? Jetzt scheint es zu zeichnen, ein leeres Bild mit einem weißen pixel in der letzten Stelle, die Sie angeklickt haben. Wenn Sie auf einen weißen Bereich des Eingangs geht es in eine Endlosschleife. Auf der anderen Seite bedeutet dies, dass der output-Bild ist nicht schwarz, sondern hat ein einzelnes weißes pixel.
  • Ich will zu Durchlaufen, alle Nachbarn Pixel seed-pixel(geklickt) und Pixel, deren Wert größer als 0 auf 255 gesetzt. Und weiter, dass, so lange gibt es Nachbarn Pixel größer als 0 ist. Und ordnen Sie der region, wird das Ausgabe-Bild
InformationsquelleAutor David | 2017-05-11
Schreibe einen Kommentar