Opencv python 圖片生成視頻的方法示例
本文主要介紹了Opencv圖片生成視頻,分享給大家,具體如下:

生成視頻
import random as rd
import cv2 as cv
import numpy as np
# 保存視頻
class RecordMovie(object):
def __init__(self, img_width, img_height):
self.video_writer = None # 視頻對(duì)象
self.is_end = False # 結(jié)束保存視頻
self.img_width = img_width # 寬度
self.img_height = img_height # 高度
# 創(chuàng)建 視頻寫(xiě)入對(duì)象
def start(self, file_name, freq):
# 創(chuàng)建視頻格式
four_cc = cv.VideoWriter_fourcc(*'mp4v')
img_size = (self.img_width, self.img_height) # 視頻尺寸
# 創(chuàng)建視頻寫(xiě)入對(duì)象
self.video_writer = cv.VideoWriter()
self.video_writer.open(file_name, four_cc, freq, img_size, True)
# 寫(xiě)入圖片幀
def record(self, img):
if self.is_end is False:
self.video_writer.write(img)
# 完成視頻 釋放資源
def end(self):
self.is_end = True
self.video_writer.release()
def move_image(img_src):
img_height, img_width = img_src.shape[:2]
# 隨機(jī) xy平移方向與大小設(shè)置
x_size = rd.randint(-3, 3)
y_size = rd.randint(-3, 3)
# 自定義轉(zhuǎn)換矩陣
transform_matrix = np.float32([[1, 0, x_size], [0, 1, y_size]])
# 執(zhí)行平移
return cv.warpAffine(img_src, transform_matrix, (img_width, img_height))
def main():
# 1.讀取圖片
img_org = cv.imread("img.png", cv.IMREAD_GRAYSCALE)
# 2.顯示圖片
cv.imshow("org", img_org)
cv.namedWindow("shift")
# 3.視頻文件生成
height, width = img_org.shape[:2]
print(height, width)
rm = RecordMovie(width, height)
# 設(shè)置視頻文件名稱 頻率
rm.start("test.mp4", 20)
# 4.圖片寫(xiě)入視頻
for i in range(300):
# 圖片微調(diào)調(diào)整
img_move = move_image(img_org)
img_move = cv.cvtColor(img_move, cv.COLOR_GRAY2RGB)
rm.record(img_move)
cv.imshow("shift", img_move)
key = cv.waitKey(10)
if key == 27: # esc 按鍵
break
# 5.關(guān)閉視頻文件
rm.end()
if __name__ == '__main__':
main()


到此這篇關(guān)于Opencv python 圖片生成視頻的方法示例的文章就介紹到這了,更多相關(guān)Opencv圖片生成視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview)
今天小編就為大家分享一篇詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
python lambda函數(shù)及三個(gè)常用的高階函數(shù)
這篇文章主要介紹了python lambda函數(shù)及三個(gè)常用的高階函數(shù),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車程序
這篇文章主要介紹了Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車程序,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-05-05
Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支
這篇文章主要介紹了Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
python+requests接口自動(dòng)化框架的實(shí)現(xiàn)
這篇文章主要介紹了python+requests接口自動(dòng)化框架的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python Datetime模塊和Calendar模塊用法實(shí)例分析
這篇文章主要介紹了Python Datetime模塊和Calendar模塊用法,結(jié)合實(shí)例形式分析了Python日期時(shí)間及日歷相關(guān)的Datetime模塊和Calendar模塊原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2019-04-04

