在深度学习时,制作样本数据集时,需要产生和读取一些二进制图像的数据集,如MNIST,CIFAR-10等都提供了适合C语言的二进制版本。
以CIFAR-10的数据集为例,官网上有两段关键的介绍:
二进制版本数据集格式为(图像大小为32x32):
<1 x label><3072 x pixel> ... <1 x label><3072 x pixel> 123In other words, the first byte is the label of the first image, which is a number in the range 0-9. The next 3072 bytes are the values of the pixels of the image. The first 1024 bytes are the red channel values, the next 1024 the green, and the final 1024 the blue. The values are stored in row-major order, so the first 32 bytes are the red channel values of the first row of the image.
由此,绘制一个简图:
具体代码如下:
char str_buffer[32*32];
string batchfilename = “data_batch_1.bin”; //二进制数据文件
ifstream data_file(batchfilename.c_str(), ios::in | ios::binary);
data_file.read(str_buffer, 32*32);
Mat iMat(32, 32, CV_8UC3);
for (int r = 0; r < 32; r++)
for (int c = 0; c < 32; c++)
{
iMat.at<Vec3b>(r,c)[0] = str_buffer[r*32 + c];
iMat.at<Vec3b>(r,c)[1] = str_buffer[r*32 + 1024 +c];
iMat.at<Vec3b>(r,c)[0] = str_buffer[r*32 + 2048+ c];
}
IplImage limage;
limage = IplImage(iMat);
cvSaveImage("data_batch_2.jpg", &limage);
data_file.close();