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

遇到的错误, 应该是配置问题, 请自行百度配置方式

// 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;
}

猜你喜欢

转载自blog.csdn.net/u012678352/article/details/54971197