首先是Video---->Image
clc;
clear;
video_file = '1VIS.avi';
video = VideoReader(video_file);% 读取视频
frame_number = video.NumberOfFrames;% 得到帧数
for i=1:frame_number
image_name = strcat('VIS\', num2str(i-1));
img_name = strcat(image_name,'.jpg');
I = read(video, i);% 读出图片
if i~=1
imwrite(I, img_name, 'jpg');%写图片。保存
end
% imwrite(I, img_name, 'jpg');%写图片。保存
I = [];
end
frame_number
Image--->Video
clc;
clear;
video_file = '1VIS.avi';
video = VideoReader(video_file);% 读取视频
frame_number = video.NumberOfFrames;% 得到帧数
for i=1:frame_number
image_name = strcat('VIS\', num2str(i-1));
img_name = strcat(image_name,'.jpg');
I = read(video, i);% 读出图片
if i~=1
imwrite(I, img_name, 'jpg');%写图片。保存
end
% imwrite(I, img_name, 'jpg');%写图片。保存
I = [];
end
frame_number
从指定区域截取图像
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 27 10:29:00 2019
@author: Administrator
"""
#Python - 截取指定帧数间隔指定大小的视频,并保存图片到指定位置
import shutil
import os
import cv2
os.chdir(os.path.split(os.path.realpath(__file__))[0])
print(os.getcwd())
dirs_path = os.getcwd() # 设置路径
dirs = os.listdir(dirs_path) # 获取指定路径下的文件
for video_name in dirs: # 循环读取路径下的文件并筛选输出
if os.path.splitext(video_name)[1] == ".avi": # 筛选mp4文件
print(video_name) # 输出所有的mp4文件
break
output_img_path = './1VIS/'
if not os.path.exists(output_img_path):
os.mkdir(output_img_path)
else:
# 删除原有目录
shutil.rmtree(output_img_path)
# 创建一个新目录
os.mkdir(output_img_path)
cap = cv2.VideoCapture(video_name)
#videoFPS=cap.get(cv2.CAP_PROP_FPS)
#print (videoFPS)
cnt = 1
print ("start")
while(1):
ret, frame = cap.read()
# show a frame
if ret is True:
print(frame.shape)
cnt=cnt+1
print(cnt)
if cnt % 10 == 0:
cropped = frame[20:320, 15:260] # 裁剪坐标为[y0:y1, x0:x1]
output_name = "%s%s%s"%(output_img_path,str(int(cnt/10)),'.bmp')
cv2.imwrite(output_name, cropped)
else:
break
print("finished")
cap.release()
cv2.destroyAllWindows()