入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Tensorflow C++ png图片读取

创建时间:2018-03-23 投稿人: 浏览次数:212
  • 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头、标志位等等的原始数据,不要使用该数据进行处理,否则可能会导致内存泄露(非法访问)。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。