Extract external contour or silhouette of image in Python


I want to extract the silhouette of an image, and I'm trying to do it using the contour function of MatplotLib. This is my code:

from PIL import Image
from pylab import *

# read image to array
im = array(Image.open('HOJA.jpg').convert('L'))

# create a new figure

# show contours with origin upper left corner
contour(im, origin='image')


This is my original image:


And this is my result:


But I just want to show the external contour, the silhouette. Just the read lines in this example.

How can I do it? I read the documentation of the contour function, but I can't get what I want.

If you know a better way to do this in Python, please tell me! (MatplotLib, OpenCV, etc.)

12/2/2012 3:43:58 PM

Accepted Answer

If you want to stick with your contour approach you can simply add a levels argument with a value 'thresholding' the image between the white background and the leaf.

You could use the histogram to find an appropriate value. But in this case any value slightly lower than 255 will do.


contour(im, levels=[245], colors='black', origin='image')

enter image description here

Make sure you checkout Scikit-Image if you want to do some serious image processing. It contains several edge detection algoritms etc.


11/27/2012 3:29:13 PM

For those who want the OpenCV solution, here it is:

ret,thresh = cv2.threshold(image,245,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

tam = 0

for contorno in contours:
    if len(contorno) > tam:
        contornoGrande = contorno
        tam = len(contorno)


cv2.imshow('My image',image)


In this example, I only draw the biggest contour. Remember that 'image' must be a single-channel array.

You should change the parameters of the threshold function, the findContours function and the drawContours function to get what you want.

I do the conversion to 'int' in the drawContours function because there is a bug in the Open CV 2.4.3 version, and if you don't do this conversion, the program breaks. This is the bug.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow