我正在尝试编写一个程序,将 4 个视频组合成一个视频,将它们排列成 2x2 网格。下面是相关代码:
void CombineVideoApp::on_GoButton_clicked()
{
std::string Video1Name = Video1Path.toUtf8().constData();
std::string Video2Name = Video2Path.toUtf8().constData();
std::string Video3Name = Video3Path.toUtf8().constData();
std::string Video4Name = Video4Path.toUtf8().constData();
int VideoWidth = 640;
int VideoHeight = 360;
cv::Mat Video1Frame, Video2Frame, Video3Frame, Video4Frame; // Creating Mat objects to store the captured frames
cv::Mat Video1Shrunk, Video2Shrunk, Video3Shrunk, Video4Shrunk; //Creating Mat objects to store the shrunk frames
cv::Mat FullCombinedVideo(VideoWidth,VideoHeight, Video1Shrunk.type()); //Creating Mat object to store the combined video
cv::Mat CombinedVideoTop(VideoWidth, VideoHeight/2, Video1Shrunk.type());
cv::Mat CombinedVideoBottom(VideoWidth, VideoHeight/2, Video1Shrunk.type());
cv::VideoCapture Video1(Video1Name);
cv::VideoCapture Video2(Video2Name);
cv::VideoCapture Video3(Video3Name);
cv::VideoCapture Video4(Video4Name);
double Video1FrameCount = Video1.get(CV_CAP_PROP_FRAME_COUNT);
double Video2FrameCount = Video2.get(CV_CAP_PROP_FRAME_COUNT);
double Video3FrameCount = Video3.get(CV_CAP_PROP_FRAME_COUNT);
double Video4FrameCount = Video4.get(CV_CAP_PROP_FRAME_COUNT);
double CombinedFrameTopCount = min(Video1FrameCount, Video2FrameCount);
double CombinedFrameBottomCount = min(Video3FrameCount, Video4FrameCount);
double CombinedFrameCount = min(CombinedFrameBottomCount, CombinedFrameTopCount);
Video1.set(CV_CAP_PROP_FRAME_COUNT, CombinedFrameCount);
Video2.set(CV_CAP_PROP_FRAME_COUNT, CombinedFrameCount);
Video3.set(CV_CAP_PROP_FRAME_COUNT, CombinedFrameCount);
Video4.set(CV_CAP_PROP_FRAME_COUNT, CombinedFrameCount);
for(;;)
{
Video1 >> Video1Frame;
Video2 >> Video2Frame;
Video3 >> Video3Frame;
Video4 >> Video4Frame;
cv::resize(Video1Frame, Video1Shrunk, Size((VideoWidth/2), (VideoHeight/2)));
cv::resize(Video2Frame, Video2Shrunk, Size((VideoWidth/2), (VideoHeight/2)));
cv::resize(Video3Frame, Video3Shrunk, Size((VideoWidth/2), (VideoHeight/2)));
cv::resize(Video4Frame, Video4Shrunk, Size((VideoWidth/2), (VideoHeight/2)));
Video1Shrunk.copyTo(CombinedVideoTop(Rect(0,0,Video1Shrunk.cols,Video1Shrunk.rows)));
Video2Shrunk.copyTo(CombinedVideoTop(Rect(Video1Shrunk.cols,0,Video2Shrunk.cols,Video2Shrunk.rows)));
Video3Shrunk.copyTo(CombinedVideoBottom(Rect(0,0,Video3Shrunk.cols,Video3Shrunk.rows)));
Video4Shrunk.copyTo(CombinedVideoBottom(Rect(Video3Shrunk.cols,0,Video4Shrunk.cols,Video4Shrunk.rows)));
CombinedVideoTop.copyTo(FullCombinedVideo(Rect(0,0,CombinedVideoTop.cols, CombinedVideoTop.rows)));
CombinedVideoBottom.copyTo(FullCombinedVideo(Rect(0,CombinedVideoTop.rows, CombinedVideoBottom.cols,CombinedVideoBottom.rows)));
std::string FinalFilePath = CombinedVideo.toUtf8().constData();
cv::VideoWriter CombinedWriter(FinalFilePath, CV_FOURCC('D', 'I', 'V', 'X'),10,cvSize(FullCombinedVideo.cols, FullCombinedVideo.rows));
CombinedWriter << FullCombinedVideo;
}
}
该程序编译没有问题,但是当我实际尝试运行它时,我收到以下错误:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /tmp/OpenCV-2.4.3/modules/core/src/matrix.cpp, line 322
Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.
The program has unexpectedly finished.
谁能告诉我这里有什么问题?我完全被难住了。我试图打开的视频文件是 .avi 格式,我使用的是 QtCreator、OpenCV 2.4.3,并在 OSX Snow Leopard 上运行它。
谢谢