Context
I wrote a program that takes in input an image normalized by another of my programs.
The input image is:
The program outputs the normalized version of the image. The normalization method is shown further below.
The problem
The output wrongly consists in multicolor mosaics of pixels. Given the above input, the output is (zoomed):
The expected result should be the above plane.
Minimal & Executable Sources
- In a directories path of your choice, put the input image in a directory named of your choice.
- In this same directories path, create a second directory named differently. It will contain the output image created by the program.
- Copy&Paste the below code, replace the two constants containing the directories paths with your custom both directories paths.
- Execute the code
- Take a look at the output image: you should see the same output image than the one I have shown in this question!
In the below code:
- You can see the denormalization method
- The methods
__plot_images_predicted_for_testing
creates with Matplotlib.Pyplot a window containing the output image, from the RAM input image - The input image is fetched thanks to
__fetch_the_normalized_image
and__load_XYZ
loads the input image from disk to RAM.
import numpy as np
from numpy import array
import matplotlib.pyplot as plt
import os
from skimage import data
class Launcher:
__PATH_CONTAINING_THE_NORMALIZED_IMAGE = "/content/drive/My Drive/Informatique/Projets_Informatiques" \
"/Projets_Python/bug_isolation_mosaics_of_pixels/normalized_image"
__PATH_CONTAINING_THE_DENORMALIZED_IMAGE = "/content/drive/My Drive/Informatique/Projets_Informatiques" \
"/Projets_Python/bug_isolation_mosaics_of_pixels" \
"/denormalized_image"
def __init__(self):
pass
def show_denormalized_image_from_normalized_image(self):
self.__plot_images_predicted_for_testing()
def __plot_images_predicted_for_testing(self):
normalized_images = self.__fetch_the_normalized_image()
normalized_image = normalized_images[[0, 0, 0]]
denormalized_image = self.__denormalize(normalized_image)
figure = plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(denormalized_image[1], interpolation='nearest')
plt.axis('off')
plt.tight_layout()
plt.savefig(self.__PATH_CONTAINING_THE_DENORMALIZED_IMAGE + '/the_denormalized_image')
plt.close(figure)
@staticmethod
def __denormalize(input_data):
input_data = (input_data + 1) * 127.5
return input_data.astype(np.uint8)
def __fetch_the_normalized_image(self):
print("Loading the normalized image")
images = []
loaded_images = self.__load_data(
Launcher.__PATH_CONTAINING_THE_NORMALIZED_IMAGE,
".jpg")
for img in range(len(loaded_images)):
images.append(loaded_images[img])
images = array(images)
print("/Loading the normalized image")
return images
@staticmethod
def __load_data_from_dirs(dirs, ext):
files = []
file_names = []
count = 0
for d in dirs:
for f in os.listdir(d):
if f.endswith(ext):
image = data.imread(os.path.join(d, f))
if len(image.shape) > 2:
files.append(image)
file_names.append(os.path.join(d, f))
count = count + 1
return files
def __load_path(self, path):
directories = []
if os.path.isdir(path):
directories.append(path)
for elem in os.listdir(path):
if os.path.isdir(os.path.join(path, elem)):
directories = directories + self.__load_path(os.path.join(path, elem))
directories.append(os.path.join(path, elem))
return directories
def __load_data(self, directory, ext):
files = self.__load_data_from_dirs(self.__load_path(directory), ext)
return files
print("Executing the program")
launcher = Launcher()
launcher.show_denormalized_image_from_normalized_image()
print("/Executing the program")
The question
Do you know why I get a multicolor mosaic of pixels using this denormalization method?