File size: 834 Bytes
a37f9bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np


def image_to_patches(img, label = None, patch_height, patch_width):
    H, W = img.shape
    # Calculate how many patches fit along each dimension
    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