在Python中从图像中提取连接的对象 [英] Extracting connected objects from an image in Python

查看:300
本文介绍了在Python中从图像中提取连接的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个灰度png图像,我想从我的图像中提取所有连接的组件。
有些组件具有相同的强度,但我想为每个对象分配一个唯一的标签。
这是我的形象

I have a graysacle png image and I want to extract all the connected components from my image. Some of the components have same intensity but I want to assign a unique label to every object. here is my image

我试过这段代码:

img = imread(images + 'soccer_cif' + str(i).zfill(6) + '_GT_index.png')
labeled, nr_objects = label(img)
print "Number of objects is %d " % nr_objects

但是我只使用了三个对象。
请告诉我如何获取每个对象。

But I get just three objects using this. Please tell me how to get each object.

推荐答案

JF塞巴斯蒂安展示了一种识别图像中物体的方法。它需要手动选择高斯模糊半径和阈值,但是:

J.F. Sebastian shows a way to identify objects in an image. It requires manually choosing a gaussian blur radius and threshold value, however:

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt

fname='index.png'
blur_radius = 1.0
threshold = 50

img = scipy.misc.imread(fname) # gray-scale image
print(img.shape)

# smooth the image (to remove small objects)
imgf = ndimage.gaussian_filter(img, blur_radius)
threshold = 50

# find connected components
labeled, nr_objects = ndimage.label(imgf > threshold) 
print "Number of objects is %d " % nr_objects

plt.imsave('/tmp/out.png', labeled)
plt.imshow(labeled)

plt.show()

使用 blur_radius = 1.0 ,这将找到4个对象。
使用 blur_radius = 0.5 ,找到5个对象:

With blur_radius = 1.0, this finds 4 objects. With blur_radius = 0.5, 5 objects are found:

这篇关于在Python中从图像中提取连接的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆