OpenCV库很强大,不仅能够显示图片,还能解码显示各种格式的视频文件,而且提供的API接口也很多,包括C、C++、Python。下面比较一下分别使用C和C++播放一段视频文件的实现:

C语言实现

$ cat test_video.c

#include

#include

#include

int main(int argc, char **argv)

{

CvCapture *cap;

IplImage *frame;

cap = cvCaptureFromFile("./kakou.mp4");

printf("cap = %p\n", cap);

while ((frame = cvQueryFrame(cap))) {

//printf("frame = %p\n", frame);

cvShowImage("MyTest", frame);

cvWaitKey(33);

}

return 0;

}

$ gcc -o test_video test_video.c -lopencv_core -lopencv_imgproc -lopencv_highgui

C++语言实现

$ cat test_video.cpp

#include

#include

using namespace std;

using namespace cv;

int main(void){

//打开视频文件

VideoCapture capture("./kakou.mp4");

//isOpen判断视频是否打开成功

if(!capture.isOpened())

{

cout<

return -1;

}

//获取视频帧频

double rate=capture.get(CV_CAP_PROP_FPS);

cout<

cout<

Mat frame;

namedWindow("Movie Player");

double position=0.0;

//设置播放到哪一帧,这里设置为第0帧

capture.set(CV_CAP_PROP_POS_FRAMES,position);

while(1)

{

//读取视频帧

if(!capture.read(frame))

break;

imshow("Movie Player",frame);

//获取按键值

char c=waitKey(33);

if(c==27)

break;

}

capture.release();

destroyWindow("Movie Player");

return 0;

}

$g++ -o test_video test_video.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui

来一个效果图:

76df1592384b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
74addff3 docs: fix spelling errors in code and comments 20 小时前
4cfd9689 Fix UB in cv::error when breakOnError set to true 22 小时前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐