/
8combvidhor.cpp
85 lines (69 loc) · 2.34 KB
/
8combvidhor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <stdio.h>
int FPS,width,height;
int FPS1,movieWidth1, movieHeight1;
int FPS2,movieWidth2,movieHeight2;
CvCapture* capture1 = 0;
CvCapture* capture2 = 0;
CvVideoWriter *videoWriter = 0;
int max(int a,int b)
{
if(a>=b)
return a;
else
return b;
}
int main( int argc, char** argv )
{
IplImage *imageOut = 0;
IplImage *frame1,*frame2;
int B = 0; // border size.
capture1 = cvCaptureFromAVI( argv[1] );
capture2 = cvCaptureFromAVI( argv[2] );
FPS1 = cvRound( cvGetCaptureProperty(capture1, CV_CAP_PROP_FPS) );
FPS2 = cvRound( cvGetCaptureProperty(capture2, CV_CAP_PROP_FPS) );
FPS = max(FPS1, FPS2); // Use the fastest video speed.
frame1 = cvQueryFrame( capture1 );
frame2 = cvQueryFrame( capture2 );
movieWidth1 = frame1->width;
movieHeight1 = frame1->height;
movieWidth2 = frame2->width;
movieHeight2 = frame2->height;
printf("Got a video source 1 with a resolution of %dx%d at %d fps.\n", movieWidth1, movieHeight1, FPS1);
printf("Got a video source 2 with a resolution of %dx%d at %d fps.\n", movieWidth2, movieHeight2, FPS2);
width = B + movieWidth1 + B + movieWidth2 + B;
height = B + max(movieHeight1, movieHeight2) + B;
CvSize size = cvSize(width, height);
imageOut = cvCreateImage( size, 8, 3 ); // RGB image
imageOut->origin = frame1->origin;
printf("Combining the videos into a resolution of %dx%d at %d fps.\n", width, height, FPS);
int fourCC_code = CV_FOURCC('M','J','P','G'); // M-JPEG codec
int isColor = 1;
videoWriter = cvCreateVideoWriter(argv[3], fourCC_code, FPS, size, isColor);
printf("Storing stabilized video into '%s'\n", argv[3]);
cvNamedWindow( "CombineVids", 1 );
cvResizeWindow("CombineVids",1000,600);
while (1)
{
frame1 = cvQueryFrame( capture1 );
frame2 = cvQueryFrame( capture2 );
if( !frame1 && !frame2 )
break;
else if(!frame1)
frame1=frame2;
else if(!frame2)
frame2=frame1;
cvSetImageROI( imageOut, cvRect(B, B, movieWidth1, movieHeight1) );
cvCopy( frame1, imageOut, NULL );
cvSetImageROI( imageOut, cvRect(B + movieWidth1 + B, B, movieWidth2, movieHeight2) );
cvCopy( frame2, imageOut, NULL );
cvResetImageROI( imageOut );
cvWriteFrame(videoWriter, imageOut);
cvShowImage( "CombineVids", imageOut );
int c = cvWaitKey(30);
if( (char)c == 27 )
break;
}
return 0;
}