从 opencv c++ 中的矢量中检索固定的帧数
Retrieve fix number of frames from vector in opencv c++
我读取了一个视频并将所有帧存储在一个向量中。现在我想拼接存储在矢量中的前 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 filen";
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 successfullyn" << result_name << " saved!";
return EXIT_SUCCESS;
}
您首先将所有帧存储在vector
中,并且仅在运行算法将它们拼接在一起之后。这浪费了很多内存。
更好的方法是阅读前两帧并将它们拼接在一起以创建全景图。然后阅读第三帧并将其拼接到现有的全景图上。然后阅读第四个并缝合它,然后是第五个...等等。
没有必要将所有这些帧存储在矢量中。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- 使用VerQueryValue检索应用程序的文件描述
- OpenCV C++.快速计算混淆矩阵
- 从 opencv c++ 中的矢量中检索固定的帧数
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 从OpenCV C 中的视频文件中检索每三个帧
- OpenCV视频捕获抓取和检索
- 使用OpenCV中的指针检索深度
- 在编译时检索 Opencv Mat_的类型<T>
- 在 OpenCV 中检索当前帧号
- 使用opencv检索视频帧-未处理的异常
- 如何从OpenCV FileNode映射中检索键值对
- 如何检索轮廓点-转换为矢量使用opencv
- 从OpenCV FeatureDetector检索功能类型
- 我可以将数据分配给从OpenCV对象解引用中检索的双函数返回以任何方式