首页 > 解决方案 > 使用 Matplotlib.Pyplot 显示非规范化图像会导致多色像素马赛克:为什么?

问题描述

语境

我编写了一个程序,该程序接收由我的另一个程序规范化的图像。

输入图像为:

在此处输入图像描述

程序输出图像的标准化版本。标准化方法如下所示。

问题

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

在此处输入图像描述

预期的结果应该是上面的平面。

最小和可执行的源

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

在下面的代码中:

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")

问题

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

标签: pythonmatplotlib

解决方案


推荐阅读