mnist数据集是深度学习入门的一个很经典的手写体数字的数据集,我们跑的第一个深度学习网络往往就是mnist数据集,但是它的数据格式比较特殊,具体的格式及介绍见官网http://yann.lecun.com/exdb/mnist/
今天就写个程序,把mnist数据集中的数据转换为图像,这样我们就可以利用这些数据把图像转换为像caffe、Tensorflow等要求的格式作为练习,以后对自己的学习任务的样本也可以这样做。
废话不多说了,直接上代码,代码主要参考的是caffe中的convert_mnist_data.cpp文件中的转换方法。
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;
//把大端数据转换为我们常用的小端数据
uint32_t swap_endian(uint32_t val)
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
return (val << 16) | (val >> 16);
}
void readAndSave(const string& mnist_i