前言
题目地址:https://docs.opencv.org/4.x/d0/d86/tutorial_py_image_arithmetics.html
题目内容:
Create a slide show of images in a folder with smooth transition between images using cv.addWeighted function
Source Code
# 开发时间:2022/2/20 22:15
# Create a slide show of images in a folder with smooth transition between images using cv.addWeighted function
import os
import time
import cv2 as cv
def get_absolute_filename():
'''
获取指定文件夹下的绝对文件名
:return:
'''
# base_path是图片所在的文件路径
base_path = r'D:\pycharm_professional\PycharmProjects\fisrt_try_of_opencv\opencv\test\test_img'
# absolute_filenames 用于存储图片绝对路径
absolute_filenames = []
# 循环取出文件名并拼接成绝对路径
for filename in os.listdir(base_path):
# print("filename:\n",filename)
absolute_filenames.append(os.path.join(base_path,filename))
# print("absolute_filenames:\n",absolute_filenames)
return absolute_filenames
def get_img(filenames):
'''
获取图片对象
:param filenames:
:return:
'''
img = cv.imread(filenames[0])
print("filenames[0]\n",filenames[0])
print("img.shape:\n",img.shape)
del filenames[0]
return img
def read_img_smooth_transition():
'''
平滑的阅读图片
:return:
'''
global k
filenames = get_absolute_filename()
while True:
# 获取第一个图片对象
try:
img1 = get_img(filenames)
img1 = img1[0:480,0:600] # 取出图片指定大小的部分
except:
break
# 获取第二个图片对象
try:
img2 = get_img(filenames)
img2 = img2[0:480, 0:600] # 取出图片指定大小的部分
except:
break
i = 0
for j in range(11):
# 一定要注意叠加前需要保证两张图片的尺寸相同 where arrays have the same size and the same number of channels
dst = cv.addWeighted(img1,i,img2,1-i,0)
i = 0.1 * j
cv.imshow("dst",dst)
k = cv.waitKey(25) & 0xFF # 指定每帧显示25毫秒
if k == 27: # 按esc键退出
break
if k == 27: # 按esc键退出
break
if __name__ == "__main__":
# get_absolute_filename()
read_img_smooth_transition()
cv.destroyAllWindows()
Result
效果就像小视频一样,这里就不放视频演示了,直接粘贴下来使用的时候注意要修改路径。
总结
参考代码地址:
https://docs.opencv.org/4.x/d0/d86/tutorial_py_image_arithmetics.html