Fuzhou University "Comprehensive Design of Embedded Systems" Experiment 11: OpenCV Video Decoding

1. Experimental purpose

Master the OpenCV video decoding process and compare the difference with FFMPEG.

2. Experimental content

Build an experimental development environment, compile and run the decoding program, and decode the encoded video stream through OpenCV.

3. Development environment

Development host: Ubuntu 20.04.6 LTS

Hardware: Suaneng SE5

4. Experimental equipment

Development host + cloud platform (or SE5 hardware)

5. Experimental process and conclusion

OpenCV decoding principle and process

OpenCV also supports decoding videos. OpenCV encapsulates FFMPEG internally , but actually calls the FFMPEG interface.

However, OpenCV provides a simple interface to the outside world, which can be quickly called to implement video decoding.

OpenCV uses the mat data structure to represent images. For example, you can use OpenCV to quickly decode video files through the following methods:

//初始化VideoCapture类
VideoCapture cap;

//打开文件或者摄像头或者某个RTSP连接
cap.open(argv[1], CAP_FFMPEG, card); 

//读取视频帧存入image中
Mat image;
cap.read(image);   

OpenCV also supports setting some decoding parameters through the VideoCapture class interface, such as output format type, length and width, etc .:

//设置输出的高和宽
cap.set(CAP_PROP_FRAME_HEIGHT, (double)h);  
cap.set(CAP_PROP_FRAME_WIDTH, (double)w);

//设置输出为YUV数据格式
cap.set(cv::CAP_PROP_OUTPUT_YUV, PROP_TRUE);

It should be noted that OPENCV uses hardware acceleration internally. If you need to perform CPU processing on the processed pictures or data , such as saving files, you need to perform memory synchronization operations. Please refer to the following:

//内存同步到CPU
bmcv::downloadMat(image);
for (int i = 0; i < image.avRows(); i++) {  
    fwrite((char*)image.avAddr(0)+i*image.avStep(0),1,image.avCols(),dumpfile);
}
for (int i = 0; i < image.avRows()/2; i++) {
    fwrite((char*)image.avAddr(1)+i*image.avStep(1),1,image.avCols()/2,dumpfile);
}
for (int i = 0; i < image.avRows()/2; i++) {
    fwrite((char*)image.avAddr(2)+i*image.avStep(2),1,image.avCols()/2,dumpfile);
}
experiment procedure

The specific experimental process is similar to the previous experimental process and will not be described again here.

Guess you like

Origin blog.csdn.net/m0_52537869/article/details/134702640