在Python中从图像中提取连接的对象

Khu*_*boo 9 python image scipy

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

在此输入图像描述

我试过这段代码:

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
Run Code Online (Sandbox Code Playgroud)

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

unu*_*tbu 13

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

from PIL import Image
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt

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

img = Image.open(fname).convert('L')
img = np.asarray(img)
print(img.shape)
# (160, 240)

# 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 {}".format(nr_objects))
# Number of objects is 4 

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

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用blur_radius = 1.0,这会找到4个对象.随着blur_radius = 0.5,找到5个对象:

在此输入图像描述