defNLM_fliter(img): img2 = np.zeros([img.shape[0],img.shape[1]],np.uint8) for i inrange(10,img.shape[0]-10): for j inrange(10,img.shape[1]-10): search_window=img[i-10:i+11,j-10:j+11] patch=img[i-3:i+4,j-3:j+4] img2[i,j]=pixel_estimation(search_window,patch,100/256) return img2 defpixel_estimation(search_window,patch,h): window_height=search_window.shape[0] window_width=search_window.shape[1] patch_height=patch.shape[0] patch_width=patch.shape[1]
similarity=np.zeros((window_height-patch_height//2*2, window_width-patch_width//2*2)) for i inrange(patch_height//2,window_height-patch_height//2): for j inrange(patch_width//2,window_width-patch_width//2): temp=search_window[i-patch_height//2:i+patch_height//2+1, j-patch_width//2:j+patch_width//2+1] similarity[i-patch_height//2,j-patch_width//2]=weight_eucledian_distance(patch,temp,h) Z=np.sum(similarity) weights=np.zeros(search_window.shape) for i inrange(patch_height//2,window_height-patch_height//2): for j inrange(patch_width//2,window_width-patch_width//2): weights[i,j]=1/Z*similarity[i-patch_height//2,j-patch_width//2] NLM_estimation=np.sum(weights*search_window) return NLM_estimation.astype(np.uint8) defweight_eucledian_distance(patch1,patch2,h): return np.exp(-(patch1-patch2)**2/(h*h))