opencv imencode和imdecode使用,用于网络传输图片

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

#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的例子。

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)


  • 10
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
`cv2.imencode()` 和 `cv2.imdecode()` 是 OpenCV用于图像编码解码的函数。`cv2.imencode()` 用于将图像编码为特定格式的字节数组,而 `cv2.imdecode()` 则用于从内存缓冲区中解码图像。 具体来说,`cv2.imencode()` 将图像数据编码为指定格式的字节数组,以便于存储或传输。例如,我们可以将图像编码为 JPEG 格式的字节数组,并将其保存到文件或发送到远程服务器。其语法如下: ```python retval, buf = cv2.imencode(ext, img, params=None) ``` 其中,`ext` 是表示所需编码格式的扩展名,例如 `.jpg` 或 `.png`;`img` 是要编码的图像,必须是一个 NumPy 数组;`params` 是可选参数,用于设置编码器的参数。`retval` 是一个布尔值,表示编码是否成功,`buf` 是一个包含编码后数据的字节数组。 相应地,`cv2.imdecode()` 函数用于从内存缓冲区中解码图像数据,并将其转换为 OpenCV 中的图像格式。其语法如下: ```python img = cv2.imdecode(buf, flags) ``` 其中,`buf` 是一个包含图像数据的字节数组,`flags` 是一个整数值,表示解码图像时应该采用的解码方式。常用的解码方式有: - `cv2.IMREAD_COLOR`:读取彩色图像,默认值(1) - `cv2.IMREAD_GRAYSCALE`:以灰度模式读取图像(0) - `cv2.IMREAD_UNCHANGED`:读取包含 alpha 通道的图像(-1) 当函数成功解码图像时,它将返回一个 NumPy 数组,表示该图像。如果解码失败,则返回 `None`。 因此,`cv2.imencode()` 和 `cv2.imdecode()` 经常被用于将图像编码为字节数组,然后进行存储或传输

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值