python等高线图轮廓线提取_在Python中提取外部轮廓或图像轮廓

博主尝试使用Matplotlib的contour函数从'HOJA.jpg'图像中提取轮廓,但得到的结果显示了所有轮廓而非仅外部轮廓(即图像的边缘)。为解决此问题,可以在调用contour函数时设置适当的`levels`参数,例如`levels=[245]`,以阈值处理图像,只显示背景和叶子之间的边界。此外,推荐使用Scikit-Image库进行更复杂的图像处理任务,它包含多种边缘检测算法。
摘要由CSDN通过智能技术生成

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值