opencv-imencode把Mat转换成vector

 

#include<opencv2/opencv.hpp>
#include<iostream>
#include  <vector>
#include "opencv2/imgcodecs/legacy/constants_c.h" 


int main(int argc, char** argv) {

    cv::Mat src(5, 4, CV_8UC3, cv::Scalar(10, 20, 30));
    std::vector<unsigned char> vec;
    cv::imencode(".jpg", src, vec);//把Mat转换成vector
    //参数1:按什么图片格式编码

    
    cv::waitKey(0);
    return 0;
}

 

#include<opencv2/opencv.hpp>
#include<iostream>
#include  <vector>
#include "opencv2/imgcodecs/legacy/constants_c.h" 


int main(int argc, char** argv) {

    cv::Mat src(5, 4, CV_8UC3, cv::Scalar(10, 20, 30));
    std::vector<unsigned char> vec;

    int quality = 50; //压缩比率0~100

    std::vector<int> cp;  //创建vector对象,用来存储质量压缩信息
    cp.push_back(cv::IMWRITE_JPEG_QUALITY);
    //cv::IMWRITE_JPEG_QUALITY     jpg格式
    //其它格式:看 https://www.cnblogs.com/liming19680104/p/15388462.html   

    cp.push_back(quality);


    cv::imencode(".jpg", src, vec,cp);//把Mat转换成vector
    //参数1:按什么图片格式编码
    //参数2:Mat矩阵
    //参数3:vector对象
    //参数4:质量压缩信息[可选]

    std::cerr << vec.size() << std::endl;

    
    cv::waitKey(0);
    return 0;
}

 

 

 

 

posted @ 2021-11-27 12:08  天子骄龙  阅读(295)  评论(0编辑  收藏  举报