您的位置:首页 > 运维架构

OpenCV - 将图片的二进制信息写入到文件中

2017-02-10 15:47 232 查看
遇到的错误, 应该是配置问题, 请自行百度配置方式

// imageToDat.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"

//图像写入二进制(.dat)文件
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

#include <iostream>
#include <fstream>
using namespace std;

//////////////////////////////////////////////////////////////////////////
const string filename = "test.dat";
const string picname = "pic.jpg";
//////////////////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{
//打开文件
ofstream outfile;
outfile.open(filename.c_str(), ios::binary);
if (!outfile)
{
cerr << "failed to open the file : " << filename << endl;
return -1;
}

//打开图像
Mat srcImg = imread(picname);
if (srcImg.empty())
{
cerr << "failed to open the file : " << picname << endl;
return -1;
}

//写入二进制文件
for (int r = 0; r < srcImg.rows; r++)
outfile.write(reinterpret_cast<const char*>(srcImg.ptr(r)), srcImg.cols*srcImg.elemSize());
//outfile<<endl;
cout << "write to file ok!" << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv