python - 使用 Matplotlib.Pyplot 显示非规范化图像会导致多色像素马赛克:为什么?
问题描述
语境
我编写了一个程序,该程序接收由我的另一个程序规范化的图像。
输入图像为:
程序输出图像的标准化版本。标准化方法如下所示。
问题
输出错误地包含在像素的多色马赛克中。给定上述输入,输出为(放大):
预期的结果应该是上面的平面。
最小和可执行的源
- 在您选择的目录路径中,将输入图像放在您选择的目录中。
- 在这个相同的目录路径中,创建另一个名称不同的目录。它将包含程序创建的输出图像。
- 复制并粘贴以下代码,将包含目录路径的两个常量替换为您自定义的两个目录路径。
- 执行代码
- 看一下输出图像:您应该看到与我在这个问题中显示的输出图像相同的输出图像!
在下面的代码中:
- 可以看到非规范化方法
- 这些方法
__plot_images_predicted_for_testing
使用 Matplotlib.Pyplot 从 RAM 输入图像创建一个包含输出图像的窗口 - 输入图像是通过将输入图像从磁盘加载到 RAM 来获取
__fetch_the_normalized_image
的。__load_XYZ
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")
问题
你知道为什么我使用这种非规范化方法得到多色像素马赛克吗?
解决方案
推荐阅读
- r - 有没有办法将 gtsummary 中引用类别中的“_”更改为“Ref”或任何其他字符串?
- python - 如何生成定制的黑白位图
- button - 如何使用 kotlin 在图像视图上放置一个按钮
- apache - 替换 htaccess 中不存在的图像
- swiftui - SwiftUI 应用程序显示领域变化但不是新对象
- php - 如何在laravel中对多个withcount()列求和和排序
- javascript - 使用 reduce 过滤数组
- python-3.x - 使用python从html获取javascript变量和数据
- symfony - 在共享 2 个环境的机器上运行 Symfony 3.2 项目中的作曲家安装
- python - 在一个单元格中转换具有多个值的数据框