MNIST数据集来自美国国家标准与技术研究所, National Institute of Standards and Technology (NIST)。训练集 (training set) 由来自 250 个不同人手写的数字构成,其中50%是高中学生,50%来自人口普查局 (the Census Bureau) 的工作人员。测试集(test set)也是同样比例的手写数字数据。
Training set images: train-images-idx3-ubyte.gz (9.9 MB, 解压后 47 MB, 包含 60,000 个样本)
Training set labels: train-labels-idx1-ubyte.gz (29 KB, 解压后 60 KB, 包含 60,000 个标签)
Test set images: t10k-images-idx3-ubyte.gz (1.6 MB, 解压后 7.8 MB, 包含 10,000 个样本)
Test set labels: t10k-labels-idx1-ubyte.gz (5KB, 解压后 10 KB, 包含 10,000 个标签)
图片是以字节的形式进行存储,下面是官网的数据集说明
1 2 3 4 5 6 7 8 9 10 11 12
TRAINING SET IMAGE FILE (train-images-idx3-ubyte): [offset] [type] [value] [description] 0000 32 bit integer 0x00000803(2051) magic number 0004 32 bit integer 60000 number of images 0008 32 bit integer 28 number of rows 0012 32 bit integer 28 number of columns 0016 unsigned byte ?? pixel 0017 unsigned byte ?? pixel ........ xxxx unsigned byte ?? pixel
if (!f.is_open()) { cout << "无法读取图像数据" << endl; exit(-1); } /* byte 0 - 3 : Magic Number(Not to be used) byte 4 - 7 : Total number of images in the dataset byte 8 - 11 : rows of each image in the dataset byte 12 - 15 : cols of each image in the dataset */ int magic_number = 0; int number_of_images = 0; int height = 0; int width = 0;
Mat train_images = Mat(number_of_images, height * width, CV_8UC1); for (int i = 0; i < number_of_images; i++) { //第几张图 for (int r = 0; r < height; ++r) { for (int c = 0; c < width; ++c) { unsignedchar temp = 0; f.read((char *)&temp, sizeof(temp)); train_images.at<uchar>(i, r * width + c) = (int)temp; if (i == 0) { Mat digit = Mat(height, width, CV_8UC1); digit.at<uchar>(r, c) = (int)temp; imwrite("digit.png", digit); //输出第一张图片 } } } }
if (!f.is_open()) { cout << "无法读取标签数据" << endl; exit(-1); } /* byte 0 - 3 : Magic Number(Not to be used) byte 4 - 7 : Total number of labels in the dataset */ int magic_number = 0; int number_of_labels = 0;