Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9139721
  • 博文数量: 1725
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19840
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1725)

文章存档

2024年(1)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: Android平台

2017-12-04 19:48:16

http://blog.csdn.net/tt_ren/article/details/53227900

程序首先读入一个图片。然后encode,之后把encode后的内容写入文件(实际应用可以发送到网络)。
第二步,从文件读取encode的内容。然后解码decode。转换为mat格式,显示出来。



点击(此处)折叠或打开

  1. #include <boost/filesystem.hpp>
  2. #include <boost/filesystem/fstream.hpp>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6. #include <opencv2/opencv.hpp>
  7. #include <vector>
  8.   
  9. using namespace boost::filesystem;
  10. namespace newfs = boost::filesystem;
  11. using namespace cv;
  12.   
  13. int main(int argc, char ** argv)
  14. {
  15.     cv::Mat img_encode;
  16.     img_encode = imread("../res/test.png", CV_LOAD_IMAGE_COLOR);
  17.   
  18.     //encode image and save to file
  19.     std::vector<uchar> data_encode;
  20.     imencode(".png", img_encode, data_encode);
  21.     //同样一张图片, 用BMP=10.6K  PNG=39.2K  JPG=9K. 网络不好建议用JPG, 追求清晰和带宽用PNG
  22.     std::string str_encode(data_encode.begin(), data_encode.end());
  23.   
  24.     path p("../res/imgencode_cplus.txt");
  25.     newfs::ofstream ofs(p);
  26.     assert(ofs.is_open());
  27.     ofs << str_encode;
  28.     ofs.flush();
  29.     ofs.close();
  30.   
  31.     //read image encode file and display
  32.     newfs::fstream ifs(p);
  33.     assert(ifs.is_open());
  34.     std::stringstream sstr;
  35.     while(ifs >> sstr.rdbuf());
  36.     ifs.close();
  37.   
  38.     Mat img_decode;
  39.     std::string str_tmp = sstr.str();
  40.     std::vector<uchar> data(str_tmp.begin(), str_tmp.end());
  41.     img_decode = imdecode(data, CV_LOAD_IMAGE_COLOR);
  42.     imshow("pic",img_decode);
  43.     cvWaitKey(10000);
  44.   
  45.     return 0;
  46. }

点击(此处)折叠或打开

  1. //python
  2. import sys
  3. import cv2
  4. import numpy as np
  5. def img_endecode( img):
  6.     #type img: cv::mat
  7.     #encode image from cv::mat
  8.     img_encode = cv2.imencode('.png', img)[1]
  9.     data_encode = np.array(img_encode)
  10.     str_encode = data_encode.tostring()
  11.       
  12.     #save to file
  13.     fw = open('img_encode.txt', 'w')
  14.     fw.write(str_encode)
  15.     fw.flush
  16.     fw.close
  17.       
  18.     #decode and display
  19.     nparr = np.fromstring(str_encode, np.uint8)
  20.     img_decode = cv2.imdecode(nparr, 1)
  21.     cv2.imshow("img_decode", img_decode)
  22.     cv2.waitKey(10000)


阅读(8359) | 评论(0) | 转发(0) |
0

上一篇:GTK 中自定义信号

下一篇:python + mysql + gtk

给主人留下些什么吧!~~