您的位置:首页 > 理论基础 > 计算机网络

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可以按照你的实际数据更改



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: