读取视频文件转换到指定分辨率
因为模型需要指定分辨率,但包括格式工厂内的软件均不提供指定分辨率,没办法,自己参考编一个python程序
安装完opencv后,要重新启动anaconda
# -*- coding: utf-8 -*-
"""
Spyder 编辑器
这是一个临时脚本文件。
"""
import cv2
#cap = cv2.VideoCapture(r"C:\Users\DELL\Desktop\seg\MOVA0015.mp4")
cap = cv2.VideoCapture("MOVA0014.mp4")
videowriter = cv2.VideoWriter("MOVA0014_512_256"+".mp4", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 25, (512,256))
success, img1 = cap.read()
print(f"img1 = {img1.shape}")
# img = cv2.resize(img1, (512, 256), interpolation=cv2.INTER_LINEAR)
# print(f"img = {img.shape}")
while success:
success, img1 = cap.read()
try:
img = cv2.resize(img1, (512, 256), interpolation=cv2.INTER_LINEAR)
videowriter.write(img)
except:
print(f"img = {img.shape}")
break
维度符合要求
读取视频文件逐帧显示
import numpy as np
import matplotlib.pyplot as plt
import pylab
import imageio
import skimage.io
import numpy as np
import cv2
cap = cv2.VideoCapture('/path/to/your/video.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('image', frame)
k = cv2.waitKey(20)
#q键退出
if (k & 0xff == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
计算图片集中值
import os
import numpy as np
import cv2
# 这个是你图片的根目录,注意不要带中文路径,楼主就因为这个调试了很久。
image_path = r'C:\Users\DELL\Desktop\seg\val\lindau'
file_names = os.listdir(image_path)
count = 0
mean = np.zeros(3, np.int64)
for i in file_names[1:]:
print(i)
img = cv2.imread(image_path + '/' + i)
#print(img)
count += 1
mean += np.sum(img, axis=(0, 1)).astype(int)
h, w = img.shape[0:-1]
print(h, w, count)
means = mean / (1.0 * h * w * count)
print('b, g, r = ', means)
0003, 路较多
读取摄像头并存盘
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.FOURCC(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
def flip(src, flipCode, dst=None)
src:输入图像
flipCode:flipCode 一个标志来指定如何翻转数组;0表示上下翻转,正数表示左右翻转,负数表示上下左右都翻转。
dst:输出图像