import cv2
import numpy as np
import math
import matplotlib.pyplot as plt
import time
from datetime import datetime
def create_aurora_effect(img, time_param=0):
"""
为输入图像创建极光效果
:param img: 输入图像(BGR格式)
:param time_param: 时间参数,用于动画效果
:return: 处理后的图像
"""
rows, cols = img.shape[:2]
aurora = np.zeros_like(img)
for y in range(rows):
for x in range(cols):
wave = (math.sin((x + time_param)*0.05) + math.sin((y + time_param)*0.05))*0.5 + 0.5
r = int(255 * wave * 0.6)
g = int(255 * wave * 0.8)
b = int(255 * wave)
aurora[y, x] = [b, g, r]
blended = cv2.addWeighted(img, 0.7, aurora, 0.3, 0)
return blended
img = cv2.imread('D:\LC\星空\1.jpg')
if img is None:
raise FileNotFoundError("无法加载图像,请检查路径")
current_time = datetime.now().timestamp()
height, width = img.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_out = cv2.VideoWriter('aurora_effect.mp4', fourcc, 20.0, (width, height))
for i in range(100):
time_param = current_time + i * 0.1
result = create_aurora_effect(img, time_param)
video_out.write(result)
print(f"处理进度: {i+1}/100 帧")
video_out.release()
print("视频生成完成: aurora_effect.mp4")
plt.figure(figsize=(12, 6))
plt.subplot(121), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), plt.title('原始星空图')
plt.subplot(122), plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)), plt.title('极光效果(最后一帧)')
plt.tight_layout()
plt.show()