我想在图像中检测一些圆圈(像细胞一样的圆圈),然后测量每个圆圈中的绿色(绿色像素的数量?)。
我正在使用以下代码进行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.
这个错误意味着什么,我应该怎么做才能解决它?
修复此错误后,如何遍历每个区域中的所有像素以计算绿色像素?
非常感谢您的帮助
答案 0 :(得分:1)
此处发生错误:
regions = measure.regionprops(image)
显然regionprops()
要求其参数具有整数数据类型。您使用
image
image = img_as_bool(color.rgb2gray(io.imread('0.06_3a.jpg')))
表示image
的数据类型为bool
。 bool
不是np.integer
的子类型,因此regionprops
会抱怨。
您可以尝试的快速解决方法是:
regions = measure.regionprops(image.astype(int))
但您应该重新考虑创建image
的方式。你为什么用img_as_bool()
?