视频上下翻转后

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)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值