15

グレーサクルの 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

しかし、これを使用して取得できるオブジェクトは 3 つだけです。各オブジェクトの入手方法を教えてください。

4

2 に答える 2

22

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()

ここに画像の説明を入力

blur_radius = 1.0場合、これは 4 つのオブジェクトを検出します。ではblur_radius = 0.5、5 つのオブジェクトが見つかりました。

ここに画像の説明を入力

于 2013-06-05T12:08:47.757 に答える