C/C++ 图像二进制存储与读取
来源:互联网 发布:mac终端bash 编辑:程序博客网 时间:2024/05/24 15:40
本系列文章由 @yhl_leo 出品,转载请注明出处。
文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792
在深度学习时,制作样本数据集时,需要产生和读取一些二进制图像的数据集,如MNIST,CIFAR-10等都提供了适合C语言的二进制版本。
以CIFAR-10的数据集为例,官网上有两段关键的介绍:
二进制版本数据集格式为(图像大小为32x32
):
<1 x label><3072 x pixel>...<1 x label><3072 x pixel>
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.
由此,绘制一个简图:
根据图像大小32x32 = 1024
,不难知道,每个颜色值存储为1 byte
,因此,对于单个图像的二进制存储与读取(先不管RGB颜色存储顺序),找了一张32x32
的彩色lena图像,如下实现:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include "cv.h"#include "highgui.h"using namespace cv;using namespace std;void main(){ FILE *fpw = fopen( "E:\\patch.bin", "wb" ); if ( fpw == NULL ) { cout << "Open error!" << endl; fclose(fpw); return; } Mat image = imread("E:\\lena32.jpg"); if ( !image.data || image.channels() != 3 ) { cout << "Image read failed or image channels isn't equal to 3." << endl; return; } // write image to binary format file int labelw = 1; int rows = image.rows; int cols = image.cols; fwrite( &labelw, sizeof(char), 1, fpw ); char* dp = (char*)image.data; for ( int i=0; i<rows*cols; i++ ) { fwrite( &dp[i*3], sizeof(char), 1, fpw ); fwrite( &dp[i*3+1], sizeof(char), 1, fpw ); fwrite( &dp[i*3+2], sizeof(char), 1, fpw ); } fclose(fpw); // read image from binary format file FILE *fpr = fopen( "E:\\patch.bin", "rb" ); if ( fpr == NULL ) { cout << "Open error!" << endl; fclose(fpr); return; } int labelr(0); fread( &labelr, sizeof(char), 1, fpr ); cout << "label: " << labelr << endl; Mat image2( rows, cols, CV_8UC3, Scalar::all(0) ); char* pData = (char*)image2.data; for ( int i=0; i<rows*cols; i++ ) { fread( &pData[i*3], sizeof(char), 1, fpr ); fread( &pData[i*3+1], sizeof(char), 1, fpr ); fread( &pData[i*3+2], sizeof(char), 1, fpr ); } fclose(fpr); imshow("1", image2); waitKey(0); }
运行结果如下:
再看图片属性:
与官网上的大小3073
一致,那么这么存取应该没问题。
严格按照官网的RGB通道分别存储,略作修改就可以实现:
/* for ( int i=0; i<rows*cols; i++ ) { fwrite(&dp[i*3], sizeof(char), 1, fpw); fwrite(&dp[i*3+1], sizeof(char), 1, fpw); fwrite(&dp[i*3+2], sizeof(char), 1, fpw); }*/ for ( int i=0; i<rows*cols; i++ ) fwrite(&dp[i*3+2], sizeof(char), 1, fpw); // R for ( int i=0; i<rows*cols; i++ ) fwrite(&dp[i*3+1], sizeof(char), 1, fpw); // G for ( int i=0; i<rows*cols; i++ ) fwrite(&dp[i*3], sizeof(char), 1, fpw); // B
存储和读取多张图片方法类似,这里就不做介绍。
本文已同步于GitHub:yhlleo/image2binarytest
- C/C++ 图像二进制存储与读取
- C/C++ 图像二进制存储与读取
- 存储、读取二进制的图像
- C 二进制读取文件
- 读取图像数据的C实现(.raw存储格式)
- 二进制读取文件内容[C#]
- C语言读取二进制文件大小
- C 语言读取二进制文件大小
- 【C/C++】unsigned与二进制
- 从二进制数据流中构造GDAL可以读取的图像数据(C#)
- .NET二进制图片存储与读取
- system verilog与c语言接口读取yuv图像序列
- C/C++二进制读取文件总结
- 位图BitMap图像的读取与存储
- 位图BitMap图像的读取与存储
- 位图BitMap图像的读取与存储
- 位图BitMap图像的读取与存储
- 【数字图像处理】<纯C++>读取、裁剪、缩放、旋转和存储8位bmp灰度图像
- 我不知道的
- 627 A codeforces
- android自定义通用下拉刷新
- java int short long float double 大整理
- HBase分布式安装
- C/C++ 图像二进制存储与读取
- 51nod1613翻硬币规律!
- 关于xml类型的背景文件的编写
- Java的IO流—思维导图
- 【POJ2778】 DNA Sequence AC自动机+矩阵快速幂
- 操作系统原理
- hdu2673
- idea 15 mybatis plus 破解过程
- fatal error C1083: 无法打开包括文件:“SDKDDKVer.h”