Skip to content

Commit

Permalink
Corrected image dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
titu1994 committed Sep 30, 2016
1 parent 5e104a4 commit cda9833
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
10 changes: 5 additions & 5 deletions img_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ def split_image(img, scaling_factor):
nb_shards = scaling_factor * scaling_factor

# Holder for image shards
shards = np.empty((nb_shards, shard_height, shard_width, 3))
shards = np.empty((nb_shards, shard_width, shard_height, 3))
shard_index = 0

for i in range(0, scaling_factor):
for j in range(0, scaling_factor):
shards[shard_index, :, :, :] = img[i*shard_height: (i+1)*shard_height,
j*shard_width: (j+1)*shard_width, :]
shards[shard_index, :, :, :] = img[j*shard_width: (j+1)*shard_width,
i*shard_height: (i+1)*shard_height, :]
shard_index += 1

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

# Holder for image
img = np.empty((true_height, true_width, 3))
img = np.empty((true_width, true_height, 3))
img_index = 0

for i in range(0, scaling_factor):
for j in range(0, scaling_factor):
img[i * height : (i+1) * height, j * width : (j+1) * width, :] = imgs[img_index, :, :, :]
img[j * width : (j+1) * width, i * height : (i+1) * height, :] = imgs[img_index, :, :, :]
img_index += 1

return img
Expand Down
37 changes: 18 additions & 19 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ def upscale(self, img_path, scale_factor=2, save_intermediate=False, return_imag
# Read image
scale_factor = int(scale_factor)
true_img = imread(img_path, mode='RGB')
init_height, init_width = true_img.shape[0], true_img.shape[1]
init_width, init_height = true_img.shape[0], true_img.shape[1]
if verbose: print("Old Size : ", true_img.shape)
if verbose: print("New Size : (%d, %d, 3)" % (init_height * scale_factor, init_width * scale_factor))

images = None
img_height, img_width = 0, 0

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

nb_images = images.shape[0]
img_height, img_width = images.shape[1], images.shape[2]
img_width, img_height = images.shape[1], images.shape[2]
print("Number of patches = %d, Patch Shape = (%d, %d)" % (nb_images, img_height, img_width))
else:
# Use full image for super resolution
img_height, img_width = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
init_width, scale_factor)

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

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

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

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

if mode == 'patch':
intermediate_img = img_utils.make_patches(intermediate_img, scale_factor, patch_size, upscale=False)
else:
img_height, img_width = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
img_width, img_height = self.__match_denoise_size(denoise_models, img_height, img_width, init_height,
init_width, scale_factor)

intermediate_img = imresize(true_img, (img_height, img_width))
intermediate_img = imresize(true_img, (img_width, img_height))
intermediate_img = np.expand_dims(intermediate_img, axis=0)

if K.image_dim_ordering() == "th":
Expand Down Expand Up @@ -230,9 +229,9 @@ def create_model(self, height=33, width=33, channels=3, load_weights=False, batc
Creates a model to be used to scale images of specific height and width.
"""
if K.image_dim_ordering() == "th":
shape = (channels, height, width)
shape = (channels, width, height)
else:
shape = (height, width, channels)
shape = (width, height, channels)

init = Input(shape=shape)

Expand Down Expand Up @@ -268,9 +267,9 @@ def create_model(self, height=33, width=33, channels=3, load_weights=False, batc
Creates a model to be used to scale images of specific height and width.
"""
if K.image_dim_ordering() == "th":
shape = (channels, height, width)
shape = (channels, width, height)
else:
shape = (height, width, channels)
shape = (width, height, channels)

init = Input(shape=shape)

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

if K.image_dim_ordering() == "th":
shape = (channels, height, width)
shape = (channels, width, height)
else:
shape = (height, width, channels)
shape = (width, height, channels)

init = Input(shape=shape)

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

if K.image_dim_ordering() == "th":
shape = (channels, height, width)
shape = (channels, width, height)
else:
shape = (height, width, channels)
shape = (width, height, channels)

init = Input(shape=shape)

Expand Down

0 comments on commit cda9833

Please sign in to comment.