Opencv 两个视频同时显示

26 篇文章 26 订阅

以下是将两个视频同时显示在一个框内:


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
 


void Show_two_videos()
{
IplImage *newFrame = NULL;
IplImage *frame1 = NULL;
IplImage *frame2 = NULL;
CvCapture * pCapture1 = cvCaptureFromAVI("第一个视频地址");
CvCapture * pCapture2 = cvCaptureFromAVI("第二个视频地址");
CvRect rect;
CvVideoWriter *pWriter = NULL;
//get the frame number of two videos  
int frameNo1 = (int)cvGetCaptureProperty(pCapture1, CV_CAP_PROP_FRAME_COUNT);
int frameNo2 = (int)cvGetCaptureProperty(pCapture2, CV_CAP_PROP_FRAME_COUNT);
if (frameNo1 != frameNo2)
printf("video length 1 != video length 2\n");
else
printf("Total frame numbers: %d\n", frameNo1);
//get the frame width of two videos  
int frameWidth1 = (int)cvGetCaptureProperty(pCapture1, CV_CAP_PROP_FRAME_WIDTH);
int frameWidth2 = (int)cvGetCaptureProperty(pCapture2, CV_CAP_PROP_FRAME_WIDTH);
if (frameWidth1 != frameWidth2)
printf("video width 1 != video width 2\n");
else
printf("frame width: %d\n", frameWidth1);
//get the frame height of two videos  
int frameHeight1 = (int)cvGetCaptureProperty(pCapture1, CV_CAP_PROP_FRAME_HEIGHT);
int frameHeight2 = (int)cvGetCaptureProperty(pCapture2, CV_CAP_PROP_FRAME_HEIGHT);
if (frameHeight1 != frameHeight2)
printf("video height 1 != video height 2\n");
else
printf("frame height: %d\n", frameHeight1);
//get the video fps  
int fps1 = (int)cvGetCaptureProperty(pCapture1, CV_CAP_PROP_FPS);
int fps2 = (int)cvGetCaptureProperty(pCapture2, CV_CAP_PROP_FPS);
if (fps1 != fps2)
printf("video fps 1 != video fps 2\n");
else
printf("frame fps: %d\n", fps1);


int initFlag = 0;
int counter = 0;
while ((frame1 = cvQueryFrame(pCapture1)) != NULL && (frame2 = cvQueryFrame(pCapture2)) != NULL)
{
printf("%d\n", counter++);
if (initFlag == 0)
{
newFrame = cvCreateImage(cvSize(frame1->width * 2, frame1->height), frame1->depth, frame1->nChannels);
pWriter = cvCreateVideoWriter("合成后视频的地址", CV_FOURCC('X', 'V', 'I', 'D'), fps1, cvSize(frame1->width * 2, frame1->height), 1);
initFlag = 1;
}
rect.x = 0;
rect.y = 0;
rect.height = frameHeight1;
rect.width = frameWidth1;
//use ROI to implement the video split joint  
cvSetImageROI(newFrame, rect);
cvCopyImage(frame1, newFrame);
cvResetImageROI(newFrame);
rect.x = frameWidth1;
rect.y = 0;
rect.height = frameHeight1;
rect.width = frameWidth1;
cvSetImageROI(newFrame, rect);
cvCopyImage(frame2, newFrame);
cvResetImageROI(newFrame);
cvWriteFrame(pWriter, newFrame);
}
cvReleaseImage(&frame1);
cvReleaseImage(&frame2);
cvReleaseImage(&newFrame);
cvReleaseVideoWriter(&pWriter);
}


int main(int argc, char *argv[])
{
Show_two_videos();    //同时显示两个视频
return 0;
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值