Spaces:
Sleeping
Sleeping
Update RealESRGAN/utils.py
Browse files- RealESRGAN/utils.py +35 -9
RealESRGAN/utils.py
CHANGED
|
@@ -14,31 +14,57 @@ def pad_reflect(image, pad_size):
|
|
| 14 |
def unpad_image(image, pad_size):
|
| 15 |
return image[pad_size:-pad_size, pad_size:-pad_size, :]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
def split_image_into_overlapping_patches(image_array, patch_size, padding_size=2):
|
| 18 |
xmax, ymax, _ = image_array.shape
|
| 19 |
x_extend = (patch_size - (xmax % patch_size)) % patch_size
|
| 20 |
y_extend = (patch_size - (ymax % patch_size)) % patch_size
|
| 21 |
extended_image = np.pad(image_array, ((0, x_extend), (0, y_extend), (0, 0)), 'edge')
|
|
|
|
| 22 |
padded_image = np.pad(extended_image, ((padding_size, padding_size), (padding_size, padding_size), (0, 0)), 'edge')
|
| 23 |
|
| 24 |
-
|
| 25 |
patches = []
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
patch = padded_image[x-padding_size:x+patch_size+padding_size, y-padding_size:y+patch_size+padding_size, :]
|
| 29 |
patches.append(patch)
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
def stich_together(patches, padded_image_shape, target_shape, padding_size=4):
|
| 33 |
xmax, ymax, _ = padded_image_shape
|
|
|
|
| 34 |
patch_size = patches.shape[1]
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
complete_image = np.zeros((xmax, ymax, 3))
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
for i in range(len(patches)):
|
| 39 |
if i % n_patches_per_row == 0:
|
| 40 |
row += 1
|
| 41 |
col = 0
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
col += 1
|
| 44 |
-
|
|
|
|
|
|
| 14 |
def unpad_image(image, pad_size):
|
| 15 |
return image[pad_size:-pad_size, pad_size:-pad_size, :]
|
| 16 |
|
| 17 |
+
def unpad_patches(image_patches, padding_size):
|
| 18 |
+
return image_patches[:, padding_size:-padding_size, padding_size:-padding_size, :]
|
| 19 |
+
|
| 20 |
def split_image_into_overlapping_patches(image_array, patch_size, padding_size=2):
|
| 21 |
xmax, ymax, _ = image_array.shape
|
| 22 |
x_extend = (patch_size - (xmax % patch_size)) % patch_size
|
| 23 |
y_extend = (patch_size - (ymax % patch_size)) % patch_size
|
| 24 |
extended_image = np.pad(image_array, ((0, x_extend), (0, y_extend), (0, 0)), 'edge')
|
| 25 |
+
|
| 26 |
padded_image = np.pad(extended_image, ((padding_size, padding_size), (padding_size, padding_size), (0, 0)), 'edge')
|
| 27 |
|
| 28 |
+
xmax_pad, ymax_pad, _ = padded_image.shape
|
| 29 |
patches = []
|
| 30 |
+
|
| 31 |
+
# PERBAIKAN 1: Hitung jumlah patch per baris secara akurat dari loop
|
| 32 |
+
n_patches_per_row = len(range(padding_size, ymax_pad - padding_size, patch_size))
|
| 33 |
+
|
| 34 |
+
for x in range(padding_size, xmax_pad - padding_size, patch_size):
|
| 35 |
+
for y in range(padding_size, ymax_pad - padding_size, patch_size):
|
| 36 |
patch = padded_image[x-padding_size:x+patch_size+padding_size, y-padding_size:y+patch_size+padding_size, :]
|
| 37 |
patches.append(patch)
|
| 38 |
+
|
| 39 |
+
return np.array(patches), padded_image.shape, n_patches_per_row
|
| 40 |
|
| 41 |
+
def stich_together(patches, padded_image_shape, target_shape, padding_size=4, n_patches_per_row=None):
|
| 42 |
xmax, ymax, _ = padded_image_shape
|
| 43 |
+
patches = unpad_patches(patches, padding_size)
|
| 44 |
patch_size = patches.shape[1]
|
| 45 |
+
|
| 46 |
+
if n_patches_per_row is None:
|
| 47 |
+
n_patches_per_row = max(1, round(ymax / patch_size))
|
| 48 |
+
|
| 49 |
complete_image = np.zeros((xmax, ymax, 3))
|
| 50 |
+
|
| 51 |
+
row = -1
|
| 52 |
+
col = 0
|
| 53 |
for i in range(len(patches)):
|
| 54 |
if i % n_patches_per_row == 0:
|
| 55 |
row += 1
|
| 56 |
col = 0
|
| 57 |
+
|
| 58 |
+
# PERBAIKAN 2: Gunakan min() untuk mencegah assignment melebihi batas array (mencegah broadcast error)
|
| 59 |
+
x_start = row * patch_size
|
| 60 |
+
x_end = min(x_start + patch_size, xmax)
|
| 61 |
+
y_start = col * patch_size
|
| 62 |
+
y_end = min(y_start + patch_size, ymax)
|
| 63 |
+
|
| 64 |
+
patch_x_end = x_end - x_start
|
| 65 |
+
patch_y_end = y_end - y_start
|
| 66 |
+
|
| 67 |
+
complete_image[x_start:x_end, y_start:y_end, :] = patches[i][:patch_x_end, :patch_y_end, :]
|
| 68 |
col += 1
|
| 69 |
+
|
| 70 |
+
return complete_image[0: target_shape[0], 0: target_shape[1], :]
|