首页 > 解决方案 > 为什么 numpy 文件存储嘈杂的图像?

问题描述

我正在编写一个代码来将大量图像存储在一个 numpy 文件中。当我运行它时,一切都很好,代码保存了 2 个 numpy 文件,但是当我尝试加载文件并查看里面的内容时,我得到了很多嘈杂的图像。

此数据用于训练我的网络

from __future__ import division
import scipy.misc
import scipy.ndimage
import cv2
import numpy as np
from glob import glob
import pickle
from PIL import Image
import os

def main():
    data_path_input = '80000/original/1/all'
    data_path_label = '80000/original/2/all'
    size_w = 160
    size_h = 120 
    _depth = 1
    channel = 3
    w_stride = 160
    h_stride = 120
    depth_stride = 1
    h5_path = '80000/original/h5'
    prepare_training_data(data_path_input,
                          data_path_label,                  
                          size_w,
                          size_h,
                          channel,
                          w_stride,
                          h_stride,
                          depth_stride,
                          h5_path)


def prepare_training_data(data_path_input, data_path_label, size_w, size_h, channel, w_stride, h_stride, depth_stride, h5_path):#train_data_name,

    test_image_tensor_lb = np.zeros((1, size_h, size_w, channel), np.float32)
    test_image_tensor_in = np.zeros((1, size_h, size_w, channel), np.float32)

    counter = 0

    video_label = sorted(glob(os.path.join(data_path_label, '*.png')))
    video_input = sorted(glob(os.path.join(data_path_input, '*.png')))

    assert len(video_label) == len(video_input)

    for i in range(200):
        if channel == 1:

            temp = scipy.misc.imread(video_label[i])
            _lb = rgb2y(temp)
            temp = scipy.misc.imread(video_input[i])
            _lb = rgb2y(temp)
            _pr = np.loadtxt("%s" % video_param[i])

        else:
            _lb = scipy.misc.imread("%s" % video_label[i])
            _in = scipy.misc.imread("%s" % video_input[i]) 


        image_lb = np.array(_lb, dtype='float') / 255.
        image_in = np.array(_in, dtype='float') / 255. 


        sub_label = np.array(image_lb)
        sub_input = np.array(image_in)  

        if np.var(sub_label) > 0.0035 * 0.0035:
            if channel == 1:
                test_image_tensor_lb[counter, :, :, 0] = sub_label
                test_image_tensor_in[counter, :, :, 0] = sub_input
                test_image_tensor_in[counter, :, :, 0] = sub_input
            else:
                test_image_tensor_lb[counter, :, :, :] = sub_label
                test_image_tensor_in[counter, :, :, :] = sub_input
                test_image_tensor_in[counter, :, :, :] = sub_input      

            counter += 1
            test_image_tensor_lb.resize((counter + 1, size_h, size_w, channel))
            test_image_tensor_in.resize((counter + 1, size_h, size_w, channel))

    scipy.misc.imsave('%s/a.png' %(h5_path,i), test_image_tensor_lb[2, :, :, :])
    np.save('%s/label.npy' %h5_path, test_image_tensor_lb)
    np.save('%s/input.npy' %h5_path, test_image_tensor_in)

当,在python环境中,我输入

import numpy as np
a=np.load('label.npy')
from PIL import Image
img = Image.fromarray(a[0,:,:,:], 'RGB')
img.show()

我得到嘈杂的图像。

我的原始图像:输入 我使用 img.show() 获得的图像:npy 文件中的图像

我该如何解决这个问题?

标签: pythonnumpyimage-processing

解决方案


推荐阅读