【发布时间】:2020-05-17 23:28:25
【问题描述】:
我阅读了一个视频并将所有帧存储在一个向量中。现在我想缝合存储在矢量中的前 50 帧并保存结果,然后缝合接下来的 50 帧并保存结果,依此类推。这是因为opencv的拼接类不会一次处理大量的帧,报错:内存不足。我无法实现正确的逻辑。请帮助我更正代码。谢谢。我的代码如下:
int main(int argc, char const *argv[])
{
std::string video_file;
// Read video file
if (argc > 1)
{
video_file = argv[1];
}
else
{
video_file = "vid/xyz.mp4";
}
VideoCapture inputVideo(video_file);
if (!inputVideo.isOpened())
cerr << "Error opening video file\n";
double totalFrames(inputVideo.get(CV_CAP_PROP_FRAME_COUNT));
double frameRate(inputVideo.get(CV_CAP_PROP_FPS));
double timeInterval = 0.5; // in sec
int sizeReduction = 4;
int num = 1;
vector<Mat> frames;
vector<Mat> frames1;
Mat frame;
for (int num = 0; num < totalFrames; num++)
{
num = num + round(frameRate * timeInterval);
inputVideo.set(1, num);
inputVideo >> frame;
if (frame.empty())
continue;
cv::Size sz = frame.size();
cv::resize(frame, frame, sz / sizeReduction);
frames.push_back(frame); // vector 'frames' have all the frames to be processed
}
Stitcher::Mode mode = Stitcher::SCANS;
string result_name = "xyz.jpg";
vector<Mat> pano1;
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(mode);
int f1, f2 = 0;
// need help to correct the code below:
for (f1 = f2; f1 <= frames.size(); f1++) {
int count = 0;
while (count < 50) {
frames1.push_back(frames[f1]); // I want to save first 10 frames in vector 'frames1'
count++;
}
Stitcher::Status status = stitcher->stitch(frames1, pano);
pano1.push_back(pano);
f2 = f2 + count;
}
Stitcher::Status status = stitcher->stitch(pano1, pano);
imwrite(result_name, pano);
cout << "stitching completed successfully\n" << result_name << " saved!";
return EXIT_SUCCESS;
}
【问题讨论】:
-
该程序是否适用于 50 帧?为什么是50?您是否有少于 50 帧的错误?