2017-08-04 66 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 = img_as_bool(color.rgb2gray(io.imread('0.06_3a.jpg'))) 

这意味着image数据类型为bool创建imagebool不是np.integer的子类型,所以regionprops抱怨。

速战速决,您可以尝试是:

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

但你也许应该反思一下,你创建image的方式。你为什么用img_as_bool()

相关问题