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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
读取视频流并保存视频文件,您可以使用OpenCV库中的`VideoWriter`类。以下是一个示例代码,演示了如何读取视频流并将其保存视频文件: ```python import cv2 # 打开视频文件 cap = cv2.VideoCapture('your_video_file.mp4') # 或者使用摄像头:cap = cv2.VideoCapture(0) # 检查视频是否成功打开 if not cap.isOpened(): print("无法打开视频文件") exit() # 获取视频流的宽度和高度 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 创建用于保存视频的VideoWriter对象 fourcc = cv2.VideoWriter_fourcc(*'XVID') # 可根据需要选择视频编码器 out = cv2.VideoWriter('output_video.avi', fourcc, 20.0, (width, height)) while True: # 逐帧读取视频流 ret, frame = cap.read() # 检查是否已经到达视频末尾 if not ret: break # 在窗口中显示当前帧 cv2.imshow('Video', frame) # 将当前帧写入输出视频文件 out.write(frame) # 按下 'q' 键退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放资源 cap.release() out.release() cv2.destroyAllWindows() ``` 在上述代码中,我们首先使用`cv2.VideoCapture()`函数打开视频文件或者连接到摄像头。然后,使用`cap.read()`逐帧读取视频流,并将每一帧显示在窗口中。同时,我们使用`cv2.VideoWriter()`创建一个用于保存视频的对象,并指定输出视频文件的名称、编码器、帧率和分辨率。在每一帧读取后,我们使用`out.write(frame)`将当前帧写入输出视频文件中。最后,我们需要释放资源,关闭窗口,并且调用`out.release()`来保存输出视频文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值