使用显示非规范化图像Matplotlib.Pyplot文件产生多色像素马赛克:为什么?

2024-05-08 14:07:27 发布

您现在位置:Python中文网/ 问答频道 /正文

上下文

我编写了一个程序,它接收由我的另一个程序规范化的图像输入。你知道吗

输入图像为:

enter image description here

程序输出图像的标准化版本。规范化方法在下面进一步显示。你知道吗

问题

输出错误地包含在像素的多色马赛克中。给定上述输入,输出为(缩放):

enter image description here

预期结果应为上述平面。你知道吗

最小可执行源(&E)

  1. 在您选择的目录路径中,将输入图像放在您选择的名为的目录中。你知道吗
  2. 在相同的目录路径中,创建另一个名为different的目录。它将包含由程序创建的输出图像。你知道吗
  3. 复制并粘贴下面的代码,将包含目录路径的两个常量替换为自定义的两个目录路径。你知道吗
  4. 执行代码
  5. 看一下输出图像:您应该看到与我在这个问题中显示的输出图像相同的输出图像!你知道吗

在以下代码中:

  • 您可以看到反规范化方法
  • 方法__plot_images_predicted_for_testing用Matplotlib.Pyplot文件从RAM输入图像中包含输出图像的窗口
  • 通过__fetch_the_normalized_image获取输入图像,__load_XYZ将输入图像从磁盘加载到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")

问题

你知道我为什么用这种非规范化方法得到一个多色的像素马赛克吗?你知道吗


Tags: thepathfrom图像imageself目录for

热门问题