Tensorflow C++ png图片读取

  • Tensorflow C++ 内部提供PNG图片解码的操作,其定义的头文件为
#include "tensorflow/core/lib/png/png_io.h"

// 解码函数
// CommonInitDecode
// CommonFinishDecode
  • 读取PNG并解码
  // 读取原始PNG数据
  std::string pngString; 
  std::fstream png_in("xxxxx.png");
  png_in.seekg(0, std::ios::end);
  pngString.resize(png_in.tellg());
  png_in.seekg(0, std::ios::beg);   
  png_in.read(&pngString[0], pngString.size());  
  png_in.close();  

  // 解码
  tensorflow::png::DecodeContext context;
  CommonInitDecode(pngString, 3 /*RGB*/, 8 /*uint8*/, &context);
  char* image_buffer = new char[3*context.width*context.height];
  CommonFinishDecode(tensorflow::bit_cast<png_byte*>(image_buffer),
        3*context.width /*stride*/, &context);
  // ... use image_buffer
  delete[] image_buffer;
  • 注意:CommonInitDecode函数后,context对象的data成员,context.data不是PNG解码后的数据。而是,去掉了PNG头、标志位等等的原始数据,不要使用该数据进行处理,否则可能会导致内存泄露(非法访问)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值