提前配置:

OpenCV:​​https://opencv.org/​

代码:

(1)Iplimage类型

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
string video_dir = "F:\\1.mp4";
string saveDir = "./image/";
CvCapture *capture = NULL;
IplImage *frame = NULL;
IplImage* temp = NULL;
IplImage *dst = NULL;
capture = cvCreateFileCapture(video_dir.c_str());//最后要cvReleaseCapture(&capture);

int src_frame_width = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);//获取视频的宽
int src_frame_height = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);//获取视频的高
CvSize size;
size.width = src_frame_width;
size.height = src_frame_height;

temp = cvCreateImage(size, IPL_DEPTH_8U, 1);//创建目标图像
CvSize dstSize = cvSize(temp->width / 8, temp->height / 8);
dst = cvCreateImage(dstSize, temp->depth, temp->nChannels);
char savePath[100];
int pictureNumbers = 0;
cvNamedWindow("video");

while (1)
{
frame = cvQueryFrame(capture);
cvCvtColor(frame, temp, CV_BGR2GRAY);//cvCvtColor(src,des,CV_BGR2GRAY)
cvResize(temp, dst, CV_INTER_LINEAR);

cvShowImage("video", dst);
sprintf(savePath, "%s/%04d.jpg", saveDir, pictureNumbers);
cvSaveImage(savePath, dst);
cvWaitKey(1);
}
cvReleaseCapture(&capture);
cvReleaseImage(&dst);
cvDestroyAllWindows();
return 0;
}

更多《计算机视觉与图形学》知识,可关注下方公众号:

OpenCV视频读取、显示、保存_#include

 

 

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
//视频路径
string videoPath = "D:\\1.mp4";
//图像保存路径
string saveImagePath = "D:/image/1";
char saveChar[200];
// 创建了一个名为video的窗口用来显示帧
cv::namedWindow("video", cv::WINDOW_AUTOSIZE);
cv::VideoCapture cap;

// 读取视频文件
cap.open(videoPath);

cv::Mat frame;
int imageCount = 0;
while (true)
{
// 按帧读取
cap >> frame;
if (frame.empty())
break;
cv::imshow("video", frame);
sprintf(saveChar, "%s/%04d.jpg", saveImagePath, imageCount);
cout << saveChar << endl;
imwrite(saveChar, frame);
imageCount++;
if (cv::waitKey(33) >= 0)
break;
}
destroyAllWindows();
return 0;
}