问题描述
我想提取图像的轮廓,我正在尝试使用MatplotLib的轮廓功能。这是我的代码:
来自PIL导入图片
来自pylab import *
#读取图像到数组
im = array(Image.open('HOJA.jpg')。convert('L'))
#创建一个新数字
figure()
#显示原点左上角的轮廓
轮廓(im,origin ='image')
轴('等于')
show( )
这是我的原始图片:
这是我的结果:
但我只是想展示外部轮廓,轮廓。只是这个例子中的读取行。
我该怎么办?我阅读了
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
figure()
# show contours with origin upper left corner
contour(im, origin='image')
axis('equal')
show()
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.)
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.
So:
contour(im, levels=[245], colors='black', origin='image')
Make sure you checkout Scikit-Image if you want to do some serious image processing. It contains several edge detection algoritms etc.
http://scikit-image.org/docs/dev/auto_examples/
这篇关于在Python中提取外部轮廓或图像轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!