首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Python堆叠天文图像

使用Python堆叠天文图像
EN

Stack Overflow用户
提问于 2012-02-13 02:47:58
回答 1查看 7.7K关注 0票数 19

我认为这会更容易,但过了一段时间,我终于放弃了,至少在几个小时内……

我想从一组时间流逝的照片中重现这张拖尾恒星的图像。受此启发:

The original author使用VirtualDub拍摄的低分辨率视频帧,并与imageJ结合使用。我想我可以很容易地重现这个过程,但是使用Python使用内存更敏感的方法,所以我可以使用the original high-resolution images来获得更好的输出。

我的算法很简单,一次合并两个图像,然后通过合并结果图像和下一个图像进行迭代。这样做了数百次,并对其进行了适当的加权,以便每个图像对最终结果的贡献都相同。

我是python的新手(我不是专业程序员,这一点很明显),但在我看来,Python图像库是非常标准的,所以我决定使用它(如果你认为其他东西会更好,请纠正我)。

这是我到目前为止所知道的:

#program to blend many images into one
import os,Image
files = os.listdir("./")
finalimage=Image.open("./"+files[0]) #add the first image
for i in range(1,len(files)): #note that this will skip files[0] but go all the way to the last file
  currentimage=Image.open("./"+files[i])
  finalimage=Image.blend(finalimage,currentimage,1/float(i+1))#alpha is 1/i+1 so when the image is a combination of i images any adition only contributes 1/i+1.
  print "\r" + str(i+1) + "/" + str(len(files)) #lousy progress indicator
finalimage.save("allblended.jpg","JPEG")

这做了它应该做的,但结果图像是暗的,如果我只是试图增强它,很明显,由于缺乏像素值的深度,信息丢失了。(我不确定这里的恰当术语是什么,颜色深度,颜色精度,像素大小)。以下是使用低分辨率图像的最终结果:

或者是我尝试的4k x 2k分辨率的照片(来自另一组照片):

因此,我尝试通过设置图像模式来修复它:

firstimage=Image.open("./"+files[0])
size = firstimage.size
finalimage=Image.new("I",size)

但显然Image.blend不接受这种图像模式。

ValueError:图像模式错误

有什么想法吗?

(在与im.point(lambda i: i* 2)组合之前,我还尝试通过乘法使图像“不那么暗”,但结果同样糟糕)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-13 08:54:57

这里的问题是,您平均每个像素的亮度。这可能看起来很合理,但实际上并不是你想要的--明亮的星星将会“平均消失”,因为它们在图像中移动。取以下四个帧:

1000 0000 0000 0000
0000 0100 0000 0000
0000 0000 0010 0000
0000 0000 0000 0001

如果你对这些求平均,你会得到:

0.25 0    0    0
0    0.25 0    0
0    0    0.25 0
0    0    0    0.25

当你需要的时候:

1000
0100
0010
0001

你可以尝试取任何图像中每个像素的最大值,而不是混合图像。如果你有PIL,你可以尝试ImageChops中的lighter功能。

from PIL import ImageChops
import os, Image
files = os.listdir("./")
finalimage=Image.open("./"+files[0])
for i in range(1,len(files)):
    currentimage=Image.open("./"+files[i])
    finalimage=ImageChops.lighter(finalimage, currentimage)
finalimage.save("allblended.jpg","JPEG")

下面是我得到的信息:

编辑:我读了Reddit的帖子,看到他实际上结合了两种方法--一种用于恒星轨迹,另一种用于地球。这里是你尝试的平均值的一个更好的实现,并带有适当的权重。我使用numpy数组作为中间存储,而不是uint8图像数组。

import os, Image
import numpy as np
files = os.listdir("./")
image=Image.open("./"+files[0])
im=np.array(image,dtype=np.float32)
for i in range(1,len(files)):
    currentimage=Image.open("./"+files[i])
    im += np.array(currentimage, dtype=np.float32)
im /= len(files) * 0.25 # lowered brightness, with magic factor
# clip, convert back to uint8:
final_image = Image.fromarray(np.uint8(im.clip(0,255)))
final_image.save('all_averaged.jpg', 'JPEG')

这是这张图片,然后你可以将它与前一张照片中的星迹结合起来。

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9251580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档