Python TypeError:标签图像必须为整型

Python TypeError:标签图像必须为整型,python,numpy,image-processing,matplotlib,scikit-image,Python,Numpy,Image Processing,Matplotlib,Scikit Image,我想检测图像中的一些圆形细胞,然后测量绿色像素的绿色数?在每个圆圈里 我正在使用带有以下代码的讨论: 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')))

我想检测图像中的一些圆形细胞,然后测量绿色像素的绿色数?在每个圆圈里

我正在使用带有以下代码的讨论:

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.
这个错误意味着什么?我应该做些什么来修复它

修复此错误后,如何循环每个区域中的所有像素以计算绿色像素

非常感谢您的帮助

此处出现错误:

regions = measure.regionprops(image)
显然,regionprops要求其参数具有整数数据类型。您使用创建图像

这意味着图像的数据类型是bool。bool不是np.integer的子类型,因此regionprops会抱怨

您可以尝试的快速修复方法是:

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

但你可能应该重新思考你创造形象的方式。为什么使用img\u as\u bool?

可以提供完整的回溯吗?报告python错误时,始终显示完整的回溯,即完整的错误消息。它包括有用的信息。最重要的是,它显示了哪一行触发了错误。好的,对不起,我编辑了这篇文章:
regions = measure.regionprops(image.astype(int))