opencv 视频切图片,图片合成视频、视频旋转

计算机视觉的训练任务通常需要很多图像数据来做数据集。常见大数据集如ImageNet, VOC,COCO,等等。我们要完成自己特定场合的识任务,就需要收集或采集场景数据,涉及到一些基本的图像处理方法。我遇到或需要使用到的图像处理方法大概有以下几种:

1.视频切图片(video_to_image.py)

import cv2

vc = cv2.VideoCapture("/home/ba/Videos/video.mp4")  # 读入视频文件,命名cv
n = 1  # 计数

if vc.isOpened():  # 判断是否正常打开
    rval, frame = vc.read()
else:
    rval = False

timeF = 1  # 视频帧计数间隔频率

i = 0
while rval:  # 循环读取视频帧
    rval, frame = vc.read()
    if (n % timeF == 0):  # 每隔timeF帧进行存储操作
        i += 1
        print(i)
        cv2.imwrite("/home/bova/Desktop/open/video/{}.jpg".format(i),
                   frame)  # 存储为图像
    n = n + 1
    cv2.waitKey(1)
vc.release()

2.视频旋转(video_rotate.py)

有时候,遇到视频是歪的,需要将视频旋转到正常视角,然后再使用1中的方法。这里涉及到opencv的transpose函数

import numpy as np
import cv2

cap = cv2.VideoCapture("/home/bova/Videos/demo.mp4")
fps=25
size=(1080,1920)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('/home/bova/Videos/output.mp4',fourcc, fps, size)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.transpose(cv2.flip(frame,0)) #0是顺时针90度,-1是逆时针90度

        # 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()

3.视频旋转切图片(video_to_image_rotate.py

如果不需要纠正视频,可以直接将视频旋转切片,其实是1和2的组合。

import cv2

vc = cv2.VideoCapture("/home/bova/Desktop/demo.mp4")  # 读入视频文件,命名cv
n = 1  # 计数

if vc.isOpened():  # 判断是否正常打开
    rval, frame = vc.read()
else:
    rval = False

timeF =1  # 视频帧计数间隔频率

i = 0
while rval:  # 循环读取视频帧
    rval, frame = vc.read()
    if (n % timeF == 0):  # 每隔timeF帧进行存储操作
        i += 1
        print(i)
        cv2.imwrite("/home/bova/Desktop/demo/{}.jpg".format(i),
                   cv2.transpose(cv2.flip(frame,0)))  # 存储为图像,0顺时针90, -1 逆时针90
    n = n + 1
    cv2.waitKey(1)
vc.release()

4.按帧数截取一段视频(pull_video.py)

有时候需要截取视频的某一片段

import cv2
print(cv2.__version__)
videoCapture = cv2.VideoCapture('test.mp4')
 
fps = 25 #保存视频的帧率
size = (1280,720) #保存视频的大小
 
videoWriter =cv2.VideoWriter('video4.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
i = 0
 
while True:
    success,frame = videoCapture.read()
    if success:
        i += 1
        print('i = ',i)
        if(i>=15 and i < 100): #拉取15帧到100帧的视频
            videoWriter.write(frame)
    else:
        print('end')   
        break     

5.图片旋转(image_rotate.py)

from PIL import Image
import os

# 获得文件夹下所有文件
filePath = './img/'
filenames = os.listdir(filePath)

# 指定保存的文件夹
outputPath = './image_rotate/'

# 迭代所有图片
for filename in filenames:
    # 读取图像
    im = Image.open(filePath + filename)

    # 指定逆时针旋转的角度
    im_rotate = im.rotate(90)
    
    # 保存图像
    im_rotate.save(outputPath + filename) 

6.图片合成视频(images_to_video.py)

如果图片的尺寸一样,可以直接合并,但是如果图片大小不一致,需要先resize到相同大小,再合并。

def resize(img_array, align_mode):
    _height = len(img_array[0])
    _width = len(img_array[0][0])
    for i in range(1, len(img_array)):
        img = img_array[i]
        height = len(img)
        width = len(img[0])
        if align_mode == 'smallest':
            if height < _height:
                _height = height
            if width < _width:
                _width = width
        else:
            if height > _height:
                _height = height
            if width > _width:
                _width = width
 
    for i in range(0, len(img_array)):
        img1 = cv2.resize(img_array[i], (_width, _height), interpolation=cv2.INTER_CUBIC)
        img_array[i] = img1
 
    return img_array, (_width, _height)
"""

如果图片尺寸大小一致,可以直接使用下面的代码:

import cv2

img = cv2.imread('/home/ba/Desktop/demo/1.jpg')  #图片所在路径,这里选取第一张获取图片信息

# 设置每秒读取多少张图片
fps = 25
imgInfo = img.shape

# 获取图片宽高度信息
size = (imgInfo[1], imgInfo[0])

videowriter = cv2.VideoWriter("a.mp4",cv2.VideoWriter_fourcc(*"mp4v"),fps,size)
 
for i in range(1,1700):#图片的总数,最好将图片统一命名为
	img = cv2.imread("{}.jpg".format(i))
	videowriter.write(img)
print("end")
	
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值