C/C++ 图像二进制存储与读取
本系列文章由 @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++ 图像二进制存储与读取的更多相关文章
- SQLite数据库如何存储和读取二进制数据
SQLite数据库如何存储和读取二进制数据 1. 存储二进制数据 SQLite提供的绑定二进制参数接口函数为: int sqlite3_bind_blob(sqlite3_stmt*, int, co ...
- .NET中二进制图片的存储与读取
判断HttpContext是否为空: string configPath = "img/defaultPhoto.png"; if (HttpContext.Current != ...
- .Net下二进制形式的文件存储与读取
.Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ...
- [TFRecord格式数据]利用TFRecords存储与读取带标签的图片
利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...
- 在VC下采用ADO实现BLOB(Binary)数据的存储,读取,修改,删除。
在VC下采用ADO实现BLOB(Binary)数据的存储,读取,修改,删除. 作者:邵盛松 2009-09-05 前言 1关于的BLOB(Binary)数据的存储和读取功能主要参考了MSDN上的一篇& ...
- MFC用串行化实现文档存储和读取功能
在面向对象的程序设计中,一般都是用二进制文件来保存文档资料.在VC++中控制和使用文件流的方法很多,MFC程序设计中常用的有两种方法:用CFile对象存储和读取文件:利用串行化存取文件.其中用CFil ...
- Python3基础 pickle.dump和load 对一个对象进行序列化存储及读取
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- numpy中文件的存储和读取-嵩天老师笔记
numpy中csv文件的存储和读取 CSV文件:(Comma‐Separated Value, 逗号分隔值) 一维和二维数组 存储 np.savetxt(frame,array,fmt='%.18e' ...
- Numpy用于数组数据的存储和读取
Python的Numpy模块可用于存储和读取数据: 1.将一个数组存储为二进制文件 Numpy.save:将一个数组以.npy的格式保存为二进制文件 调用格式:numpy.save(file, arr ...
随机推荐
- Android - 找到当前类的Context
找到当前类的Context 本文地址: http://blog.csdn.net/caroline_wendy 假设是在onContinueCreate或onCreate中, 直接使用this, 就代 ...
- 高效使用hive
工作中常常使用hive.熟练使用hvie的配置參数能够更加高效的使用Hive Hive option: hive -f script.hql : 从文件script.hql中的读取hql运行 hi ...
- HDU1846(巴什博奕)
Brave Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- luogu2518 [HAOI2010] 计数
题目大意 给出一个数字$n$,求满足下列条件的数$x$的个数: $x<n$ 对于来自于$x$十进制各个数位上的非零数字,它们的种类与个数都与$n$的相同. 思路 入手点 设$n$有$t$位数字, ...
- vim相关资料
http://www.360doc.cn/article/15064667_402846667.html http://blog.jobbole.com/86809/ http://blog.csdn ...
- angular4 select 绑定(ngModel)对象
欢迎加入前端交流群交流知识&&获取视频资料:749539640 <h1>My Application</h1> <select [(ngModel)]=& ...
- LeetCode.2-两个数字相加(Add Two Numbers)
这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...
- pythonOCC版 瓶子代码
#!/usr/bin/env python # -*- coding:utf-8 -*- ##Copyright 2009-2015 Thomas Paviot (tpaviot@gmail.com) ...
- CI中的分页
根据MVC的思想,分页是需要传数据到模型中,把页码传过去,在模型中根据页码分配: 更多分页类函数可以通过CI手册的分页类查看: $this -> load ->library('pagin ...
- 【SQL】SELECT 语句
1.1 SELECT基本语法: Select * |{[distinct]colum|expression [alias],…} from table; 1.2 查询当前用户所有在用的表及视图: HR ...