我正在编写一个openCV简单程序,用于在单帧中显示多视频。
我可以写两个视频,我想要4个视频,有些人可以指导如何在单帧中显示4个视频。 下面是我的代码
int main(int argc, char** argv)
{
string filename = "/home/user/testavicaravi.avi";
VideoCapture capture(filename);
VideoCapture capture1(filename);
Mat frame;
Mat frame1;
if( !capture.isOpened() )
throw "Error when reading steam_avi0";
if( !capture1.isOpened() )
throw "Error when reading steam_avi1";
namedWindow( "w", 1);
for( ; ; )
{
capture >> frame;
capture1 >> frame1;
if(frame.empty())
break;
if(frame1.empty())
break;
Mat canvas = Mat::zeros(frame.rows*2+1, frame.cols*2+1, frame.type());
frame.copyTo(canvas(Range::all(), Range(0, frame.cols)));
frame1.copyTo(canvas(Range::all(), Range(frame1.cols+1, frame1.cols*2+1)));
// if it is too big to fit on the screen, then scale it down by 2, hopefully it'll fit :-)
imshow("w", canvas);
}
waitKey(0); // key press to close window
// releases and window destroy are automatic in C++ interface
}
答案 0 :(得分:2)
您正在寻找解决此问题的正确方法。 根据您想要显示4个视频的方式(作为2x2,1x4,4x1矩阵),试试这个:
frame1, frame2, frame3, frame4 are frames read from cameras 1, 2, 3 and 4
For 1x4: canvas size =
cols = frame1.cols + frame2.cols + frame3.cols + frame4.cols
rows = max(frame1.rows, frame2.rows, frame3.rows, frame4.rows)
frame1.copyTo(canvas(Rect(Point(0,0), frame1.size())))
frame2.copyTo(canvas(Rect(Point(frame1.cols,0), frame2.size())))
frame3.copyTo(canvas(Rect(Point(frame2.cols,0), frame3.size())))
frame4.copyTo(canvas(Rect(Point(frame3.cols,0), frame3.size())))
For 4x1: canvas size =
cols = max(frame1.cols, frame2.cols, frame3.cols, frame4.cols)
rows = frame1.rows + frame2.rows + frame3.rows + frame4.rows)
frame1.copyTo(canvas(Rect(Point(0,0), frame1.size())))
frame2.copyTo(canvas(Rect(Point(0,frame1.rows), frame2.size())))
frame3.copyTo(canvas(Rect(Point(0,frame2.rows), frame3.size())))
frame4.copyTo(canvas(Rect(Point(0,frame3.rows), frame4.size())))
For 2x2: canvas size =
cols = max(frame1.cols, frame2.cols) + max(frame3.cols, frame4.cols)
rows = max(frame1.rows, frame2.rows) + max(frame3.rows, frame4.rows)
frame1.copyTo(canvas(Rect(Point(0,0), frame1.size())))
frame2.copyTo(canvas(Rect(Point(frame1.cols,0), frame1.size())))
frame3.copyTo(canvas(Rect(Point(0,max(frame1.rows, frame2.rows)), frame3.size())))
frame4.copyTo(canvas(Rect(Point(frame3.cols,max(frame1.rows, frame2.rows)), frame4.size())))
您可能需要考虑在将这些帧组合到一个大型画布之前调整这些帧的大小,这样才能将它们全部放在一个屏幕上