def Bwdist(image, t=8): if not isinstance(image, np.ndarray): img = np.array(image) # print(img.max()) else: img = image dist1 = bwdist(np.logical_not(img)) dist2 = bwdist(img) dist1 = 255 - ((dist1 < t) * 255).astype(np.uint8) dist2 = 255 - ((dist2 < t) * 255).astype(np.uint8) b = Image.fromarray(dist1).convert('L') f = Image.fromarray(dist2).convert('L') return b, f
def createMask(filtered): from scipy.ndimage import label,binary_erosion from scipy.ndimage import distance_transform_edt as bwdist from skimage.filters import threshold_otsu val = threshold_otsu(filtered); mask = np.zeros(filtered.shape); for z in tqdm.tqdm(range(filtered.shape[2])): label_img = label(filtered[:,:,z]<=val)[0]; mask[:,:,z] = binary_erosion(1-((label_img==label_img[0,0])*1)); dist_mask = bwdist(mask[:,:,0]); radius = np.max(dist_mask); [x,y] = np.where(dist_mask==radius); x=int(x[0]); y=int(y[0]); z = int(filtered.shape[2]/2.0); radius = int(radius/2.0); # obs = filtered[:,:,100:500].copy(); # mobs = mask[:,:,100:500].copy() # mobs = mobs==1; # obs = obs[mobs].flatten() masked = filtered*mask; obs = np.zeros([filtered.shape[0],filtered.shape[1],60]) obs[:,:,:20] = masked[:,:,:20] obs[:,:,20:40] = masked[:,:,z-10:z+10] obs[:,:,40:60] = masked[:,:,filtered.shape[2]-20:filtered.shape[2]] obs = obs[obs>0] return mask,obs;
def cal_wfm(self, pred: np.ndarray, gt: np.ndarray) -> float: # [Dst,IDXT] = bwdist(dGT); Dst, Idxt = bwdist(gt == 0, return_indices=True) # %Pixel dependency # E = abs(FG-dGT); E = np.abs(pred - gt) Et = np.copy(E) Et[gt == 0] = Et[Idxt[0][gt == 0], Idxt[1][gt == 0]] # K = fspecial('gaussian',7,5); # EA = imfilter(Et,K); K = self.matlab_style_gauss2D((7, 7), sigma=5) EA = convolve(Et, weights=K, mode="constant", cval=0) # MIN_E_EA = E; # MIN_E_EA(GT & EA<E) = EA(GT & EA<E); MIN_E_EA = np.where(gt & (EA < E), EA, E) # %Pixel importance B = np.where(gt == 0, 2 - np.exp(np.log(0.5) / 5 * Dst), np.ones_like(gt)) Ew = MIN_E_EA * B TPw = np.sum(gt) - np.sum(Ew[gt == 1]) FPw = np.sum(Ew[gt == 0]) R = 1 - np.mean(Ew[gt == 1]) P = TPw / (TPw + FPw + _EPS) # % Q = (1+Beta^2)*(R*P)./(eps+R+(Beta.*P)); Q = (1 + self.beta) * R * P / (R + self.beta * P + _EPS) return Q
def Bwdist(image, t=8): shape = image.shape[2:] batch_size = image.shape[0] image = image.reshape((-1, shape[0], shape[1], 1)) # image = image.squeeze() image2 = image.copy() dist1 = image.copy() dist2 = image.copy() for i in range(batch_size): image[i,:,:,:] = bwdist(np.logical_not(image[i,:,:,:])) image2[i,:,:,:] = bwdist((image2[i,:,:,:])) dist1[i,:,:,:] = 1.0 - ((image[i,:,:,:]<t)*1.0) dist2[i,:,:,:] = 1.0 - ((image2[i,:,:,:]<t)*1.0) dist1 = dist1.reshape((-1, 1, shape[0], shape[1])) dist2 = dist2.reshape((-1, 1, shape[0], shape[1])) return dist1, dist2
def cal_wfm(self, pred: np.ndarray, gt: np.ndarray) -> float: """ Calculate the weighted F-measure. """ # [Dst,IDXT] = bwdist(dGT); Dst, Idxt = bwdist(gt == 0, return_indices=True) # %Pixel dependency # E = abs(FG-dGT); E = np.abs(pred - gt) # Et = E; # Et(~GT)=Et(IDXT(~GT)); %To deal correctly with the edges of the foreground region Et = np.copy(E) Et[gt == 0] = Et[Idxt[0][gt == 0], Idxt[1][gt == 0]] # K = fspecial('gaussian',7,5); # EA = imfilter(Et,K); K = self.matlab_style_gauss2D((7, 7), sigma=5) EA = convolve(Et, weights=K, mode="constant", cval=0) # MIN_E_EA = E; # MIN_E_EA(GT & EA<E) = EA(GT & EA<E); MIN_E_EA = np.where(gt & (EA < E), EA, E) # %Pixel importance # B = ones(size(GT)); # B(~GT) = 2-1*exp(log(1-0.5)/5.*Dst(~GT)); # Ew = MIN_E_EA.*B; B = np.where(gt == 0, 2 - np.exp(np.log(0.5) / 5 * Dst), np.ones_like(gt)) Ew = MIN_E_EA * B # TPw = sum(dGT(:)) - sum(sum(Ew(GT))); # FPw = sum(sum(Ew(~GT))); TPw = np.sum(gt) - np.sum(Ew[gt == 1]) FPw = np.sum(Ew[gt == 0]) # R = 1- mean2(Ew(GT)); %Weighed Recall # P = TPw./(eps+TPw+FPw); %Weighted Precision # 注意这里使用mask索引矩阵的时候不可使用Ew[gt],这实际上仅在索引Ew的0维度 R = 1 - np.mean(Ew[gt == 1]) P = TPw / (TPw + FPw + _EPS) # % Q = (1+Beta^2)*(R*P)./(eps+R+(Beta.*P)); Q = (1 + self.beta) * R * P / (R + self.beta * P + _EPS) return Q