Python:将彩色标注图转变为灰度值标签图

import os
from PIL import Image
import cv2 as cv
import numpy as np

label_dir = './label/'
label_process_dir = './label_process/'
if not os.path.exists(label_process_dir):
    os.makedirs(label_process_dir)

for file in os.listdir(label_dir):
    label = label_dir + file
    if os.path.isfile(label):
        print(file)
        # 获得一张图片的所有种类的灰度值
        label_array = cv.imread(label, 0)
        print('label:', label_array.shape)
        class_image = np.unique(np.array(label_array))
        print('class_image:', class_image)

        for i in class_image:
            if i in [255, 106, 0]:  # 84, 111, 208,

                pass
            else:
                print('---------------------------------------------------------------')
                
        label_array[label_array == 0] =   1   #舌脉
        label_array[label_array == ]255 = 0   #背景
       # label_array[label_array == 106] = 2  
       # label_array[label_array == 84] = 2
       # label_array[label_array == 111] = 2
       # label_array[label_array == 208] = 2


        cv.imwrite(label_process_dir + file, label_array)

你可能感兴趣的:(代码,python)