眼底图像预处理方案:
def crop_image_from_gray(img, tol=7):
if img.ndim == 2:
mask = img > tol
return img[np.ix_(mask.any(1), mask.any(0))]
elif img.ndim == 3:
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
mask = gray_img > tol
check_shape = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))].shape[0]
if (check_shape == 0): # image is too dark so that we crop out everything,
return img # return original image
else:
img1 = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))]
img2 = img[:, :, 1][np.ix_(mask.any(1), mask.any(0))]
img3 = img[:, :, 2][np.ix_(mask.any(1), mask.any(0))]
# print(img1.shape,img2.shape,img3.shape)
img = np.stack([img1, img2, img3], axis=-1)
# print(img.shape)
return img
def load_ben_color(path, sigmaX=10 ):
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = crop_image_from_gray(image)
image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)
return image
img = ‘C:\Users\210\Desktop\32_4939.jpg’
image = load_ben_color( img, sigmaX=10)
cv2.imshow(‘processed’,image)
前后效果对比
链接:https://www.kaggle.com/ratthachat/aptos-updated-preprocessing-ben-s-cropping