二进制图片保存为jpg文件
2016-12-02 11:23
330 查看
在深度学习时,制作样本数据集时,需要产生和读取一些二进制图像的数据集,如MNIST,CIFAR-10等都提供了适合C语言的二进制版本。
以CIFAR-10的数据集为例,官网上有两段关键的介绍:
二进制版本数据集格式为(图像大小为
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();
以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();
相关文章推荐
- 将RAR压缩文件转换为JPG图片保存的牛B方法
- 通过文件二进制信息判断图片类型(png,jpg,gif)
- JPG,PNG等格式的图片转换的ICON保存为ICO文件
- C编写以二进制读取文件(任意格式eg :图片),保存到C语言数据格式的TXT文档
- C#——主进程负责向屏幕不间断输出四位随机数,子线程负责每隔 1 秒钟对当前计算机屏幕进行截屏并以 jpg 格式的图片保存文件。
- JPG,PNG等格式的图片转换的ICON保存为ICO文件
- 怎样恢复丢失的JPG图片做好的图片文件忘记保存了怎么办
- 用opencv1.0连续打开多张BMP文件、连续保存多张jpg图片
- 用VB将剪切板中的图片保存为JPG文件
- 把 Rational Rose 的图表保存为 JPG 图片文件
- 就是那个上传的。按实际上传时的文件名称保存(不在是test.jpg了)图片可以改大小,图
- 通过文件二进制信息判断图片类型(png,jpg,gif)
- MVC 保存二进制到数据库,并显示文件的方法(图片显示或下载文件)
- 如何将数据库二进制图片保存到硬盘上
- 在sql server里怎么用二进制存图片?包括office文件等等
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- 从剪贴板取出图片然后写上字保存到文件
- 从数据库中读取一个图片并保存为一个图片文件
- 从数据库中读取二进制文件,并将图片打印到页面上
- 将图片保存到XML文件中