|
import numpy as np |
|
|
|
|
|
def image_to_patches(img, label = None, patch_height, patch_width): |
|
H, W = img.shape |
|
|
|
num_patches_vert = H // patch_height |
|
num_patches_horz = W // patch_width |
|
|
|
patches = [] |
|
for i in range(num_patches_vert): |
|
for j in range(num_patches_horz): |
|
patch_img = img[i * patch_height:(i+1) * patch_height, |
|
j * patch_width:(j+1) * patch_width |
|
] |
|
if label is not None: |
|
patch_label = label[i * patch_height:(i+1) * patch_height, |
|
j * patch_width:(j+1) * patch_width |
|
] |
|
patches.append((patch_img, patch_label)) |
|
else: |
|
patches.append(patch_img) |
|
return patches |