您的位置:首页 > 其它

二进制图片保存为jpg文件

2016-12-02 11:23 330 查看
在深度学习时,制作样本数据集时,需要产生和读取一些二进制图像的数据集,如MNISTCIFAR-10等都提供了适合C语言的二进制版本。

以CIFAR-10的数据集为例,官网上有两段关键的介绍:

二进制版本数据集格式为(图像大小为
32x32
):

<1 x label><3072 x pixel>
...
<1 x label><3072 x pixel>
1
2
3

In 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];

   
4000
                    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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: