这段代码用于多类分割处理掩码图片,在深度学习中,如果有三类分割,需要将标签设置为0,1,2
#改变图片像素值164-1;255-2;
from PIL import Image
import cv2
import os
# img = cv2.imread("F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Fovea_Illustration/g0001.jpg")
# img = cv2.imread("F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Non-Glaucoma_512/n0001.bmp",cv2.IMREAD_UNCHANGED) # 读取系统的内照片
def change_pixel(img):
width = img.shape[0] # 长度
height = img.shape[1] # 宽度
for i in range(0, width): # 遍历所有长度的点
for j in range(0, height): # 遍历所有宽度的点
if (img[i, j] == 164):
img[i, j] = 1
elif (img[i, j] == 255):
img[i, j] = 2
else:
img[i, j] = 0
return img
if __name__ =="__main__":
filePath = 'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Glaucoma_512/'
savePath = 'F:/AI/glaucoma_set/gla_img_120_Chall/Annotation-Training400/Disc_Cup_Masks/Glaucoma_512_012/'
name = os.listdir(filePath)
for i in name:
image_path = os.path.join(filePath,i)
image_save_Path = os.path.join(savePath,i)
image = cv2.imread(image_path, cv2.IMREAD_ANYCOLOR)
img = change_pixel(image)
cv2.imwrite(image_save_Path, img)