Alignment of several images

asked 2018-10-20 08:06:19 -0500

HaVasder gravatar image

updated 2018-10-20 10:32:38 -0500

Hei,

I'm trying to align several images of the night sky. For example I have 30 (4000,4000) arrays which represent the 30 images. Every of these 30 images is a little bit shifted since the sky moved in between the single pictures. I now want to stack them to get a brighter picture. Every code that I find online has its problems and wont run. Either some function isn't in the cv2 package anymore (cvtColor for example) or I get some cryptic errormessages that I cant figure out how to fix. I'm wondering if there is a code that just takes two/n arrays and "aligns" them and returns a set of arrays which then can just be added so that the single images in this list/set of arrays are in alignment that works in python3 ?
I'm working on Ubuntu.

Hope someone can help me with that.

Thanks a lot !


import numpy as np
import cv2
from matplotlib import pyplot as plt
from astropy.io import fits

"""Reads fits data and converts it to ndarray of shape (4000.4000) with values between 0 and 64000 (unimportant for the problem)"""
def open_fits(groundfile_name, number,color=None):
    names=groundfile_name
    lisi=[]
    number+=1
    anzahl=number
    if color==None:
        if anzahl >=10:
            for i in range(1,10):
                name=names+'-00'+str(i)+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
            for i in range(10,anzahl):
                name=names+'-0'+str(i)+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
        else: 
            for i in range(1,anzahl):
                name=names+'-00'+str(i)+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
    else:
        if anzahl >=10:
            for i in range(1,10):
                name=names+'-00'+str(i)+'_'+color+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
            for i in range(10,anzahl):
                name=names+'-0'+str(i)+'_'+color+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
        else: 
            for i in range(1,anzahl):
                name=names+'-00'+str(i)+'_'+color+'.fit'
                data=fits.open(name)
                data=data[0].data
                lisi.append(data)
    return lisi





"""Copied from readthedocs opencv python tutroals.pdf p188"""
"""This part should take two images, comapre them, rotate and shift one image and then add it to the second one so that the same stars are overlapping"""
img1=cv2.imread('form.jpg',cv2.IMREAD_GRAYSCALE) #testimages as jpg
img2=cv2.imread('form2.jpg',cv2.IMREAD_GRAYSCALE)
orb=cv2.ORB_create()
kp1,des1=orb.detectAndCompute(img1,None) #Here I normaly want to input the result of open_fits and not an jpg
kp2,des2=orb.detectAndCompute(img2,None)
bf=cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches=bf.match(des1,des2)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
good=[]
for m,n in matches:
    if m.distance<0.7*n.distance:
        good.append ...
(more)
edit retag flag offensive close merge delete

Comments

In my opinion, night sky is not really appropriate for feature-based image registration (mostly textureless).

You can try with photogrammetry approach if the keypoint-based approach is not successful:

Eduardo gravatar imageEduardo ( 2018-10-23 09:37:00 -0500 )edit