如何将 png 转换为 python 的数据框?
How can I convert a png to a dataframe for python?
我为数字识别器 (https://www.kaggle.com/c/digit-recognizer/data) 训练了一个模型。输入数据是一个 csv 文件。文件中的每一行代表一个高 28 像素、宽 28 像素的图像,总共有 784 个像素。该模型已准备好使用,但我想知道如何为此输入创建测试数据?如果我有一张带有数字编号的图片,我怎样才能将它转换为 28 x 28 像素的数组格式。
我试过下面的代码,但它将图像背景渲染为黄色。 png图片是白底的,所以我不明白为什么会显示黄色。
import numpy as np
import cv2
import csv
import matplotlib.pyplot as plt
img = cv2.imread('./test.png', 0) # load grayscale image. Shape (28,28)
flattened = img.flatten() # flatten the image, new shape (784,)
row = flattened.reshape(28,28)
plt.imshow(row)
plt.show()
我为您准备了一个小示例,希望它能让您了解如何完成此任务:
我以这张图片为例:
完整脚本:
import numpy as np
import cv2
import csv
img = cv2.imread('./1.png', 0) # load grayscale image. Shape (28,28)
flattened = img.flatten() # flatten the image, new shape (784,)
flattened = np.insert(flattened, 0, 0) # insert the label at the beginning of the array, in this case we add a 0 at the index 0. Shape (785,0)
#create column names
column_names = []
column_names.append("label")
[column_names.append("pixel"+str(x)) for x in range(0, 784)] # shape (785,0)
# write to csv
with open('custom_test.csv', 'w') as file:
writer = csv.writer(file, delimiter=';')
writer.writerows([column_names]) # dump names into csv
writer.writerows([flattened]) # add image row
# optional: add addtional image rows
现在您拥有与示例中提供的相同的 csv 结构。
custom_test.csv 输出(缩短):
label;pixel0;pixel1;pixel2;pixel3;pixel4;pixel5;pixel6;pixel7;pixel ...
0;0;0;0;0;0;0;0;0;0;0;0....
编辑:
要使用 matplotlib 可视化扁平图像,您必须指定一个颜色图:
row = flattened.reshape(28,28)
plt.imshow(row, cmap='gray') # inverse grayscale is possible with: cmap='gray_r'
我为数字识别器 (https://www.kaggle.com/c/digit-recognizer/data) 训练了一个模型。输入数据是一个 csv 文件。文件中的每一行代表一个高 28 像素、宽 28 像素的图像,总共有 784 个像素。该模型已准备好使用,但我想知道如何为此输入创建测试数据?如果我有一张带有数字编号的图片,我怎样才能将它转换为 28 x 28 像素的数组格式。
我试过下面的代码,但它将图像背景渲染为黄色。 png图片是白底的,所以我不明白为什么会显示黄色。
import numpy as np
import cv2
import csv
import matplotlib.pyplot as plt
img = cv2.imread('./test.png', 0) # load grayscale image. Shape (28,28)
flattened = img.flatten() # flatten the image, new shape (784,)
row = flattened.reshape(28,28)
plt.imshow(row)
plt.show()
我为您准备了一个小示例,希望它能让您了解如何完成此任务:
我以这张图片为例:
完整脚本:
import numpy as np
import cv2
import csv
img = cv2.imread('./1.png', 0) # load grayscale image. Shape (28,28)
flattened = img.flatten() # flatten the image, new shape (784,)
flattened = np.insert(flattened, 0, 0) # insert the label at the beginning of the array, in this case we add a 0 at the index 0. Shape (785,0)
#create column names
column_names = []
column_names.append("label")
[column_names.append("pixel"+str(x)) for x in range(0, 784)] # shape (785,0)
# write to csv
with open('custom_test.csv', 'w') as file:
writer = csv.writer(file, delimiter=';')
writer.writerows([column_names]) # dump names into csv
writer.writerows([flattened]) # add image row
# optional: add addtional image rows
现在您拥有与示例中提供的相同的 csv 结构。
custom_test.csv 输出(缩短):
label;pixel0;pixel1;pixel2;pixel3;pixel4;pixel5;pixel6;pixel7;pixel ...
0;0;0;0;0;0;0;0;0;0;0;0....
编辑: 要使用 matplotlib 可视化扁平图像,您必须指定一个颜色图:
row = flattened.reshape(28,28)
plt.imshow(row, cmap='gray') # inverse grayscale is possible with: cmap='gray_r'