2017-08-04 15 views
0

イメージ内の円(セルのような円)を検出し、各円の緑色(緑色のピクセル数?)を測定したいとします。TypeError:ラベルイメージは整数型でなければなりません

私は次のコードでthis議論を使用しています:私は次のエラーを取得する

from skimage import io, color, measure, draw, img_as_bool 
import numpy as np 
from scipy import optimize 
import matplotlib.pyplot as plt 


image = img_as_bool(color.rgb2gray(io.imread('0.06_3a.jpg'))) 
regions = measure.regionprops(image) 
bubble = regions[0] 

y0, x0 = bubble.centroid 
r = bubble.major_axis_length/2. 

def cost(params): 
    x0, y0, r = params 
    coords = draw.circle(y0, x0, r, shape=image.shape) 
    template = np.zeros_like(image) 
    template[coords] = 1 
    return -np.sum(template == image) 

x0, y0, r = optimize.fmin(cost, (x0, y0, r)) 

import matplotlib.pyplot as plt 

f, ax = plt.subplots() 
circle = plt.Circle((x0, y0), r) 
ax.imshow(image, cmap='gray', interpolation='nearest') 
ax.add_artist(circle) 
plt.show() 

/home/mahsa/anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py:118: UserWarning: Possible sign loss when converting negative image of type float64 to positive image of type bool. 
    .format(dtypeobj_in, dtypeobj_out)) 
/home/mahsa/anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py:122: UserWarning: Possible precision loss when converting from float64 to bool 
    .format(dtypeobj_in, dtypeobj_out)) 
Traceback (most recent call last): 
    File "img.py", line 28, in <module> 
    regions = measure.regionprops(image) 
    File "/home/mahsa/anaconda3/lib/python3.6/site-packages/skimage/measure/_regionprops.py", line 539, in regionprops 
    raise TypeError('Label image must be of integral type.') 
TypeError: Label image must be of integral type. 
  1. は、このエラーが何を意味し、私はそれを修正するために何をすべき?

  2. このエラーを修正したら、各領域のすべてのピクセルをループして緑色のピクセルを数えますか?

+1

トレースバック全体を提供できますか? – user615501

+0

Pythonエラーを報告するときは、常に* complete * traceback(完全なエラーメッセージ)を表示してください。有用な情報が含まれています。最も重要なことは、エラーをトリガーした行を示します。 –

+0

さて、申し訳ありません、投稿を編集しました。 :) – user8224662

答えて

1

あなたの助けをありがとうございましたエラーが発生し、ここで:

regions = measure.regionprops(image) 

どうやらregionprops()は、整数データ型を持っているために、その引数を必要とします。あなたは

imageのデータ型が boolであることを意味し
image = img_as_bool(color.rgb2gray(io.imread('0.06_3a.jpg'))) 

imageを作成しました。 boolnp.integerのサブタイプではないため、regionpropsが文句を言います。

regions = measure.regionprops(image.astype(int)) 

しかし、あなたはおそらくあなたがimageを作成する方法を再考する必要があります。あなたが試すことができます

クイックフィックスです。 img_as_bool()はなぜ使用しましたか?

関連する問題