NO IMAGE
<pre name="code" class="cpp">#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <fstream>
#include<iostream>
#include<string>
#define NUM_FRAME 300 //只處理前300幀,根據視訊幀數可修改
void Video_to_image(char* filename)
{
printf("------------- video to image ... ----------------\n");
//初始化一個視訊檔案捕捉器
CvCapture* capture = cvCaptureFromAVI(filename);
//獲取視訊資訊
cvQueryFrame(capture);
int frameH = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps, numFrames);
//定義和初始化變數
int i = 0;
IplImage* img = 0;
std::string path = "C:\\Users\\Administrator\\Desktop\\aviToimage\\";
char image_name[13];
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
//讀取和顯示
while (1)
{
img = cvQueryFrame(capture); //獲取一幀圖片
cvShowImage("mainWin", img); //將其顯示
char key = cvWaitKey(20);
sprintf(image_name, "%s%d%s", "image",   i, ".jpg");//儲存的圖片名
std::string image = path image_name;
cvSaveImage(image.c_str(), img);   //儲存一幀圖片
if (i == NUM_FRAME) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("mainWin");
}
void Image_to_video()
{
int i = 0;
IplImage* img = 0;
char image_name[13];
printf("------------- image to video ... ----------------\n");
//初始化視訊編寫器,引數根據實際視訊檔案修改
CvVideoWriter *writer = 0;
int isColor = 1;
int fps = 30; // or 25
int frameW = 400; // 744 for firewire cameras
int frameH = 240; // 480 for firewire cameras
writer = cvCreateVideoWriter("out.avi", CV_FOURCC('X', 'V', 'I', 'D'), fps, cvSize(frameW, frameH), isColor);
printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n", frameH, frameW, fps);
//建立視窗
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
while (i<NUM_FRAME)
{
sprintf(image_name, "%s%d%s", "image",   i, ".jpg");
img = cvLoadImage(image_name);
if (!img)
{
printf("Could not load image file...\n");
exit(0);
}
cvShowImage("mainWin", img);
char key = cvWaitKey(20);
cvWriteFrame(writer, img);
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("mainWin");
}
using namespace std;
int main(int argc, char *argv[])
{
char filename[13] = "E:\\w.avi";
Video_to_image(filename); //視訊轉圖片
Image_to_video();    //圖片轉視訊
return 0;
}