import glob
import os
import cv2
import numpy as np
def dice(pre_img,mask_img):
# smooth = 1e-5
s = []
s1 = cv2.imread(pre_img,0)
pre_img = cv2.resize(s1, (256, 256))
pre_img[pre_img <125]=0
pre_img[pre_img > 125] = 1
s1 = pre_img
mask_img = cv2.imread(mask_img,0)
mask_img[mask_img < 125] = 0
mask_img[mask_img >= 125] = 1
s2 = mask_img
# print(pre_img.shape)
row,col = s2.shape[0],s2.shape[1]
for r in range(row):
for c in range(col):
if s1[r][c] == s2[r][c]:
s.append(s2[r][c])
# print(s)
intersection = np.linalg.norm(s)
output = np.linalg.norm(s1.flatten()) + np.linalg.norm(s2.flatten())
return (2 * intersection) / (output)
pre_path = '/home/zhengsc/Desktop/pytorch-nested-unet/outputs/colon_train_UNet_woDS_double_up/0/'
mask_path = '/home/zhengsc/Desktop/pytorch-nested-unet/inputs/colon_test/masks/0/'
pre_imgs = glob.glob(pre_path+'*.jpg')
DICE = []
# mask_imgs = glob.glob(mask_path+'*.jpg')
for pre_img in pre_imgs:
name = pre_img[84:]
mask_img = os.path.join(mask_path,name)
Dice = dice(pre_img,mask_img)
DICE.append(Dice)
dice_mean = np.mean(DICE)
dice_var = np.var(DICE)
std = np.std(DICE,ddof=1)
print(dice_mean,dice_var,std)
dice_test
最新推荐文章于 2022-01-02 20:10:01 发布