Skip to content

Commit cda9833

Browse files
committed
Corrected image dimensions
1 parent 5e104a4 commit cda9833

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

img_utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ def split_image(img, scaling_factor):
186186
nb_shards = scaling_factor * scaling_factor
187187

188188
# Holder for image shards
189-
shards = np.empty((nb_shards, shard_height, shard_width, 3))
189+
shards = np.empty((nb_shards, shard_width, shard_height, 3))
190190
shard_index = 0
191191

192192
for i in range(0, scaling_factor):
193193
for j in range(0, scaling_factor):
194-
shards[shard_index, :, :, :] = img[i*shard_height: (i+1)*shard_height,
195-
j*shard_width: (j+1)*shard_width, :]
194+
shards[shard_index, :, :, :] = img[j*shard_width: (j+1)*shard_width,
195+
i*shard_height: (i+1)*shard_height, :]
196196
shard_index += 1
197197

198198
return shards
@@ -207,12 +207,12 @@ def merge_images(imgs, scaling_factor):
207207
true_height, true_width = height * scaling_factor, width * scaling_factor
208208

209209
# Holder for image
210-
img = np.empty((true_height, true_width, 3))
210+
img = np.empty((true_width, true_height, 3))
211211
img_index = 0
212212

213213
for i in range(0, scaling_factor):
214214
for j in range(0, scaling_factor):
215-
img[i * height : (i+1) * height, j * width : (j+1) * width, :] = imgs[img_index, :, :, :]
215+
img[j * width : (j+1) * width, i * height : (i+1) * height, :] = imgs[img_index, :, :, :]
216216
img_index += 1
217217

218218
return img

models.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,10 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
101101
# Read image
102102
scale_factor = int(scale_factor)
103103
true_img = imread(img_path, mode='RGB')
104-
init_height, init_width = true_img.shape[0], true_img.shape[1]
104+
init_width, init_height = true_img.shape[0], true_img.shape[1]
105105
if verbose: print("Old Size : ", true_img.shape)
106106
if verbose: print("New Size : (%d, %d, 3)" % (init_height * scale_factor, init_width * scale_factor))
107107

108-
images = None
109108
img_height, img_width = 0, 0
110109

111110
denoise_models = ['Deep Denoise SR']
@@ -120,14 +119,14 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
120119
images = img_utils.make_patches(true_img, scale_factor, patch_size, verbose)
121120

122121
nb_images = images.shape[0]
123-
img_height, img_width = images.shape[1], images.shape[2]
122+
img_width, img_height = images.shape[1], images.shape[2]
124123
print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_height, img_width))
125124
else:
126125
# Use full image for super resolution
127-
img_height, img_width = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
126+
img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
128127
init_width, scale_factor)
129128

130-
images = imresize(true_img, (img_height, img_width))
129+
images = imresize(true_img, (img_width, img_height))
131130
images = np.expand_dims(images, axis=0)
132131
print("Image is reshaped to : (%d, %d, %d)" % (images.shape[1], images.shape[2], images.shape[3]))
133132

@@ -136,7 +135,7 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
136135
if save_intermediate:
137136
if verbose: print("Saving intermediate image.")
138137
fn = path[0] + "_intermediate_" + path[1]
139-
intermediate_img = imresize(true_img, (init_height * scale_factor, init_width * scale_factor))
138+
intermediate_img = imresize(true_img, (init_width * scale_factor, init_height * scale_factor))
140139
imsave(fn, intermediate_img)
141140

142141
# Transpose and Process images
@@ -159,9 +158,9 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
159158
else:
160159
result = result.astype(np.float32) * 255.
161160

162-
# Output shape is (original_height * scale, original_width * scale, nb_channels)
161+
# Output shape is (original_width * scale, original_height * scale, nb_channels)
163162
if mode == 'patch':
164-
out_shape = (init_height * scale_factor, init_width * scale_factor, 3)
163+
out_shape = (init_width * scale_factor, init_height * scale_factor, 3)
165164
result = img_utils.combine_patches(result, out_shape, scale_factor)
166165
else:
167166
result = result[0, :, :, :] # Access the 3 Dimensional image vector
@@ -181,15 +180,15 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
181180
if verbose: print("Evaluating results.")
182181
# Convert initial image into correct format
183182
if intermediate_img is None:
184-
intermediate_img = imresize(true_img, (init_height * scale_factor, init_width * scale_factor))
183+
intermediate_img = imresize(true_img, (init_width * scale_factor, init_height * scale_factor))
185184

186185
if mode == 'patch':
187186
intermediate_img = img_utils.make_patches(intermediate_img, scale_factor, patch_size, upscale=False)
188187
else:
189-
img_height, img_width = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
188+
img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
190189
init_width, scale_factor)
191190

192-
intermediate_img = imresize(true_img, (img_height, img_width))
191+
intermediate_img = imresize(true_img, (img_width, img_height))
193192
intermediate_img = np.expand_dims(intermediate_img, axis=0)
194193

195194
if K.image_dim_ordering() == "th":
@@ -230,9 +229,9 @@ def create_model(self, height=33, width=33, channels=3, load_weights=False, batc
230229
Creates a model to be used to scale images of specific height and width.
231230
"""
232231
if K.image_dim_ordering() == "th":
233-
shape = (channels, height, width)
232+
shape = (channels, width, height)
234233
else:
235-
shape = (height, width, channels)
234+
shape = (width, height, channels)
236235

237236
init = Input(shape=shape)
238237

@@ -268,9 +267,9 @@ def create_model(self, height=33, width=33, channels=3, load_weights=False, batc
268267
Creates a model to be used to scale images of specific height and width.
269268
"""
270269
if K.image_dim_ordering() == "th":
271-
shape = (channels, height, width)
270+
shape = (channels, width, height)
272271
else:
273-
shape = (height, width, channels)
272+
shape = (width, height, channels)
274273

275274
init = Input(shape=shape)
276275

@@ -308,9 +307,9 @@ def create_model(self, height=33, width=33, channels=3, load_weights=False, batc
308307
from keras.layers import merge
309308

310309
if K.image_dim_ordering() == "th":
311-
shape = (channels, height, width)
310+
shape = (channels, width, height)
312311
else:
313-
shape = (height, width, channels)
312+
shape = (width, height, channels)
314313

315314
init = Input(shape=shape)
316315

@@ -357,9 +356,9 @@ def create_model(self, height=32, width=32, channels=3, load_weights=False, batc
357356
from keras.layers import merge
358357

359358
if K.image_dim_ordering() == "th":
360-
shape = (channels, height, width)
359+
shape = (channels, width, height)
361360
else:
362-
shape = (height, width, channels)
361+
shape = (width, height, channels)
363362

364363
init = Input(shape=shape)
365364

0 commit comments

Comments
 (0)