from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import os
import glob
import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageFilter, ImageChops
def rotate_bound1(image, angle):
'''
. 旋转图片
. @param image opencv读取后的图像
. @param angle (逆)旋转角度
'''
# img = cv2.imread("img/1.jpg")
(h, w) = image.shape[:2] # 返回(高,宽,色彩通道数),此处取前两个值返回
# 抓取旋转矩阵(应用角度的负值顺时针旋转)。参数1为旋转中心点;参数2为旋转角度,正的值表示逆时针旋转;参数3为各向同性的比例因子
M = cv2.getRotationMatrix2D((w / 2, h / 2), -angle, 1.0)
# 计算图像的新边界维数
newW = int((h * np.abs(M[0, 1])) + (w * np.abs(M[0, 0])))
newH = int((h * np.abs(M[0, 0])) + (w * np.abs(M[0, 1])))
# 调整旋转矩阵以考虑平移
M[0, 2] += (newW - w) / 2
M[1, 2] += (newH - h) / 2
# 执行实际的旋转并返回图像
return cv2.warpAffine(image, M, (newW, newH)) # borderValue 缺省,默认是黑色
# def rotation(img):
# image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
# rotation_img = image.rotate(20)#旋转角度
# #rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
# img = cv2.cvtColor(np.asarray(rotation_img),cv2.COLOR_RGB2BGR)
# return img
def gen_video(imgs, saved_path,img_file):
#fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
fourcc = cv2.VideoWriter_fourcc(*'XVID') #opencv版本是3
fps = 15 # 视频每秒24帧
size = imgs[0].shape
size=(size[1],size[0])
# 可以使用cv2.resize()进行修改
video = cv2.VideoWriter(saved_path+'/'+str(img_file[:-4])+'.avi', fourcc, fps, size)
for img in imgs:
video.write(img)
video.release()
cv2.destroyAllWindows()
# 读取视频文件
seq_dir = os.path.expanduser('../')
img_files=glob.glob(seq_dir + '*.avi')
for f, img_file in enumerate(img_files):
videoCapture = cv2.VideoCapture(img_file)
success, frame = videoCapture.read()
frame = np.flipud(frame)
# 通过摄像头的方式
# videoCapture=cv2.VideoCapture(1)
imgs=[]
#读帧
i=0
while success:
imgs.append(frame)
if success:
print('save image:',i)
success, frame = videoCapture.read()
saved_path="./翻转/" #视频保存路径
gen_video(imgs,saved_path,img_file)
视频上下翻转后
最新推荐文章于 2024-07-25 20:42:47 发布