def save_sliding_windows(in_dir, out_dir, stepSize, windowSize): img_cnt = 0 ff.check_and_mkdir(out_dir) for subdir, dirs, files in os.walk(in_dir): for f in files: if ff.is_image(f): file_path = os.path.join(subdir, f) image = cv2.imread(file_path) windowSize = random.randint(80, 100) #print(image.shape) #image = generate_padding_image(image, stepSize, windowSize) #print(image.shape) for x in range(0, image.shape[0], stepSize): for y in range(0, image.shape[0], stepSize): if (x + windowSize <= image.shape[0] and y + windowSize <= image.shape[0]): save_img = image[x:x + windowSize, y:y + windowSize, :] if (save_img.shape[0] == save_img.shape[1]): img_cnt += 1 save_dir = os.path.join( out_dir, "Crop_%d.png" % img_cnt) print("Saving %s ..." % save_dir) cv2.imwrite(save_dir, save_img)
def change_img_dir(in_dir, out_dir): img_cnt = 0 ff.check_and_mkdir(out_dir) for subdir, dirs, files in os.walk(in_dir): for f in files: if ff.is_image(f): file_path = os.path.join(subdir, f) img = cv2.imread(file_path) img_cnt += 1 out_path = os.path.join(out_dir, "RBC_%d.png" % img_cnt) print("Saving %s ..." % out_path) cv2.imwrite(out_path, img)
def pick_random_slides(in_dir, out_dir): ff.check_and_mkdir(out_dir) path, dirs, files = os.walk(in_dir).next() file_count = len(files) lst = random.sample(range(0, file_count), (1000 - 377 + 25)) for file_num in lst: file_path = os.path.join(in_dir, "Crop_%d.png" % file_num) img = cv2.imread(file_path) save_path = os.path.join(out_dir, "Crop_%d.png" % file_num) print(save_path) cv2.imwrite(save_path, img)
def save_random_crop(in_dir, out_dir): windowSize = random.randint(80, 100) img_cnt = 0 subdir_cnt = 0 ff.check_and_mkdir(out_dir) for subdir, dirs, files in os.walk(in_dir): for f in files: if ff.is_image(f): file_path = os.path.join(subdir, f) image = cv2.imread(file_path) print("Saving from %s ... " % f) subdir_cnt += 1 for i in range(5): crop = random_crop(image, (windowSize, windowSize, 3)) img_cnt += 1 save_dir = os.path.join(out_dir, "Crop_%d.png" % img_cnt) print("\tSaving %s ..." % save_dir) cv2.imwrite(save_dir, crop) print(subdir_cnt)
else: path = raw_input('Enter [original/resized] : ') if (not path in ['original', 'resized']): print( "[Error] : Please define the mode between [original/resized]." ) else: if (path == 'original'): ff.read_all_imgs(cf.data_base) elif (path == 'resized'): ff.read_all_imgs(cf.resize_dir) ############################################# # @ Module 3 : Resize and check images elif (mode == 'resize' or mode == '3'): ff.check_and_mkdir(cf.resize_base) if (sys.version_info > (3, 0)): target_size = int(input('Enter size : ')) else: target_size = int(raw_input('Enter size : ')) ff.resize_images(cf.data_base, cf.resize_dir, target_size) # ff.resize_and_contrast(cf.data_base, cf.resize_dir, target_size) ############################################# # @ Module 4 : Train-Validation split elif (mode == 'split' or mode == '4'): ff.check_and_mkdir(cf.split_base) split_dir = ff.create_train_val_split(cf.resize_dir, cf.split_dir) print("Train-Validation split directory = " + cf.split_dir) ############################################