星轨

感谢deepseek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2
import numpy as np
import glob
import sys
def pre_process(img):
# 降噪处理(示例使用非局部均值去噪)
img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)

# 对比度增强(示例使用CLAHE)
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
l = clahe.apply(l)
img = cv2.cvtColor(cv2.merge((l,a,b)), cv2.COLOR_LAB2BGR)
return img

def create_star_trail(input_path, output_file):
"""
合成星轨图像
:param input_path: 输入图像路径(支持通配符,如 'input/*.jpg')
:param output_file: 输出文件名
"""
# 获取按文件名排序的图像文件列表
img_files = sorted(glob.glob(input_path))
if not img_files:
print("未找到输入图像,请检查路径是否正确")
return

# 初始化星轨合成图像
star_trail = None

for idx, file in enumerate(img_files):
# 读取图像
img = cv2.imread(file)
# pre_process(img)
if img is None:
print(f"警告:无法读取图像 {file},已跳过")
continue

# 首次初始化星轨图像
if star_trail is None:
star_trail = img.copy().astype(np.float32)
else:
# 最大值叠加法(保留每个像素最亮值)
star_trail = np.maximum(star_trail, img.astype(np.float32))

# 显示处理进度
print(f"已处理 {idx+1}/{len(img_files)} 张图像", end='\r')

temp = np.clip(star_trail, 0, 255).astype(np.uint8)
cv2.imwrite(sys.path[0] + f'/temp/{idx}.jpg', temp)

if star_trail is None:
print("错误:未找到有效输入图像")
return

# 转换为8位无符号整型并保存结果
star_trail = np.clip(star_trail, 0, 255).astype(np.uint8)
cv2.imwrite(output_file, star_trail)
print(f"\n星轨合成完成!结果已保存至 {output_file}")

if __name__ == "__main__":
# 使用示例
create_star_trail(
input_path='/Volumes/backup/星空2/修改/*.jpg', # 输入图像路径(按文件名排序)
output_file='star_trail1.jpg'
)