MCPcopy Create free account
hub / github.com/CandleLabAI/PCBSegClassNet / create_patches

Function create_patches

src/data/create_patches.py:21–61  ·  view source on GitHub ↗

Helper function to create patches Args: images_list: list of image paths masks_list: list of mask paths patch_size: integer size of patch dest_images_dir: destination directory to store images dest_masks_dir: destination directory to store masks

(images_list,
                   masks_list,
                   patch_size,
                   dest_images_dir,
                   dest_masks_dir)

Source from the content-addressed store, hash-verified

19import cv2
20
21def create_patches(images_list,
22 masks_list,
23 patch_size,
24 dest_images_dir,
25 dest_masks_dir):
26 """
27 Helper function to create patches
28 Args:
29 images_list: list of image paths
30 masks_list: list of mask paths
31 patch_size: integer size of patch
32 dest_images_dir: destination directory to store images
33 dest_masks_dir: destination directory to store masks
34 """
35 count = 0
36 with tqdm(total=len(images_list)) as pbar:
37 for image, mask in zip(images_list, masks_list):
38 img = cv2.imread(image)
39 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
40 msk = cv2.imread(mask)
41 msk = cv2.cvtColor(msk, cv2.COLOR_BGR2RGB)
42
43 for j in range(0, img.shape[0], patch_size):
44 for k in range(0, img.shape[1], patch_size):
45 temp = img[j:j+patch_size, k:k+patch_size, :]
46 if temp.shape[0] != patch_size:
47 j = j - (patch_size - temp.shape[0])
48 temp = img[j:j+patch_size, k:k+patch_size, :]
49 if temp.shape[1] != patch_size:
50 k = k - (patch_size - temp.shape[1])
51 temp = img[j:j+patch_size, k:k+patch_size, :]
52 cv2.imwrite(os.path.join(dest_images_dir,
53 f"image_{count}.png"),
54 cv2.cvtColor(img[j:j+patch_size, k:k+patch_size, :],
55 cv2.COLOR_BGR2RGB))
56 cv2.imwrite(os.path.join(dest_masks_dir,
57 f"image_{count}.png"),
58 cv2.cvtColor(msk[j:j+patch_size, k:k+patch_size, :],
59 cv2.COLOR_BGR2RGB))
60 count += 1
61 pbar.update(1)
62
63def split_data(source_images_dir,
64 source_masks_dir,

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected