使用opencv2从视频保存图像序列 [英] saving an image sequence from video using opencv2

查看:232
本文介绍了使用opencv2从视频保存图像序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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

}

提前谢谢

编辑以上内容.

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

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

推荐答案

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

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.

这是一个示例:

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

这篇关于使用opencv2从视频保存图像序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆