opencv imdecode和imencode用法

时间:2023-03-09 17:56:48
opencv imdecode和imencode用法

主要是对内存数据自动编解码

    string fname = "D:/image.jpg";
//! 以二进制流方式读取图片到内存
FILE* pFile = fopen(fname.c_str(), "rb");
fseek(pFile, , SEEK_END);
long lSize = ftell(pFile);
rewind(pFile);
char* pData = new char[lSize];
fread(pData, sizeof(char), lSize, pFile);
fclose(pFile);

//! 解码内存数据,变成cv::Mat数据
cv::Mat img_decode;
vector<uchar> data;
for (int i = ; i < lSize; ++i){
data.push_back(pData[i]);
}
img_decode = cv::imdecode(data, CV_LOAD_IMAGE_COLOR);
cv::flip(img_decode, img_decode, -);
img_decode.channels();

//! 将cv::Mat数据编码成数据流
vector<unsigned char> img_encode;
cv::imencode(".jpg", img_decode, img_encode);
unsigned char *encode_data = new unsigned char[lSize];
for (int i = ; i<lSize; i++){
encode_data[i] = img_encode[i];
}