二值图片轮廓提取
# -*- coding: UTF-8 -*- import cv2 import numpy as np import os reagents = ['MaskVIA1', 'MaskVIA3', 'MaskVILI'] if __name__ == '__main__': path = 'F:/project/Cancer/dataset/Cervical_Data/Cut_Data/CANCER/3/' for file_name in os.listdir(path): if not os.path.isdir(path+'/'+file_name): print(file_name) image = cv2.imread(path + '/' + file_name) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray_image = cv2.Canny(gray_image, 100, 200) gray_image = cv2.morphologyEx(gray_image, cv2.MORPH_DILATE, np.ones((5, 5), np.uint8)) gray_image = 255 - gray_image cv2.imwrite(path + 'new_label/' + file_name.split('.')[0] + '.' + file_name.split('.')[1], gray_image) ''' cv2.imshow(file_name, gray_image) cv2.waitKey(0) cv2.destroyWindow(file_name) ''' print('all_finished')
单个文件夹内部图片提取
# -*- coding: UTF-8 -*- import cv2 import numpy as np import os class_names = ['NORMAL', 'CIN1', 'CIN2', 'CIN3', 'CANCER'] reagents = ['MaskVIA1', 'MaskVIA3', 'MaskVILI'] if __name__ == '__main__': path = 'F:/project/Cancer/dataset/Cervical_Data/Cut_Data' path1 = 'F:/project/Cancer/dataset/Cervical_Data/Labels' for class_name in class_names: src_path = path + '/' + class_name src_path1 = path1 + '/' + class_name for patient_code in os.listdir(src_path): src_patient_dir = src_path + '/' + patient_code print(src_patient_dir) tar_patient_dir = src_path1 + '/' + patient_code print(tar_patient_dir) file_names = [res for res in os.listdir(src_patient_dir) if (res.startswith('Mask') and res.endswith('.jpg'))] os.makedirs(tar_patient_dir) for file_name in file_names: print(file_name) image = cv2.imread(src_patient_dir + '/' + file_name) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray_image = cv2.Canny(gray_image, 100, 200) gray_image = cv2.morphologyEx(gray_image, cv2.MORPH_DILATE, np.ones((10, 10), np.uint8)) gray_image = 255 - gray_image cv2.imwrite(tar_patient_dir +'/' + file_name, gray_image)
多层文件图片边缘分割