【问题标题】:Read a sequence of frames using OpenCV/C++使用 OpenCV/C++ 读取帧序列
【发布时间】:2013-10-21 16:46:13
【问题描述】:

我想使用 openCV 从任何文件夹中读取一系列帧。所有帧都按顺序排列,即 (1).jpg,(2).jpg,.... 我试过了

VideoCapture cap;
cap.open("Directory/");
for(;;)
    {
        Mat frame;
        cap >> frame;
    }

但它不起作用。 以前有人问过这个问题,但我不知道为什么这个答案对我不起作用。

OpenCV: Reading image series from a folder

我需要重命名图像吗?

【问题讨论】:

标签: opencv video-capture frames


【解决方案1】:

cap open 应该是 cap.open("Directory/(%02d).jpg"); 并且您必须重命名您的图像,使它们看起来像 (01).jpg(02).jpg 等,以便它们具有固定长度。如果图像类似于(001).jpg,那么您应该使用`cap.open("Directory/(%03d).jpg");

编辑

#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
    VideoCapture cap;
    cap.open("imgs/(%02d).jpg");
    int i=0;
    for(;;)
    {
        if(i++%37==0)cap=VideoCapture("imgs/(%02d).jpg");//there are 37 frames in the dir
        Mat frame;
        cap >> frame;
        imshow("frame",frame);
        if(waitKey(1)==27)
            exit(0);
    }
    return 0;
}

【讨论】:

  • 如果要遍历所有图像,则需要一个 for 循环。 vid 捕获像普通捕获设备一样工作,但它会在序列结束时给出一个空垫,因此示例程序将在不处理的情况下崩溃
【解决方案2】:

尝试按照所需的顺序准备一个包含名称列表和图像路径的 xml/yaml 文件。然后将列表加载为向量或一些类似的结构,然后在循环中一一打开。

【讨论】:

    【解决方案3】:

    这里的完整代码使用字符串连接的想法来读取名称为“frame00000.jpg, frame00001.jpg,.....,frame00010.jpg...) 的五个零的帧序列。

    #include "stdafx.h"
    #include <stdlib.h>
    #include <math.h>
    #include <opencv/cv.h>      // include it to used Main OpenCV functions.
    #include <opencv/highgui.h> //include it to use GUI functions.
    
    using namespace std;
    using namespace cv;
    
    string intToStr(int i,string path){
        string bla = "00000";
        stringstream ss;
        ss<<i;
        string ret ="";
        ss>>ret;
        string name = bla.substr(0,bla.size()-ret.size());
        name = path+name+ret+".jpg";
        return name;
        }
    
    
    int main(int, char**)
        {   
        string previous_window = "Previous frame";
        string current_window = "Current frame ";
    
        int i=0;
        for(int i = 1 ; i< 10 ; i++)
        {
            Mat Current, Previous;
            string Curr_name = intToStr(i,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
            string Prev_name = intToStr(i-1,"D:/NU/Junior Scientist/Datasets/egtest04/frame");
            Current = imread(Curr_name,1);
            Previous = imread(Prev_name,1);
    
            namedWindow(current_window,WINDOW_AUTOSIZE);
            namedWindow(current_window,WINDOW_AUTOSIZE);
            imshow(current_window,Current);
            imshow(previous_window,Previous);
            waitKey(0);
        }
        }
    

    其中“D:/NU/Junior Scientist/Datasets/egtest04/frame”是路径刺。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2020-05-07
      • 2013-11-08
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多