Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nagonzalezf authored Jan 16, 2023
1 parent d3f30f8 commit 4a04376
Showing 1 changed file with 62 additions and 55 deletions.
117 changes: 62 additions & 55 deletions starsalign/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,55 @@
StarsAlign
==========
A package for aligning and comparing astronomical images.
A tool for aligning and comparing astronomical images.
Specially developed to work over float32 images with a large intensity range, typically found in astronomical imaging.
Specially developed to work over float32 high density images with a large range, typically found in astronomical imaging.
This package contains functions such as ```align()``` and ```diff()```that aligns and compute the difference of two single channel images using SIFT algorithm and FLANN based matcher.
This package contains functions such as ```align()``` and ```diff()``` that aligns and compute the difference of two multi channel images using SIFT algorithm and FLANN based matcher.
You can also try functions ```ultra_align()``` and ```ultra_diff()``` for extreme precision, but it may require more resources
You can also try functions ```fast_align()``` and ```fast_diff()``` for single channel images, this method is faster but it may be less accurate for lower resolution images.
Example:
===
>>> import numpy as np
>>> import starsalign as stal
>>>
>>> diff_image = stal.ultra_diff(ref_image, science_image)
>>> aligned_image = stal.ultra_align(ref_image, science_image)
>>> import starsalign as sa
>>> aligned_image = sa.align(ref_image, science_image)
>>> diff_image = sa.diff(ref_image, science_image)
Important:
===
This package was specifically designed to work with images that have a high amount of information, such as 4096x2048 pixels, with float32 data type, and a range of values between -155.45811 and 43314.49.
If your images have a high density of information, then the align() and diff() functions should work well. However, if you are working with images of lower resolution or lower information density, it is recommended to use the ultra_align() and ultra_diff() functions.
If your images have a high density of information, then the fast_align() and fast_diff() functions should work well. However, if you are working with images of lower resolution or lower information density, it is recommended to use the align() and diff() functions.
Warning: Using the ultra_align() and ultra_diff() functions on high-density information and high-resolution images may result in prolonged waiting times.
The align() and diff() functions are slower, but supports multi channel images and performs calculations using more information wich results in extreme precision, but it may require more resources.
Warning: Using the align() and diff() functions on high-density information and high-resolution images may result in prolonged waiting times.
"""

__version__ = "1.0.7"
__version__ = "1.0.13"

import os
import cv2 as cv
import numpy as np
import tempfile

def align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
def fast_align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
"""
align()
fast_align()
===
Aligns the science_image to the reference image by finding keypoints and matching them using SIFT algorithm, then finds the homography between the two images, and applies a perspective warp to align the science_image.
Parameters:
ref_image (np.ndarray): The reference image, to which the science_image will be aligned.
science_image (np.ndarray): The science image that will be aligned to the reference image.
Returns:
np.ndarray: The aligned science image.
Example:
===
>>> import starsalign as stal
>>> aligned_image = stal.align(ref_image, science_image)
>>> import starsalign as sa
>>> aligned_image = sa.fast_align(ref_image, science_image)
"""
ref_image_uint8 = np.interp(ref_image, (ref_image.min(), ref_image.max()), (0, 255)).astype(np.uint8)
science_image_uint8 = np.interp(science_image, (science_image.min(), science_image.max()), (0, 255)).astype(np.uint8)
Expand All @@ -76,44 +77,41 @@ def align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:

dx = M[0, 2]
dy = M[1, 2]
print(f'The displacement on the x-axis is of {dx} pixels')
print(f'The displacement on the y-axis is of {dy} pixels')

print(f'The displacement on the x-axis is of {dx} pixels. (fast method)')
print(f'The displacement on the y-axis is of {dy} pixels. (fast method)')

height = science_image.shape[0]
width = science_image.shape[1]
aligned_image = cv.warpPerspective(science_image, M, (width, height))

return aligned_image

def ultra_align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
def align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
"""
ultra_align()
align()
===
A more advanced version of the alignment function that uses more channels to perform the calculations, which results in a more precise alignment.
A more advanced version of the fast_align() function that holds more information to perform the calculations, which results in a more precise alignment.
Parameters:
ref_image (np.ndarray): The reference image, to which the science_image will be aligned.
science_image (np.ndarray): The science image that will be aligned to the reference image.
Returns:
np.ndarray: The aligned science image.
Example:
===
>>> import starsalign as stal
>>> aligned_image = stal.ultra_align(ref_image, science_image)
>>> import starsalign as sa
>>> aligned_image = sa.align(ref_image, science_image)
"""
cv.imwrite('ref_buffer.png', ref_image)
cv.imwrite('science_buffer.png', science_image)
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as ref_buffer:
cv.imwrite(ref_buffer.name, ref_image)
ref_image_uint8 = cv.imread(ref_buffer.name)

ref_image_uint8 = cv.imread('ref_buffer.png')
science_image_uint8 = cv.imread('science_buffer.png')

if os.path.exists('ref_buffer.png'):
os.remove('ref_buffer.png')

if os.path.exists('science_buffer.png'):
os.remove('science_buffer.png')
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as science_buffer:
cv.imwrite(science_buffer.name, science_image)
science_image_uint8 = cv.imread(science_buffer.name)

gray1 = cv.cvtColor(ref_image_uint8, cv.COLOR_BGR2GRAY)
gray2 = cv.cvtColor(science_image_uint8, cv.COLOR_BGR2GRAY)
Expand All @@ -140,18 +138,25 @@ def ultra_align(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:

dx = M[0, 2]
dy = M[1, 2]
print(f'The displacement on the x-axis is of {dx} pixels')
print(f'The displacement on the y-axis is of {dy} pixels')

print(f'The displacement on the x-axis is of {dx} pixels. (default method)')
print(f'The displacement on the y-axis is of {dy} pixels. (default method)')

height = science_image.shape[0]
width = science_image.shape[1]
aligned_image = cv.warpPerspective(science_image, M, (width, height))


if os.path.exists(ref_buffer.name):
os.remove(ref_buffer.name)

if os.path.exists(science_buffer.name):
os.remove(science_buffer.name)

return aligned_image

def diff(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
def fast_diff(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
"""
diff()
fast_diff()
===
Computes the difference between two single channel images by first aligning them and then subtracting them.
Expand All @@ -164,19 +169,20 @@ def diff(ref_image: np.ndarray, science_image: np.ndarray) -> np.ndarray:
Example:
===
>>> import starsalign as stal
>>> diff_image = stal.diff(ref_image, science_image)
>>> import starsalign as sa
>>> diff_image = sa.diff(ref_image, science_image)
"""
aligned_image = align(ref_image, science_image)
aligned_image = fast_align(ref_image, science_image)
diff_image = ref_image - aligned_image

return diff_image

def ultra_diff(ref_image, science_image):
def diff(ref_image, science_image):
"""
ultra_diff()
diff()
===
A more advanced version of the difference function that uses more channels to perform the calculations, which results in a more precise alignment.
A more advanced version of the fast_diff() function that holds more information to perform the calculations, which results in a more precise alignment.
Computes the difference between two images by first aligning them and then subtracting them.
Parameters:
Expand All @@ -188,11 +194,12 @@ def ultra_diff(ref_image, science_image):
Example:
===
>>> import starsalign as stal
>>> diff_image = stal.ultra_diff(ref_image, science_image)
>>> import starsalign as sa
>>> diff_image = sa.diff(ref_image, science_image)
"""
aligned_image = ultra_align(ref_image, science_image)
aligned_image = align(ref_image, science_image)
diff_image = ref_image - aligned_image

return diff_image

# https://github.com/nagonzalezf/starsalign

0 comments on commit 4a04376

Please sign in to comment.