File size: 3,042 Bytes
6ee8d9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e261fac
 
 
6ee8d9f
 
 
 
 
e261fac
6ee8d9f
 
e261fac
6ee8d9f
e261fac
 
 
 
 
 
6ee8d9f
 
e261fac
 
6ee8d9f
e261fac
6ee8d9f
e261fac
6ee8d9f
e261fac
 
 
 
6ee8d9f
e261fac
 
 
6ee8d9f
 
 
 
e261fac
 
 
 
 
 
 
 
 
 
 
6ee8d9f
e261fac
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np

def pad_reflect(image, pad_size):
    imsize = image.shape
    height, width = imsize[:2]
    new_img = np.zeros([height+pad_size*2, width+pad_size*2, imsize[2]]).astype(np.uint8)
    new_img[pad_size:-pad_size, pad_size:-pad_size, :] = image
    new_img[0:pad_size, pad_size:-pad_size, :] = np.flip(image[0:pad_size, :, :], axis=0)
    new_img[-pad_size:, pad_size:-pad_size, :] = np.flip(image[-pad_size:, :, :], axis=0)
    new_img[:, 0:pad_size, :] = np.flip(new_img[:, pad_size:pad_size*2, :], axis=1)
    new_img[:, -pad_size:, :] = np.flip(new_img[:, -pad_size*2:-pad_size, :], axis=1)
    return new_img

def unpad_image(image, pad_size):
    return image[pad_size:-pad_size, pad_size:-pad_size, :]

def unpad_patches(image_patches, padding_size):
    return image_patches[:, padding_size:-padding_size, padding_size:-padding_size, :]

def split_image_into_overlapping_patches(image_array, patch_size, padding_size=2):
    xmax, ymax, _ = image_array.shape
    x_extend = (patch_size - (xmax % patch_size)) % patch_size
    y_extend = (patch_size - (ymax % patch_size)) % patch_size
    extended_image = np.pad(image_array, ((0, x_extend), (0, y_extend), (0, 0)), 'edge')
    
    padded_image = np.pad(extended_image, ((padding_size, padding_size), (padding_size, padding_size), (0, 0)), 'edge')
    
    xmax_pad, ymax_pad, _ = padded_image.shape
    patches = []
    
    # PERBAIKAN 1: Hitung jumlah patch per baris secara akurat dari loop
    n_patches_per_row = len(range(padding_size, ymax_pad - padding_size, patch_size))
    
    for x in range(padding_size, xmax_pad - padding_size, patch_size):
        for y in range(padding_size, ymax_pad - padding_size, patch_size):
            patch = padded_image[x-padding_size:x+patch_size+padding_size, y-padding_size:y+patch_size+padding_size, :]
            patches.append(patch)
            
    return np.array(patches), padded_image.shape, n_patches_per_row

def stich_together(patches, padded_image_shape, target_shape, padding_size=4, n_patches_per_row=None):
    xmax, ymax, _ = padded_image_shape
    patches = unpad_patches(patches, padding_size)
    patch_size = patches.shape[1]
    
    if n_patches_per_row is None:
        n_patches_per_row = max(1, round(ymax / patch_size))
    
    complete_image = np.zeros((xmax, ymax, 3))
    
    row = -1
    col = 0
    for i in range(len(patches)):
        if i % n_patches_per_row == 0:
            row += 1
            col = 0
        
        # PERBAIKAN 2: Gunakan min() untuk mencegah assignment melebihi batas array (mencegah broadcast error)
        x_start = row * patch_size
        x_end = min(x_start + patch_size, xmax)
        y_start = col * patch_size
        y_end = min(y_start + patch_size, ymax)
        
        patch_x_end = x_end - x_start
        patch_y_end = y_end - y_start
        
        complete_image[x_start:x_end, y_start:y_end, :] = patches[i][:patch_x_end, :patch_y_end, :]
        col += 1
        
    return complete_image[0: target_shape[0], 0: target_shape[1], :]