使用OpenCV或Matplotlib/Pyplot可视化MNIST数据集 [英] Visualize MNIST dataset using OpenCV or Matplotlib/Pyplot
问题描述
我有MNIST数据集,我正在尝试使用pyplot将其可视化.数据集采用cvs
格式,其中每一行都是784像素的一幅图像.我想以pyplot
或opencv
的格式将其可视化为28 * 28图像格式.我正在尝试直接使用:
i have MNIST dataset and i am trying to visualise it using pyplot. The dataset is in cvs
format where each row is one image of 784 pixels. i want to visualise it in pyplot
or opencv
in the 28*28 image format. I am trying directly using :
plt.imshow(X[2:],cmap =plt.cm.gray_r, interpolation = "nearest")
但是我不能正常工作吗?关于如何处理此问题的任何想法.
but i its not working? any ideas on how should i approach this.
推荐答案
假定您具有此格式的CSV文件,该格式是MNIST数据集可用的格式
Assuming you have a CSV file with this format, which is a format the MNIST dataset is available in
label, pixel_1_1, pixel_1_2, ...
在这里,您可以使用Matplotlib然后使用OpenCV在Python中对其进行可视化
Here's how you can visulize it in Python with Matplotlib and then OpenCV
import numpy as np
import csv
import matplotlib.pyplot as plt
with open('mnist_test_10.csv', 'r') as csv_file:
for data in csv.reader(csv_file):
# The first column is the label
label = data[0]
# The rest of columns are pixels
pixels = data[1:]
# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype='uint8')
# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))
# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()
break # This stops the loop, I just want to see one
您可以从上方获取pixels
numpy数组,该数组为dtype='uint8'
(无符号8位整数),形状为28 x 28,并使用cv2.imshow()
You can take the pixels
numpy array from above which is of dtype='uint8'
(unsigned 8-bits integer) and shape 28 x 28 , and plot with cv2.imshow()
title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
这篇关于使用OpenCV或Matplotlib/Pyplot可视化MNIST数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!