<p>假设您有一个具有此格式的CSV文件,即MNIST数据集可用的格式</p>
<pre><code>label, pixel_1_1, pixel_1_2, ...
</code></pre>
<p>下面是如何使用Matplotlib和OpenCV在Python中可视化它的方法</p>
<h2>Matplotlib/Pyplot</h2>
<pre><code>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
</code></pre>
<p><a href="https://i.stack.imgur.com/gYsJp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gYsJp.png" alt="enter image description here"/></a></p>
<h2>开放简历</h2>
<p>可以从上面获取<code>pixels</code>numpy数组,该数组是<code>dtype='uint8'</code>(无符号8位整数)和形状28 x 28,并用<code>cv2.imshow()</code>绘制</p>
<pre><code> title = 'Label is {label}'.format(label=label)
cv2.imshow(title, pixels)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>