saving an image sequence from video using opencv2

南笙酒味 提交于 2019-12-30 11:13:05

问题


Newbie question and yes I have spent a lot of time sifting through similar questions and Answers with no luck.

What I am trying to do is save frames from a video file in a sequential order. I have managed to save one image using c and I cannot seem to save images after that. I have started using c++ in opencv instead of c and all I can do is view the video and not save any jpg's from it.

I am using opencv2.4.4a on mac if that helps. below is my c example

#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);

}

Thank you in advance

edit to the above.

I have added to the above code which results in an image to be saved with the test.jpg and then gets rewritten with the next frame. How do I tell opencv to not copy over the last image and rename the next frame to test_2.jpg eg, test_1.jpg, test_2.jpg and so on?

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);
}

回答1:


Use an index that will keep track of the number part in the filename. In the image capturing loop, add the index with the filename and build the final filename.

here is an example :

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;
}



回答2:


This is my code... I tryed a lot and finally made it this is c++ using opencv 3... hope it works

#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:


This is my way to do in Python3.0. Have to have CV2 3+ version for it to work. This function saves images with frequency given.

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


来源:https://stackoverflow.com/questions/15870612/saving-an-image-sequence-from-video-using-opencv2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!