【问题标题】:saving an image sequence from video using opencv2使用opencv2从视频中保存图像序列
【发布时间】:2013-03-30 00:48:49
【问题描述】:

新手问题,是的,我花了很多时间筛选类似的问题和答案,但没有运气。

我想要做的是按顺序保存视频文件中的帧。我已经设法使用 c 保存了一张图像,之后我似乎无法保存图像。我已经开始在 opencv 中使用 c++ 而不是 c,我所能做的就是查看视频而不是从中保存任何 jpg。

如果有帮助,我会在 mac 上使用 opencv2.4.4a。 下面是我的c示例

#include <stdio.h>
#include <stdlib.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>

using namespace cv;
using namespace std;

int main (int argc, char** argv)
{
//initializing capture from file
CvCapture * capture = cvCaptureFromAVI ("/example/example.mov");

//Capturing a frame
IplImage* img = 0;
if(!cvGrabFrame(capture))      //capture a frame
{
printf)Could not grab a fram\n\7");
exit(0);
}
img=cvRerieveFrame(capture);    //retrieve the captured frame

//writing an image to a file
if (!cvSaveImage("/frames/test.jpg", img))
printf("Could not save: %s\n","test.jpg");

//free resources
cvReleaseCapture(&capture);

}

提前谢谢你

编辑以上内容。

我已经添加到上面的代码中,这导致图像与 test.jpg 一起保存,然后用下一帧重写。如何告诉opencv不要复制最后一张图片并将下一帧重命名为test_2.jpg,例如test_1.jpg、test_2.jpg等等?

double num_frames = cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_COUNT);

for (int i = 0; i < (int)num_frames; i++)
{
img = cvQueryFrame(capture);
cvSaveImage("frames/test.jpg", img);
}

cvReleaseCapture(&capture);
}

【问题讨论】:

    标签: opencv video sequences


    【解决方案1】:

    这是我在 Python3.0 中的做法。必须有 CV2 3+ 版本才能工作。 此功能以给定的频率保存图像。

    import cv2
    import os
    print(cv2.__version__)
    
    # Function to extract frames 
    def FrameCapture(path,frame_freq): 
    
        # Path to video file 
        video = cv2.VideoCapture(path) 
        success, image = video.read()
    
        # Number of frames in video
        fps = int(video.get(cv2.CAP_PROP_FPS))
        length = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
    
        print('FPS:', fps)
        print('Extracting every {} frames'.format(frame_freq))
        print('Total Frames:', length)
        print('Number of Frames Saved:', (length // frame_freq) + 1)
    
        # Directory for saved frames
        try:
            frame_dir = path.split('.')[0]
            os.mkdir(frame_dir)
        except FileExistsError:
            print('Directory ({}) already exists'.format(frame_dir))
    
        # Used as counter variable 
        count = 0
    
        # checks whether frames were extracted 
        success = 1
    
        # vidObj object calls read 
        # function extract frames 
    
        while count < length :
            video.set(cv2.CAP_PROP_POS_FRAMES , count)
            success, image = video.read()
            # Saves the frames with frame-count 
            cv2.imwrite(frame_dir + "/frame%d.jpg" % count, image)
            count = count + frame_freq
    

    【讨论】:

      【解决方案2】:

      这是我的代码...我尝试了很多,终于成功了 这是使用opencv 3的c ++ ...希望它有效

      #include "opencv2/opencv.hpp"
      #include <sstream>
      #include <iostream>
      
      
      using namespace cv;
      using namespace std;
      
       Mat frame,img;
          int counter;
      
      int main(int,char**)
         {
              VideoCapture vid("video3.avi");
      
      
      while (!vid.isOpened())
      {
          VideoCapture vid("video2.MOV");
          cout << "charging" << endl;
          waitKey(1000);
      
      }
      
      cout << "Video opened!" << endl;
      
      while(1)
      {
          stringstream file;
      
          vid.read(frame);
          if(frame.empty()) break;
          file << "/home/pedro/workspace/videoFrame/Debug/frames/image" << counter << ".jpg";
          counter++;
          imwrite(file.str(),frame);
      
          char key = waitKey(10);
       if ( key == 27)
       {break;}
      
      
      }
      
      
      } 
      

      【讨论】:

        【解决方案3】:

        使用索引来跟踪文件名中的数字部分。在图像捕获循环中,添加带有文件名的索引并构建最终文件名。

        这是一个例子:

        while(1)
        {
             cap.read ( frame);
             if( frame.empty()) break;
             imshow("video", frame);
             char filename[80];
             sprintf(filename,"C:/Users/cssc/Desktop/testFolder/test_%d.png",i);
             imwrite(filename, frame);
             i++;
             char key = waitKey(10);
             if ( key == 27) break;
        }
        

        【讨论】:

        • 感谢巴山!我必须包括 int i = 0;我++;它奏效了
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-05
        • 1970-01-01
        相关资源
        最近更新 更多