2017-09-04 1 views
0

I mit Bild will KonturSo sparen OpenCV Bild mit Kontur

Hier sparen, ist mein Code:

img = cv2.imread('123.png') 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) 
image, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 

for cnt in contours: 
    # some code in here 
    cv2.imwrite('234.jpg', cnt) 

Vielen Dank.

+0

Haben Sie versucht, [die Vorschläge in diesem Tutorial] (http://docs.opencv.org/3.2.0/d4/d73/tutorial_py_contours_begin.html)? –

+0

Hallo Ken, Ja, ich habe versucht, die Kontur zu zeichnen, aber ich möchte das Bild mit Kontur speichern. – Yang

+0

Was bedeutet "Bild mit Kontur speichern"? –

Antwort

1

Sie möchten eine Maske erstellen, auf die Sie die Konturen zeichnen, und dann den Rest des Bildes ausschneiden oder umgekehrt. Zum Beispiel based on this tutorial:

(contours, _) = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 
mask = np.ones(img.shape[:2], dtype="uint8") * 255 

# Draw the contours on the mask 
cv2.drawContours(mask, contours, -1, 0, -1) 

# remove the contours from the image and show the resulting images 
img = cv2.bitwise_and(img, img, mask=mask) 
cv2.imshow("Mask", mask) 
cv2.imshow("After", img) 
cv2.waitKey(0) 
Verwandte Themen