簡體   English   中英

使用openCV-python將兩個輪廓分割為兩個相同大小的不同圖像

[英]Segmenting two contours into two different images of same size using openCV-python

我在第一個圖像中有兩個輪廓。 我需要分割出各個輪廓,並像這樣從中制作出兩個圖像: image1image2 單個輸出圖像必須與輸入圖像具有相同的尺寸。 如何使用openCV-python實現? 我繪制輪廓的代碼:

    image, contours, hier = cv2.findContours(im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    box = np.int0(box)
    # draw a red 'nghien' rectangle
    cv2.drawContours(im, [box], 0, (0, 0, 255))
    cv2.drawContours(im, contours, -1, (255, 255, 0), 1)

您正在錯誤地使用cv2.drawContours 傳遞-1作為輪廓索引將繪制所有輪廓,而不是單個輪廓。 要繪制各個輪廓,您需要將相應的索引傳遞為:

_, cnt, hierarchy = cv2.findContours(canvas.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for i in xrange(len(cnt)):
    output_canvas = np.zeros(canvas.shape, dtype=np.uint8)
    cv2.drawContours(output_canvas, cnt, i, np.array([255, 255, 255, 255]), -1)
    cv2.imwrite("./contour{}.png".format(i), output_canvas)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM