Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用opencv2保存视频中的图像序列_Opencv_Video_Sequences - Fatal编程技术网

使用opencv2保存视频中的图像序列

使用opencv2保存视频中的图像序列,opencv,video,sequences,Opencv,Video,Sequences,新手问题是的,我花了很多时间筛选类似的问题和答案,但运气不好 我试图做的是按顺序保存视频文件中的帧。我已经设法用c保存了一个图像,之后我似乎无法保存图像。我已经开始使用C++在OpenCV中代替C,我所能做的就是查看视频,而不保存任何JPG。p> 如果有帮助的话,我正在mac上使用opencv2.4.4a。 下面是我的c示例 #include <stdio.h> #include <stdlib.h> #include <opencv/cv.h> #inclu

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

我试图做的是按顺序保存视频文件中的帧。我已经设法用c保存了一个图像,之后我似乎无法保存图像。我已经开始使用C++在OpenCV中代替C,我所能做的就是查看视频,而不保存任何JPG。p> 如果有帮助的话,我正在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);

}
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
//正在初始化从文件捕获
CvCapture*capture=cvCaptureFromAVI(“/example/example.mov”);
//捕捉帧
IplImage*img=0;
if(!cvGrabFrame(capture))//捕获帧
{
printf)无法获取帧\n\7“;
出口(0);
}
img=cvRerieveFrame(捕获);//检索捕获的帧
//将图像写入文件
如果(!cvSaveImage(“/frames/test.jpg”,img))
printf(“无法保存:%s\n”,“test.jpg”);
//免费资源
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);
}
double num\u frames=cvGetCaptureProperty(捕获、CV\u CAP\u PROP\u FRAME\u计数);
对于(int i=0;i<(int)num_帧;i++)
{
img=cvQueryFrame(捕获);
cvSaveImage(“frames/test.jpg”,img);
}
cvReleaseCapture(&capture);
}

使用一个索引来跟踪文件名中的数字部分。在图像捕获循环中,使用文件名添加索引并生成最终文件名

以下是一个例子:

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

这是我的代码…我尝试了很多,终于成功了 这是C++使用OpenCV 3…希望它能工作

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


}


} 
#包括“opencv2/opencv.hpp”
#包括
#包括
使用名称空间cv;
使用名称空间std;
垫架,img;
整数计数器;
int main(int,char**)
{
视频捕获视频(“video3.avi”);
而(!vid.isOpened())
{
视频捕获视频(“video2.MOV”);

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

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
导入cv2
导入操作系统
打印(cv2.版本)
#函数来提取帧
def帧捕获(路径、帧频率):
#视频文件的路径
视频=cv2.视频捕获(路径)
成功,image=video.read()
#视频中的帧数
fps=int(video.get(cv2.CAP\u PROP\u fps))
长度=int(video.get(cv2.CAP\u PROP\u FRAME\u COUNT))
打印('FPS:',FPS)
打印('提取每{}帧'。格式(帧\频率))
打印('总帧数:',长度)
打印('保存的帧数:',(长度//帧频)+1)
#已保存帧的目录
尝试:
frame_dir=path.split('.')[0]
os.mkdir(帧目录)
除文件ExistError外:
打印('Directory({})已存在。格式(frame_dir))
#用作计数器变量
计数=0
#检查是否提取了帧
成功=1
#vidObj对象调用read
#函数提取帧
当计数<长度时:
视频设置(cv2.CAP\u PROP\u POS\u帧,计数)
成功,image=video.read()
#使用帧数保存帧
cv2.imwrite(frame_dir+“/frame%d.jpg”%count,image)
计数=计数+帧频

谢谢Barshan!我必须包含int I=0;I++;并且它工作正常