|
| 1 | +import logging |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +class PatchImage: |
| 8 | + |
| 9 | + def __init__(self, patch_size: int, overlap_size: int, destination_root: str): |
| 10 | + logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) |
| 11 | + destination_root = Path(destination_root) |
| 12 | + self.train_folder = destination_root / f'imgs_{patch_size}/' |
| 13 | + self.train_gt_folder = destination_root / f'gt_imgs_{patch_size}/' |
| 14 | + self.train_folder.mkdir(parents=True, exist_ok=True) |
| 15 | + self.train_gt_folder.mkdir(parents=True, exist_ok=True) |
| 16 | + |
| 17 | + self.patch_size = patch_size |
| 18 | + self.overlap_size = overlap_size |
| 19 | + self.number_image = 1 |
| 20 | + self.image_name = "" |
| 21 | + |
| 22 | + logging.info(f"Using Patch size: {self.patch_size} - Overlapping: {self.overlap_size}") |
| 23 | + |
| 24 | + def create_patches(self, root_original: str): |
| 25 | + logging.info("Start process ...") |
| 26 | + root_original = Path(root_original) |
| 27 | + gt = root_original / 'gt_imgs' |
| 28 | + imgs = root_original / 'imgs' |
| 29 | + |
| 30 | + path_imgs = list(path_img for path_img in imgs.glob('*') if path_img.suffix in {".png", ".jpg", ".bmp", ".tif"}) |
| 31 | + for i, img in enumerate(path_imgs): |
| 32 | + or_img = cv2.imread(str(img)) |
| 33 | + gt_img = gt / img.name |
| 34 | + gt_img = gt_img if gt_img.exists() else gt / (img.stem + '.png') |
| 35 | + gt_img = cv2.imread(str(gt_img)) |
| 36 | + try: |
| 37 | + self._split_train_images(or_img, gt_img) |
| 38 | + except Exception as e: |
| 39 | + print(f'Error: {e} - {img}') |
| 40 | + |
| 41 | + def _split_train_images(self, or_img: np.ndarray, gt_img: np.ndarray): |
| 42 | + runtime_size = self.overlap_size |
| 43 | + patch_size = self.patch_size |
| 44 | + for i in range(0, or_img.shape[0], runtime_size): |
| 45 | + for j in range(0, or_img.shape[1], runtime_size): |
| 46 | + |
| 47 | + if i + patch_size <= or_img.shape[0] and j + patch_size <= or_img.shape[1]: |
| 48 | + dg_patch = or_img[i:i + patch_size, j:j + patch_size, :] |
| 49 | + gt_patch = gt_img[i:i + patch_size, j:j + patch_size, :] |
| 50 | + |
| 51 | + elif i + patch_size > or_img.shape[0] and j + patch_size <= or_img.shape[1]: |
| 52 | + dg_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 53 | + gt_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 54 | + |
| 55 | + dg_patch[0:or_img.shape[0] - i, :, :] = or_img[i:or_img.shape[0], j:j + patch_size, :] |
| 56 | + gt_patch[0:or_img.shape[0] - i, :, :] = gt_img[i:or_img.shape[0], j:j + patch_size, :] |
| 57 | + |
| 58 | + elif i + patch_size <= or_img.shape[0] and j + patch_size > or_img.shape[1]: |
| 59 | + dg_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 60 | + gt_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 61 | + |
| 62 | + dg_patch[:, 0:or_img.shape[1] - j, :] = or_img[i:i + patch_size, j:or_img.shape[1], :] |
| 63 | + gt_patch[:, 0:or_img.shape[1] - j, :] = gt_img[i:i + patch_size, j:or_img.shape[1], :] |
| 64 | + |
| 65 | + else: |
| 66 | + dg_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 67 | + gt_patch = np.ones((patch_size, patch_size, 3)) * 255 |
| 68 | + |
| 69 | + dg_patch[0:or_img.shape[0] - i, 0:or_img.shape[1] - j, :] = or_img[i:or_img.shape[0], |
| 70 | + j:or_img.shape[1], |
| 71 | + :] |
| 72 | + gt_patch[0:or_img.shape[0] - i, 0:or_img.shape[1] - j, :] = gt_img[i:or_img.shape[0], |
| 73 | + j:or_img.shape[1], |
| 74 | + :] |
| 75 | + gt_patch[0:or_img.shape[0] - i, 0:or_img.shape[1] - j, :] = gt_img[i:or_img.shape[0], |
| 76 | + j:or_img.shape[1], |
| 77 | + :] |
| 78 | + |
| 79 | + cv2.imwrite(str(self.train_folder / (str(self.number_image) + '.png')), dg_patch) |
| 80 | + cv2.imwrite(str(self.train_gt_folder / (str(self.number_image) + '.png')), gt_patch) |
| 81 | + self.number_image += 1 |
| 82 | + print(self.number_image, end='\r') |
| 83 | + |
| 84 | + def _create_name(self, folder: str, i: int, j: int): |
| 85 | + return folder + self.image_name.split('.')[0] + '_' + str(i) + '_' + str(j) + '.png' |
0 commit comments