为什么 len(measure.regionprops(imgl)) 给我错误数量的对象?
why len(measure.regionprops(imgl)) gives me a wrong number of objects?
我使用这个函数来计算分割图像中的对象(我加载了我的预训练权重用于预测)
import pandas as pd
import skimage.io
import skimage.segmentation
def process_images(img):
img1=img
test_img1 = np.expand_dims(img1, axis=0)
prediction = model.predict(test_img1)
prediction_img=np.argmax(prediction, axis=3)[0,:,:]
pred=prediction_img
for i in range(0,512):
for j in range(0,512):
if pred[i,j]==1:
pred[i,j]=0
elif pred[i,j]==2:
pred[i,j]=255
imgwater=np.stack((pred,)*3, axis=-1)
kernel = np.ones((3,3),np.uint8)
predd=np.uint8(pred)
predd=skimage.morphology.remove_small_objects(predd, min_size=25)
predd=skimage.morphology.remove_small_holes(predd,area_threshold=50)
annot = skimage.morphology.label(predd,connectivity=2)
annot=color.label2rgb(annot,bg_label=0)
imgl=measure.label(predd, background=0,connectivity=2)
propsa = measure.regionprops(imgl)
a = len(propsa)
return img, prediction_img,annot,pred,a
k=4
imgo=image_dataset[k]
test_img = X[k]
img,prediction_img,annot,pred,a=process_images(test_img)
print(a)
plt.figure(figsize=(16, 8),dpi=90)
plt.subplot(231)
plt.title('Orginal Image')
plt.imshow(imgo,cmap='gray')
plt.subplot(232)
plt.title('Predicted image')
plt.imshow(prediction_img,cmap='jet')
plt.subplot(233)
plt.title('Predicted thresholded')
plt.imshow(pred,cmap='gray')
plt.subplot(234)
plt.title('result')
plt.show()
我用我的第一个数据集尝试了这个函数,它正确地计算了图像中的细胞。但是当我尝试其他图像时,len(measure.regionprops(imgl)) 给了我一个完全错误的计数(有时它会增加 8)
这是一个例子,len(propsa) 给我 130,而当我计算结果图像中的单元格时,我发现 125。
example
我真的不明白问题出在哪里,该函数从来没有给我第一个数据集的结果图像中的错误计数(返回值始终等于结果图像中手动计数的单元格),但第二个数据集中的图像数据集总是加 1 或 2 甚至 8 我不是在谈论预测,我只是在谈论结果图像。即使图像中有嘈杂的像素,我相信我已经摆脱了它们:
predd=skimage.morphology.remove_small_objects(predd, min_size=25)
请帮帮我,我要拔头发了。如果您需要任何其他信息,请随时发表评论。提前谢谢你
remove_small_objects 需要一个带标签的图像,将:
imgl=skimage.morphology.remove_small_objects(imgl, min_size=12)
在下面
imgl=measure.label(预测,背景=0,连通性=2)
解决了问题。
def process_images(img):
img1=img
test_img1 = np.expand_dims(img1, axis=0)
prediction = model.predict(test_img1)
prediction_img=np.argmax(prediction, axis=3)[0,:,:]
#print(np.unique(prediction_img))
#prediction = (model.predict(test_img1)[0]> 0.5).astype(np.uint8)
pred=prediction_img
for i in range(0,512):
for j in range(0,512):
if pred[i,j]==1:
pred[i,j]=0
elif pred[i,j]==2:
pred[i,j]=255
imgwater=np.stack((pred,)*3, axis=-1)
kernel = np.ones((3,3),np.uint8)
#no need for morphology because il will eliminate the tiny cells
predd=np.uint8(pred)
predd=skimage.morphology.remove_small_holes(predd,area_threshold=100)
imgl=measure.label(predd, background=0,connectivity=2)
imgl=skimage.morphology.remove_small_objects(imgl, min_size=12)
annot2=color.label2rgb(imgl,bg_label=0)
propsa = measure.regionprops(imgl)
#print(propsa)
a = len(propsa)
return img, prediction_img,annot2,pred,a
我使用这个函数来计算分割图像中的对象(我加载了我的预训练权重用于预测)
import pandas as pd
import skimage.io
import skimage.segmentation
def process_images(img):
img1=img
test_img1 = np.expand_dims(img1, axis=0)
prediction = model.predict(test_img1)
prediction_img=np.argmax(prediction, axis=3)[0,:,:]
pred=prediction_img
for i in range(0,512):
for j in range(0,512):
if pred[i,j]==1:
pred[i,j]=0
elif pred[i,j]==2:
pred[i,j]=255
imgwater=np.stack((pred,)*3, axis=-1)
kernel = np.ones((3,3),np.uint8)
predd=np.uint8(pred)
predd=skimage.morphology.remove_small_objects(predd, min_size=25)
predd=skimage.morphology.remove_small_holes(predd,area_threshold=50)
annot = skimage.morphology.label(predd,connectivity=2)
annot=color.label2rgb(annot,bg_label=0)
imgl=measure.label(predd, background=0,connectivity=2)
propsa = measure.regionprops(imgl)
a = len(propsa)
return img, prediction_img,annot,pred,a
k=4
imgo=image_dataset[k]
test_img = X[k]
img,prediction_img,annot,pred,a=process_images(test_img)
print(a)
plt.figure(figsize=(16, 8),dpi=90)
plt.subplot(231)
plt.title('Orginal Image')
plt.imshow(imgo,cmap='gray')
plt.subplot(232)
plt.title('Predicted image')
plt.imshow(prediction_img,cmap='jet')
plt.subplot(233)
plt.title('Predicted thresholded')
plt.imshow(pred,cmap='gray')
plt.subplot(234)
plt.title('result')
plt.show()
我用我的第一个数据集尝试了这个函数,它正确地计算了图像中的细胞。但是当我尝试其他图像时,len(measure.regionprops(imgl)) 给了我一个完全错误的计数(有时它会增加 8) 这是一个例子,len(propsa) 给我 130,而当我计算结果图像中的单元格时,我发现 125。 example 我真的不明白问题出在哪里,该函数从来没有给我第一个数据集的结果图像中的错误计数(返回值始终等于结果图像中手动计数的单元格),但第二个数据集中的图像数据集总是加 1 或 2 甚至 8 我不是在谈论预测,我只是在谈论结果图像。即使图像中有嘈杂的像素,我相信我已经摆脱了它们:
predd=skimage.morphology.remove_small_objects(predd, min_size=25)
请帮帮我,我要拔头发了。如果您需要任何其他信息,请随时发表评论。提前谢谢你
remove_small_objects 需要一个带标签的图像,将: imgl=skimage.morphology.remove_small_objects(imgl, min_size=12) 在下面 imgl=measure.label(预测,背景=0,连通性=2) 解决了问题。
def process_images(img):
img1=img
test_img1 = np.expand_dims(img1, axis=0)
prediction = model.predict(test_img1)
prediction_img=np.argmax(prediction, axis=3)[0,:,:]
#print(np.unique(prediction_img))
#prediction = (model.predict(test_img1)[0]> 0.5).astype(np.uint8)
pred=prediction_img
for i in range(0,512):
for j in range(0,512):
if pred[i,j]==1:
pred[i,j]=0
elif pred[i,j]==2:
pred[i,j]=255
imgwater=np.stack((pred,)*3, axis=-1)
kernel = np.ones((3,3),np.uint8)
#no need for morphology because il will eliminate the tiny cells
predd=np.uint8(pred)
predd=skimage.morphology.remove_small_holes(predd,area_threshold=100)
imgl=measure.label(predd, background=0,connectivity=2)
imgl=skimage.morphology.remove_small_objects(imgl, min_size=12)
annot2=color.label2rgb(imgl,bg_label=0)
propsa = measure.regionprops(imgl)
#print(propsa)
a = len(propsa)
return img, prediction_img,annot2,pred,a