cv2.imdecode()函数从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像。
cv2.imencode()函数是将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。
#include <iostream>
#include <sstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <vector>
#include "opencv2/imgcodecs/legacy/constants_c.h"
#include "opencv2/imgproc/types_c.h"
using namespace cv;
int main()
{
std::string str("1234");
printf("size = %d\n",str.size());
std::cout<<str.end() - str.begin()<<std::endl;
VideoCapture capture("/home/john/Videos/raw.mp4");//创建一个视频文件对象
if (!capture.isOpened())
{
printf("capture open failed\n");
return 0;
}
Mat frame;
Mat d_mat;
while (1)
{
capture >> frame; //读取当前帧
printf("cols = %d, row = %d, type= %d\n",frame.cols, frame.rows, frame.type());
std::vector<uchar> data_encode;
imencode(".png", frame, data_encode);
std::string str_encode(data_encode.begin(), data_encode.end());
printf("encode size = %u \n",str_encode.size());
std::vector<uchar> data_decode(str_encode.begin(), str_encode.end());
d_mat = imdecode(data_decode, CV_LOAD_IMAGE_COLOR);
imshow("video", d_mat);
waitKey(30);
}
return 0;
}