编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
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):
            # 计算波动系数 (范围0-1)
            wave = (math.sin((x + time_param)*0.05) + math.sin((y + time_param)*0.05))*0.5 + 0.5
            
            # 计算RGB通道值 (增强蓝色通道)
            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

# 1. 读取星空图
img = cv2.imread('D:\LC\星空\1.jpg')  # 替换为您的星空图路径
if img is None:
    raise FileNotFoundError("无法加载图像,请检查路径")

# 获取当前时间戳
current_time = datetime.now().timestamp()

# 2. 创建视频输出
height, width = img.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*'mp4v')  # 或使用'XVID'
video_out = cv2.VideoWriter('aurora_effect.mp4', fourcc, 20.0, (width, height))

# 3. 生成视频帧
for i in range(100):  # 生成100帧,约5秒视频
    # 使用当前时间加上帧索引作为时间参数
    time_param = current_time + i * 0.1
    
    # 应用极光效果
    result = create_aurora_effect(img, time_param)
    
    # 写入视频帧
    video_out.write(result)
    
    # 可选:显示处理进度
    print(f"处理进度: {i+1}/100 帧")

# 4. 释放视频资源
video_out.release()
print("视频生成完成: aurora_effect.mp4")

# 5. 可选:显示最后一帧的效果
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()