mnist数据集是以二进制形式保存的,这里借助OpenCV把mnist数据集转换成图片格式。转换程序如下:
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
//mnist数据存放路径
string train_test_image[2] = { "D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_train_lmdb\\train-images.idx3-ubyte",
"D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_test_lmdb\\t10k-images.idx3-ubyte" };
string train_test_label[2] = { "D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_train_lmdb\\train-labels.idx1-ubyte",
"D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_test_lmdb\\t10k-labels.idx1-ubyte" };
int label_num[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//图片保存路径
string dir[2] = { "D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_train_image\\",
"D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_test_image\\" };
for (int iter = 0; iter < 2; iter++)
{
//读取label
ifstream fin_label(train_test_label[iter], ios::binary);
vector<int> label;
int magic_number;
fin_label.read((char *)(&magic_number), sizeof(magic_number));
int number_items;
fin_label.read((char *)(&number_items), sizeof(number_items));
while (!fin_label.eof())
{
char label_tmp;
fin_label.read((char *)&label_tmp, sizeof(label_tmp));
label.push_back(label_tmp);
}
//读取图片
vector<Mat> image;
int width = 28, height = 28;
ifstream fin_image(train_test_image[iter], ios::binary);
int magic_number1;
fin_image.read((char *)(&magic_number1), sizeof(magic_number1));
int number_images;
fin_image.read((char *)(&number_images), sizeof(number_images));
int num_rows;
fin_image.read((char *)(&num_rows), sizeof(num_rows));
int num_columns;
fin_image.read((char *)(&num_columns), sizeof(num_columns));
while (!fin_image.eof())
{
unsigned char tmp;
Mat image_tmp(width, height, CV_8UC1);
for (int r = 0; r < image_tmp.rows; r++)
{
for (int c = 0; c < image_tmp.cols; c++)
{
fin_image.read((char *)&tmp, sizeof(tmp));
image_tmp.at<uchar>(r, c) = tmp;
}
}
image.push_back(image_tmp);
}
for (int i = 0; i < label.size(); i++)
{
char clabel[10];
sprintf_s(clabel, "%d", label[i]);
string slabel = clabel;
char clabel_num[10];
sprintf_s(clabel_num, "%d", label_num[label[i]]);
string slabel_num = clabel_num;
string name = dir[iter] + slabel + "_" + slabel_num + ".jpg";
imwrite(name, image[i]);
//显示图片
imshow("mnist", image[i]);
waitKey(100);
label_num[label[i]]++;
}
}
return 0;
}
下边使用OpenCV把训练图片的0~9的前10个图片显示出来:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat MNIST = Mat(Size(289, 289), CV_8UC1, Scalar::all(255));
string mnistPath = "D:\\Software\\Caffe\\caffe-master\\data\\mnist\\mnist_train_image\\";
string mnistImagePath;
stringstream str;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
str.clear();
str << i;
string stri, strj;
str >> stri;
str.clear();
str << j;
str >> strj;
mnistImagePath = mnistPath + stri + "_" + strj + ".jpg";
Mat image = imread(mnistImagePath, 0);
Mat roi = MNIST(Rect(j * 28 + j, i * 28 + i, 28, 28));
addWeighted(roi, 0, image, 1, 0, roi);
imshow("mnist", MNIST);
}
waitKey();
}
}
执行结果: