关于如何打开一张jpg图片文件并存放到vector unsigned char中的讨论

如题,打开一个图片文件(jpg),然后存放到vector<unsigned char>中,要怎么做啊,请给出代码,谢谢!!!!下面是我自己写的,但是好像老是报错。。。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
//@函数功能:将图片文件转换成vector<unsigned char>
void  photo_to_uvchar( const  char * photo_path, vector<unsigned  char >& uvchar)
{
 
     fstream fs(  "D:\\新文档代码\\test_photo.jpg " ) ;  // 创建个文件流对象,并打开"test_photo.jpg"
     stringstream ss ;         // 创建字符串流对象
     ss << fs.rdbuf()     ;      // 把文件流中的字符输入到字符串流中
     std::string src = ss.str() ;     // 获取流中的字符串   
     uvchar.resize( src.size()+1 );
     unsigned  char * pDst = &uvchar[0];
     unsigned  char * pSrc =  reinterpret_cast <unsigned  char *>( const_cast < char *>(src.c_str()));
     memcpy ( pDst, pSrc, src.size() );
}


      <div marginwidth="0" marginheight="0" scrolling="no" width="100%">
          <div>
            <div id="kp_box_87" data-pid="87" data-track-view="{&quot;mod&quot;:&quot;kp_popu_87-702&quot;,&quot;con&quot;:&quot;,,&quot;}" data-track-click="{&quot;mod&quot;:&quot;kp_popu_87-702&quot;,&quot;con&quot;:&quot;,,&quot;}"><script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

          </div>
      </div>
    <!-- <div class="question_wrap"><span>问题点数:</span><em>分</em></div> -->
  </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++将Labelme JSON文件的imagedata转换为JPG格式的图片,可以按照以下步骤进行: 1. 打开Labelme的JSON文件,找到imagedata所在的字段。 2. 将imagedata字段的Base64编码数据解码为二进制数据。 3. 将二进制数据保存为JPG格式的图片文件。 具体的C++代码示例如下: ```c++ #include <fstream> #include <iostream> #include <string> #include <vector> #include <opencv2/opencv.hpp> #include <boost/algorithm/string.hpp> #include <boost/algorithm/string/replace.hpp> using namespace std; using namespace cv; int main() { // 读取JSON文件 ifstream ifs("example.json", ios::in); string json_str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>()); ifs.close(); // 找到imagedata字段 string imagedata_start = "\"imageData\":\""; string imagedata_end = "\",\"shapes\":"; size_t pos1 = json_str.find(imagedata_start); size_t pos2 = json_str.find(imagedata_end); string imagedata = json_str.substr(pos1 + imagedata_start.length(), pos2 - pos1 - imagedata_start.length()); // 替换Base64编码的特殊字符 boost::replace_all(imagedata, "-", "+"); boost::replace_all(imagedata, "_", "/"); int padding = 4 - imagedata.length() % 4; if (padding < 4) imagedata.append(padding, '='); // 解码Base64编码 vector<unsigned char> im_bytes = base64_decode(imagedata); // 将二进制数据转换为OpenCV图像对象 Mat im = imdecode(im_bytes, IMREAD_COLOR); // 保存为JPG格式的图片文件 imwrite("example.jpg", im); return 0; } ``` 在上面的代码,我们首先读取了JSON文件,并找到了imagedata字段的Base64编码数据。然后,我们使用Boost库将Base64编码的特殊字符替换为标准的Base64字符,并解码为二进制数据。接下来,我们使用OpenCV库将二进制数据转换为图像对象,并保存为JPG格式的图片文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值