2

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

我想要做的是按顺序保存视频文件中的帧。我已经设法使用 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);
}
4

3 回答 3

1

这是我的代码...我尝试了很多,最后使用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;}


}


} 
于 2015-07-06T18:51:37.270 回答
0

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

这是一个例子:

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;
}
于 2013-04-08T05:46:10.310 回答
0

这是我在 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
于 2019-01-04T19:54:56.873 回答