opencv imencode和imdecode使用,用于网络传输图片
2017-11-19 09:51
579 查看
这是C++版本的。程序首先读入一个图片。然后encode,之后把encode后的内容写入文件(实际应用可以发送到网络)。
第二步,从文件读取encode的内容。然后解码decode。转换为mat格式,显示出来。
[cpp]view plain
copy
print?
<span style="font-size: 18px;">#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace boost::filesystem;
namespace newfs = boost::filesystem;
using namespace cv;
int main(int argc, char ** argv)
{
cv::Mat img_encode;
img_encode = imread("../res/test.png", CV_LOAD_IMAGE_COLOR);
//encode image and save to file
std::vector<uchar> data_encode;
imencode(".png", img_encode, data_encode);
std::string str_encode(data_encode.begin(), data_encode.end());
path p("../res/imgencode_cplus.txt");
newfs::ofstream ofs(p);
assert(ofs.is_open());
ofs << str_encode;
ofs.flush();
ofs.close();
//read image encode file and display
newfs::fstream ifs(p);
assert(ifs.is_open());
std::stringstream sstr;
while(ifs >> sstr.rdbuf());
ifs.close();
Mat img_decode;
std::string str_tmp = sstr.str();
std::vector<uchar> data(str_tmp.begin(), str_tmp.end());
img_decode = imdecode(data, CV_LOAD_IMAGE_COLOR);
imshow("pic",img_decode);
cvWaitKey(10000);
return 0;
}</span>
#include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <iostream> #include <sstream> #include <string> #include <opencv2/opencv.hpp> #include <vector> using namespace boost::filesystem; namespace newfs = boost::filesystem; using namespace cv; int main(int argc, char ** argv) { cv::Mat img_encode; img_encode = imread("../res/test.png", CV_LOAD_IMAGE_COLOR); //encode image and save to file std::vector<uchar> data_encode; imencode(".png", img_encode, data_encode); std::string str_encode(data_encode.begin(), data_encode.end()); path p("../res/imgencode_cplus.txt"); newfs::ofstream ofs(p); assert(ofs.is_open()); ofs << str_encode; ofs.flush(); ofs.close(); //read image encode file and display newfs::fstream ifs(p); assert(ifs.is_open()); std::stringstream sstr; while(ifs >> sstr.rdbuf()); ifs.close(); Mat img_decode; std::string str_tmp = sstr.str(); std::vector<uchar> data(str_tmp.begin(), str_tmp.end()); img_decode = imdecode(data, CV_LOAD_IMAGE_COLOR); imshow("pic",img_decode); cvWaitKey(10000); return 0; }
使用python的例子。
[python]
view plain
copy
print?
<span style="font-size: 18px;">import sys
import cv2
import numpy as np
def img_endecode( img):
#type img: cv::mat
#encode image from cv::mat
img_encode = cv2.imencode('.png', img)[1]
data_encode = np.array(img_encode)
str_encode = data_encode.tostring()
#save to file
fw = open('img_encode.txt', 'w')
fw.write(str_encode)
fw.flush
fw.close
#decode and display
nparr = np.fromstring(str_encode, np.uint8)
img_decode = cv2.imdecode(nparr, 1)
cv2.imshow("img_decode", img_decode)
cv2.waitKey(10000)</span>
import cv2 import numpy as np img = cv2.imdecode(np.fromstring(res.content, np.uint8), cv2.CV_LOAD_IMAGE_CLOLOR) #当然,如果是灰度图,后面的flag可以按照你的实际数据更改
相关文章推荐
- [OpenCV开发]OpenCV图像编码和解码 imencode和imdecode使用,用于网络传输图片
- 利用Opencv提供的imencode和imdecode进行图像视频传输(发送端支持Linux和Windows双系统)
- 使用volley实现简单的操作,json传输,网络图片显示
- 减小网络传输图片大小,使用gzip进行压缩
- 百度笔试题2005题目大致是这样的: 第一部分选择题: 有几道网络相关的题目,巨简单,比如第一题是TCP、RIP、IP、FTP中哪个协议是传输层的......。有一道linux的 chown使用题目。其他的全是数据结构的题目!什么链,表
- 使用Java实现网络传输数据的压缩
- VC中使用Socket网络文件传输结构
- 使用SerialPort传输图片文件的实验
- 使用HttpHandlers异步传输图片
- 使用OpenCV实现WebCam摄像头保存JPEG图片
- C#.NET通过Socket实现平行主机之间网络通讯(含图片传输的Demo演示)
- [原]网络上普遍使用的图片预加载技术(一)
- .NET(C#)基于Socket编程实现平行主机之间网络通讯有图片传输的Demo演示
- 使用http协议下载网络图片
- VC++中使用Socket网络文件传输
- 使用程序保存网页中的图片 网络读取图片
- 使用php4加速网络传输
- 使用php4加速网络传输
- 使用2.0中的压缩流减少网络传输量
- 图片从数据流转换为字符型,方便网络传输 base64转换