I used this function to count objects in segmendted images (I loaded my pretrained weights for prediction)
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()
I tried this function with my first dataset and it counts correctly the cells in the images. but when I tried other images, len(measure.regionprops(imgl)) gives me a totally wrong count (it adds sometimes 8) here is an example, len(propsa) gives me 130 while when I count the cells in the result image i found 125. example I really don't understand where is the problem, the function never gave me a wrong count in the result image with the first dataset (the returned value is always equal to the manually counted cells in the result image) but the images in the second dataset always adds 1 or 2 or even 8 And I am not talking about the prediction, I am only talking about the result image. Even if there are noisy pixels in the images I believe that I got rid of them with:
predd=skimage.morphology.remove_small_objects(predd, min_size=25)
Please help me, I am about to pull my hair out. If you want any additional information please don't hesitate to comment. Thank you in advance
remove_small_objects expected a labeled image, putting: imgl=skimage.morphology.remove_small_objects(imgl, min_size=12) under imgl=measure.label(predd, background=0,connectivity=2) solved the problem.
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