【发布时间】:2017-10-24 11:47:32
【问题描述】:
我正在开发一个程序来获取图像并将其展平,以便可以将其写入 CSV 文件。那部分有效。当我尝试从 CSV 文件读回该行时遇到问题。我尝试重建图像,但出现“ValueError:无法将大小为 0 的数组重塑为形状 (476,640,3)”的错误。我添加了 CSV 文件的示例输出。
import csv
import cv2
import numpy as np
from skimage import io
from matplotlib import pyplot as plt
image = cv2.imread('Li.jpg')
def process_images (img):
img = np.array(img)
img = img.flatten()
return img
def save_data(img):
dataset = open('dataset.csv', 'w+')
with dataset:
writer = csv.writer(dataset)
writer.writerow(img)
def load_data():
with open('dataset.csv', 'r') as processed_data:
reader = csv.reader(processed_data)
for row in reader:
img = np.array(row , dtype='uint8')
img = img.reshape(476,6, 3)
return img
def print_image_stats (img):
print (img)
print (img.shape)
print (img.dtype)
def rebuilt_image(img):
img = img.reshape(476,640,3)
plt.imshow(img)
plt.show()
return img
p_images = process_images(image)
print_image_stats(p_images)
r_image = rebuilt_image(p_images)
print_image_stats(r_image)
save_data(p_images)
loaded_data = load_data()
#r_image = rebuilt_image(load_data)
#print_image_stats(r_image)
【问题讨论】:
-
"... 写入 CSV 文件。这部分有效。" "dataset.csv" 中的文本是什么样的?你能从那个文件中添加一个 sn-p,比如前 10 行,到问题中吗?
-
我从 CSV 文件中添加了一个示例。目前有一个图像被展平为一维数组。
-
使用熊猫。 Numpy 助手