opencv视频的读取与存储

1,处理视频的每一帧,并保存成图像存入指定路径

方法一:cv2.imencode

cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count)

例程如下:

import cv2


def video2frame(videos_path, frames_save_path, time_interval):
    '''
    :param videos_path: 视频的存放路径
    :param frames_save_path: 视频切分成帧之后图片的保存路径
    :param time_interval: 保存间隔
    :return:
    '''
    vidcap = cv2.VideoCapture(videos_path)
    success, image = vidcap.read()
    count = 0
    while success:
        success, image = vidcap.read()
        count += 1
        if count % time_interval == 0:
            cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count)
        # if count == 20:
        #   break
    print(count)


if __name__ == '__main__':
    videos_path = r'C:\Users\16078\Desktop\5.avi'
    frames_save_path = r'.\split'
    time_interval = 1  # 隔一帧保存一次
    video2frame(videos_path, frames_save_path, time_interval)

方法二:cv2.imwrite

创建文件夹

foldername = 'my_test/weights/len{}_bs{}_per{}_epo{}'.format(len(res_x), batch_size, train_num, epoch)
word_name = os.path.exists(foldername)
# 判断文件是否存在:不存在创建
if not word_name:
   os.makedirs(foldername)
#保存图片
TrackingFrame_Pathout = ("./output/frame_" + str(num) + ".jpg")
cv2.imwrite(TrackingFrame_Pathout,img)

2,OpenCV读取视频,逐帧处理后,保存成视频:cv2.VideoWrite.write(frame)

版本一,cpp代码

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
 
using namespace std;
using namespace cv;

int main(){
	//读取视频
	cv::VideoCapture capture("/home/nx/Pictures/input.mp4");  
	long totalFrameNumber = capture.get(CAP_PROP_FRAME_COUNT);
	cout<<"整个视频共"<<totalFrameNumber<<"帧"<<endl;
	capture.set( CAP_PROP_POS_FRAMES,0);
	
	//获取帧率
	double rate = capture.get(CAP_PROP_FPS);
	cout<<"帧率为:"<<rate<<endl;
	
	int delay = 1000/rate;
	cv::Mat frame;
	if(!capture.read(frame))
	    {
	    	cout<<"读取视频失败"<<endl;
	        return -1;
	    }
	cout<<"图片宽width为:"<<frame.rows<<endl;
	cout<<"图片高height为:"<<frame.cols<<endl;
	int isColor = 1;
	int fps = rate;
	int frameWidth = frame.rows;
	int frameHeight = frame.cols;
	cv::VideoWriter writer("output.avi", VideoWriter::fourcc('M','J','P','G'), fps, Size(frameWidth, frameHeight), isColor);
	for (;;)
	{
		frame = frame.clone();  //对图片进行处理
		writer.write(frame);
		imshow("frame",frame);
		waitKey(delay);//因为图像处理需要消耗一定时间,所以图片展示速度比保存视频要慢
	    //读取下一帧
	    if(!capture.read(frame))
	    {
	            cout<<"读取视频失败"<<endl;
	            return -1;
	    }
	}
	capture.release();
	waitKey(0);
	return 0;
}

方法二,python版本

	  cap = cv2.VideoCapture(video_path)
    SAVE_VIDEO = True
    if SAVE_VIDEO:
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        out = cv2.VideoWriter('kalman_output.avi', fourcc, 20,(768,576))
    while (True):
        ret, frame = cap.read()
        if SAVE_VIDEO:
            out.write(frame)    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值