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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值