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:





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

  • 2
    $\begingroup$ 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. $\endgroup$ Commented Feb 20, 2015 at 1:05
  • 2
    $\begingroup$ 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. $\endgroup$
    – C. E.
    Commented Feb 20, 2015 at 1:07
  • $\begingroup$ I have updated the question with direct links. $\endgroup$
    – Dargor
    Commented Feb 20, 2015 at 1:16
  • $\begingroup$ 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... $\endgroup$
    – ciao
    Commented Feb 20, 2015 at 1:33
  • 2
    $\begingroup$ 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. $\endgroup$
    – Dargor
    Commented Feb 20, 2015 at 1:55

2 Answers 2



Import the images:

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

Mathematica graphics

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;

Mathematica graphics

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;

Mathematica graphics

Align the images:

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

Mathematica graphics

Erode the images back to normal object size:

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

Mathematica graphics

Binarize the result:

% // Binarize

Mathematica graphics

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).

  • $\begingroup$ The solution is great, but for some reason, ImageAlign[] sometimes fails (not for these images) even when you use TransformationClass->"Translation". $\endgroup$
    – Dargor
    Commented Mar 12, 2015 at 11:31
  • $\begingroup$ @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 $\endgroup$
    – Dargor
    Commented Mar 12, 2015 at 11:37
  • $\begingroup$ 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. $\endgroup$
    – Kardashev3
    Commented Mar 16, 2015 at 8:55

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

First import the img downloaded from your link.

img1 = Import@"D:\\...\\lteiaq94pImage2.png"; 
img2 = Import@"D:\\...\\peadtydo9Image1.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:

img1bin enter image description here


enter image description here

get ImageData:

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

chose a sample for align:

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

which looks like:

enter image description here

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:

enter image description here

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

  • $\begingroup$ Pablo, Have yu done some treatment before stacking images, as dark, flat and bias? $\endgroup$
    – locometro
    Commented Feb 20, 2015 at 10:50
  • $\begingroup$ Yes, the images are reduced with a masterdark and a masterflat. $\endgroup$
    – Dargor
    Commented Feb 20, 2015 at 11:16
  • $\begingroup$ 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. $\endgroup$
    – Dargor
    Commented Feb 20, 2015 at 11:18
  • $\begingroup$ Probably, more reference star would help. Could you use a FOV bigger? $\endgroup$
    – locometro
    Commented Feb 20, 2015 at 11:30
  • $\begingroup$ 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. $\endgroup$
    – Harry
    Commented Feb 20, 2015 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.