# Align stars in multiple images

I hope that you find this question interesting and that this will help others with similar problems as this configuration is not so particular.

### My problem

I have multiple images of a particular region in the sky. These images depict stars. In order to make measures i have to align the images first (to identify the same star in each image, for example). I'm finding this task extreamly difficult. This is what i have tried so far:

• First I tried to use ImageAlign with the two images, but it did not work. As the correction should be a translation, I also tried to giving TransformationClass -> "Translation" as an option, but that did not work.

• The best approach so far is the following:

• Binarize the images to show the stars more clearly.

• Identify the stars using MorphologicalComponents.

• Use FindGeometricTransormation with the coordinates of the stars to find a transformation using the option TransformationClass -> "Translation".

This last approach work iff the images are very closely related.

Here is the link of the actual images and a binarize copy of them:

http://s28.postimg.org/mk78gibi3/Image1.png

http://s28.postimg.org/x5r3sihtn/Image2.png

http://s28.postimg(dot)org/o86dv5pdp/Binarize1.png

http://s28.postimg(dot)org/x4h5z3fzx/Binarize2.png

As the final objective is to automate the process, the less human input to the process the better.

• I have tried this before and I didn't find it very easy in Mathematica. My suggestion would be to use some specialized software like DeepSkyStacker, where someone has already figured out the details for you. – Oleksandr R. Feb 20 '15 at 1:05
• Please post the images in this format: img1 = Import[ "https://www.dropbox.com/s/83hyoz5g62rxiut/Image1.png"]; img2 = Import["https://www.dropbox.com/s/fh25kcmuaij3sus/Image2.png"]; Your current URLs do not work. – C. E. Feb 20 '15 at 1:07
• I have updated the question with direct links. – Dargor Feb 20 '15 at 1:16
• As Oleksandr says, if you're doing this for more than a mathematica experiment, you'll do far better using COTS software. I use something called registar, quite effective... – ciao Feb 20 '15 at 1:33
• I have checked the software you mention but it fails to find the alignment. Also, i appreciate all your suggestions, but i really want to use Mathematica. – Dargor Feb 20 '15 at 1:55

UPDATED VERSION:

Import the images:

img1 = Import[NotebookDirectory[] <> "Image1.png"];
img2 = Import[NotebookDirectory[] <> "Image2.png"];
imgs = {img1, img2};
ImageCollage[imgs]


Binarize the two images using the "MinimumError"-Method, pad the images (hald the size of the original images) and perform an Opening to get rid of binarized background noise:

imgsbin = Opening[ImagePad[Binarize[#, FindThreshold[#, Method -> "MinimumError"]], 512], 1] & /@ imgs;
ImageCollage[imgsbin]


Dilate the binarized stars (original stars are obviously too small for ImageAlign to find an appropriate transformation, 20 seems to work well):

imgsbindil = Dilation[#, 20] & /@ imgsbin;
ImageCollage[imgsbindil]


Align the images:

imgal = ImageAlign[imgsbindil[[1]], imgsbindil[[2]], TransformationClass -> "Rigid", Method -> "Keypoints"];
ColorCombine[{imgsbindil[[1]], imgal}]


Erode the images back to normal object size:

ColorCombine[{Erosion[imgsbindil[[1]], 20], Erosion[imgal, 20]}]


Binarize the result:

% // Binarize


The method works quite well for the provided images, but the result depends on the image quality. This mainly affects stars that are cut by the image border (objects do not overlap properly, see bottom left in the final image).

• The solution is great, but for some reason, ImageAlign[] sometimes fails (not for these images) even when you use TransformationClass->"Translation". – Dargor Mar 12 '15 at 11:31
• @Kardeshev3 Here you have two images in which ImageAlign[] fails. Do you know the reason? s4.postimg.org/qes23r9nv/image.png s17.postimg.org/6ndsk6z8t/image.png – Dargor Mar 12 '15 at 11:37
• I edited the proposed approach to cope with the new provided images. The trick is to dilate the binarized images prior to alignment, since ImageAling obviously needs bigger objects to find an appropriate transformation. – Kardashev3 Mar 16 '15 at 8:55

Those listed below might more like a suggestion than an answer with a lot of handwork.

img1 = Import@"D:\\...\\lteiaq94pImage2.png";


Binarize them with carefully chosed parameters:

img1bin = Erosion[Binarize[img1, 0.2], 0.4];
img2bin = Erosion[Binarize[img2, 0.4], 0.5];


img1bin and img2bin look like:

and

get ImageData:

img1bind = ImageData@img1bin; img2bind = ImageData@img2bin;


chose a sample for align:

Image@img2bind[[500 ;; -1, 1 ;; 500]]


which looks like:

define the coincide function:

data2 = img2bind[[500 ;; -1, 1 ;; 500]];
coincide[x_, y_] :=
Total[data2*img1bind[[500 - y ;; -1 - y, 1 + x ;; 500 + x]], 2]


search for the "most likely coincide area":

posi = Outer[coincide, Range[1, 450, 10], Range[1, 450, 10]]


find the position related to the "most likely coincide area":

Position[posi, Max[posi]]


which returned {{23, 15}}, define

img1cut[x_, y_] :=
Image[img1bind[[500 - y ;; -1 - y, 1 + x ;; 500 + x]]];


then we see

img1cut[231, 151]


gives the almost same four stars in sample:

you can refine the search grid(which is 10) at neighborhood of {231, 151} for more accurate result.

• Pablo, Have yu done some treatment before stacking images, as dark, flat and bias? – locometro Feb 20 '15 at 10:50
• Yes, the images are reduced with a masterdark and a masterflat. – Dargor Feb 20 '15 at 11:16
• This is a very nice approach, but there are some problems. For example, using the coincide function, sometimes the convolution (the product) of a big star of the general image with one of the stars in the reference image gives a maximum bigger than the correct one. As we want this process to be automatic, specify the region is not desirable. – Dargor Feb 20 '15 at 11:18
• Probably, more reference star would help. Could you use a FOV bigger? – locometro Feb 20 '15 at 11:30
• larger sample or larger field of view might be obtain by take larger sample with similar code. such as replace img2bind[[500 ;; -1, 1 ;; 500]]; toimg2bind[[400 ;; -1, 1 ;; 600]];` or more. but avoid the left top white noise area. – Harry Feb 20 '15 at 11:39