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;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值