OpenCV视频读取、显示、保存

提前配置:

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

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

 

 

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值