美文网首页
opencv图像压缩与解压(imencode&imdecode)

opencv图像压缩与解压(imencode&imdecode)

作者: 承诺一时的华丽 | 来源:发表于2021-06-23 17:05 被阅读0次

    问题

    在工程应用中,通常有需要用网络传输图片的需求,考虑网络带宽的限制,无法直接将原始图片进行传输。

    解决

    使用opencv中的imencode与imdecode函数进行图像压缩与解压

    imencode

    Mat img;
    int quality = 50; //压缩比率0~100
    vector<uint8_t> imageData;
    vector<int> compress_params;
    compress_params.push_back(IMWRITE_JPEG_QUALITY);
    compress_params.push_back(quality);
    imencode(".jpg", frame, imageData, compress_params);
    

    imdecode

    vector<uint8_t> p_data = imageData;
    Mat image = imdecode(p_data,CV_LOAD_IMAGE_COLOR);
    //Mat image = imdecode(p_data,-1);
    包含头文件
    #include<opencv2/opencv.hpp>
    #include<opencv2/imgcodecs.hpp>
    #include<opencv2/imgcodecs/legacy/constants_c.h> //出现CV_LOAD_IMAGE_COLOR未声明错误,通常是这个头文件未被包含
    

    相关文章

      网友评论

          本文标题:opencv图像压缩与解压(imencode&imdecode)

          本文链接:https://www.haomeiwen.com/subject/qxwbyltx.html