Python圆形场景转换
题目:
Suppose we wish to create a video transition such that the second video appears under the first video through an opening circle(like a camera iris opening), as in Fig.22. Write a formula to use the correct pixels from the two videos to achieve this special effect. Just write your answer for the red channel.
测试图片
思路
提取两张图片的红色通道亮度值构建灰度图,找到Noble_Grey图像的中心点(x0, y0),以变量t作为循环变量,设(x, y)为Noble_Grey图像中的像素点位置,当(x,y)与(x0, y0)的欧式距离小于10*t时,将该像素点替换为Lena_Grey图像中(x,y)处像素点的值。每次循环都将处理好的图像保存。最后,将所有保存的图像序列生成GIF图即可。
实现结果
代码
import cv2
import numpy as np
import math
from PIL import Image
#通过递增圆的半径,实现转场效果
#frontImg: 转场后图片
#backImg: 转场前图片
def IrisOpen(frontImg, backImg):
size = frontImg.shape
centerX = size[0]/2
centerY = size[1]/2
for t in range(0,30):
for x in range(0,size[0]):
for y in range(0,size[1]):
if math.sqrt((x-centerX)**2 + (y-centerY)**2) <= t*10:
backImg[x, y] = frontImg[x, y]
cv2.imshow("Iris Opening", backImg)
fileName = './images/result' + str(t) + '.jpg'
cv2.imwrite(fileName, backImg)
cv2.waitKey(1)
createGIF()
#利用生成的过程图片创建动态图
def createGIF():
images = []
for x in range(1, 30):
filename = './images/result' + str(x) + '.jpg'
images.append(Image.open(filename))
im = Image.open('./images/result0.jpg')
im.save('result1.gif',save_all=True,append_images=images,loop = 1, duration=1)
def main():
lena = cv2.imread('lena.jpg')
Nobel = cv2.imread('Nobel.jpg')
#获取红色通道的灰度值
lenaRed = lena[:,:,2]
NobelRed = Nobel[:,:,2]
IrisOpen(lenaRed, NobelRed)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()