---
title: "ImageAlign"
language: "en"
type: "Symbol"
summary: "ImageAlign[ref, image] returns a version of image that is aligned with the reference image ref. ImageAlign[ref, {image1, ..., imagen}] gives the result of aligning each of the imagei with the reference image ref. ImageAlign[{image1, ..., imagen}] uses image1 as the reference image."
keywords: 
- align
- image align
- transform
- image registration
- registration
- correspondences
- affine
- rigid
- perspective
- similarity
canonical_url: "https://reference.wolfram.com/language/ref/ImageAlign.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Geometric Operations"
    link: "https://reference.wolfram.com/language/guide/ImageGeometry.en.md"
  - 
    title: "Image Processing & Analysis"
    link: "https://reference.wolfram.com/language/guide/ImageProcessing.en.md"
  - 
    title: "Computer Vision"
    link: "https://reference.wolfram.com/language/guide/ComputerVision.en.md"
  - 
    title: "3D Images"
    link: "https://reference.wolfram.com/language/guide/3DImages.en.md"
  - 
    title: "Image Composition"
    link: "https://reference.wolfram.com/language/guide/ImageComposition.en.md"
  - 
    title: "Image Computation for Microscopy"
    link: "https://reference.wolfram.com/language/guide/ImageComputationForMicroscopy.en.md"
  - 
    title: "Computational Photography"
    link: "https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md"
  - 
    title: "Optimization"
    link: "https://reference.wolfram.com/language/guide/Optimization.en.md"
  - 
    title: "Image Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "ImageStitch"
    link: "https://reference.wolfram.com/language/ref/ImageStitch.en.md"
  - 
    title: "ImageCorrespondingPoints"
    link: "https://reference.wolfram.com/language/ref/ImageCorrespondingPoints.en.md"
  - 
    title: "FindGeometricTransform"
    link: "https://reference.wolfram.com/language/ref/FindGeometricTransform.en.md"
  - 
    title: "ImageKeypoints"
    link: "https://reference.wolfram.com/language/ref/ImageKeypoints.en.md"
  - 
    title: "ImageTransformation"
    link: "https://reference.wolfram.com/language/ref/ImageTransformation.en.md"
  - 
    title: "ImageCorrelate"
    link: "https://reference.wolfram.com/language/ref/ImageCorrelate.en.md"
---
# ImageAlign

ImageAlign[ref, image] returns a version of image that is aligned with the reference image ref.

ImageAlign[ref, {image1, …, imagen}] gives the result of aligning each of the imagei with the reference image ref.

ImageAlign[{image1, …, imagen}] uses image1 as the reference image.

## Details and Options

* ``ImageAlign[image1, image2]`` finds a transformation of ``image2`` that registers it with ``image1`` and returns the result of applying this transformation to ``image2``.

* ``ImageAlign`` works with arbitrary 2D and 3D images.

* Resulting images have the same dimensions as the reference image.

* Use ``FindGeometricTransform`` to compute the geometric transformation used for alignments.

* The following options can be specified:

|                      |           |                                     |
| -------------------- | --------- | ----------------------------------- |
| Background           | Automatic | background value                    |
| Method               | Automatic | alignment method to use             |
| TransformationClass  | Automatic | geometrical relation between images |

* Possible settings for the ``Method`` option are:

|                             |                                                            |
| --------------------------- | ---------------------------------------------------------- |
| Automatic                   | automatically choose a suitable method                     |
| "Keypoints"                 | use corresponding keypoints                                |
| {"Keypoints", method}       | use an ImageCorrespondingPoints method                     |
| "MeanSquareGradientDescent" | minimize the mean square distance of corresponding pixels  |
| "Fourier"                   | Fourier-based registration                                 |
| "FourierBlurInvariant"      | blur invariant Fourier-based registration                  |

* With the default setting ``TransformationClass -> Automatic``, ``ImageAlign`` attempts to find the simplest possible transformation.

* Possible transformations in order of increasing complexity are:

|               |                                       |
| ------------- | ------------------------------------- |
| "Translation" | translation only                      |
| "Rigid"       | translation and rotation              |
| "Similarity"  | translation, rotation, and scaling    |
| "Affine"      | linear transformation and translation |
| "Perspective" | linear fractional transformation      |

## Examples (21)

### Basic Examples (2)

Align a part of an image with the full image:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

---

Align two 3D volumes:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

### Scope (2)

Align a list of three images to a reference image:

```wl
In[1]:= ImageAlign[[image], {[image], [image], [image]}, IconizedObject[«...»]]

Out[1]= {[image], [image], [image]}
```

---

Align a list of three images, using the first image as the reference:

```wl
In[1]:= ImageAlign[{[image], [image], [image]}, IconizedObject[«...»]]

Out[1]= {[image], [image], [image]}
```

Three images are returned:

```wl
In[2]:= Length[%]

Out[2]= 3
```

### Options (11)

#### Background (1)

By default, a transparent background is used:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

Specify a different background color:

```wl
In[2]:= ImageAlign[[image], [image], Background -> Gray]

Out[2]= [image]
```

#### Method (5)

By default, ``ImageAlign`` uses an automatic combination of methods and initializations:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

---

The ``"Keypoints"`` method works well with natural 2D photos:

```wl
In[1]:= ref = [image];image = [image];

In[2]:= ImageAlign[ref, image, Method -> "Keypoints"]

Out[2]= [image]
```

Use "KAZE" keypoints:

```wl
In[3]:= ImageAlign[ref, image, Method -> {"Keypoints", "KAZE"}]

Out[3]= [image]
```

Use all keypoints detected by multiple methods:

```wl
In[4]:= ImageAlign[ref, image, Method -> {"Keypoints", {"ORB", "KAZE"}}]

Out[4]= [image]
```

---

The ``"MeanSquareGradientDescent"`` method uses dense distances between images:

```wl
In[1]:= ImageAlign[[image], [image], Method -> "MeanSquareGradientDescent", TransformationClass -> "Affine"]

Out[1]= [image]
```

This method typically needs a large initial overlap between images:

```wl
In[2]:= ImageAlign[[image], ImagePad[[image], 30], Method -> "MeanSquareGradientDescent", TransformationClass -> "Affine"]

Out[2]= [image]
```

---

The ``"Fourier"`` method can align images even with a small overlap:

```wl
In[1]:= {i1, i2} = {[image], [image]};

In[2]:= res = ImageAlign[i1, i2, Method -> "Fourier"]

Out[2]= [image]

In[3]:= HighlightImage[ImageCompose[i1, {res, .5}], {FaceForm[], EdgeForm[Thick], AlphaChannel[res]}]

Out[3]= [image]
```

---

Use the ``"FourierBlurInvariant"`` method in the presence of noise and motion blur:

```wl
In[1]:= {i1, i2} = {[image], [image]};

In[2]:= ImageAlign[i1, i2, Method -> "FourierBlurInvariant"]

Out[2]= [image]

In[3]:= ImageCompose[i1, {%, .5}]

Out[3]= [image]
```

#### TransformationClass (5)

Restrict the transformation to only translation:

```wl
In[1]:= ImageAlign[[image], [image], TransformationClass -> "Translation"]

Out[1]= [image]
```

---

``"Rigid"`` transformation allows for rotations in addition to translations:

```wl
In[1]:= ImageAlign[[image], [image], TransformationClass -> "Rigid"]

Out[1]= [image]
```

---

``"Similarity"`` transforms allow for translation, rotation, and scaling:

```wl
In[1]:= ImageAlign[[image], [image], TransformationClass -> "Similarity"]

Out[1]= [image]
```

---

Allow any affine transformation:

```wl
In[1]:= ImageAlign[[image], [image], TransformationClass -> "Affine"]

Out[1]= [image]
```

---

Perspective transformation:

```wl
In[1]:= ImageAlign[[image], [image], TransformationClass -> "Perspective"]

Out[1]= [image]
```

### Applications (4)

Align an image with its mirror image:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

---

Overlay the aligned images:

```wl
In[1]:= i1 = [image];i2 = [image];

In[2]:=
aligned = ImageAlign[i1, i2];
ImageCompose[aligned, {i1, .5}]

Out[2]= [image]
```

---

Align two different images of the same object:

```wl
In[1]:= ImageAlign[[image], [image]]

Out[1]= [image]
```

---

Align images before performing focus stacking:

```wl
In[1]:= images = {[image], [image], [image], [image]};

In[2]:= aligned = ImageAlign[images]

Out[2]= [image]
```

Perform focus stacking:

```wl
In[3]:= ImageFocusCombine[aligned]

Out[3]= [image]
```

Compare with the stacking without alignment:

```wl
In[4]:= ImageFocusCombine[images]

Out[4]= [image]
```

### Properties & Relations (2)

For aligning images using a specific set of corresponding points, use ``FindGeometricTransform`` :

```wl
In[1]:=
{i1, i2} = {[image], [image]};pts1 = {{47.5, 151.5}, {149.5, 151.5}, {150.5, 50.5}, {47.5, 52.5}};
pts2 = {{64.5, 185.5}, {164.5, 173.5}, {184.5, 49.5}, {68.5, 54.5}};

In[2]:= {e, t} = FindGeometricTransform[pts1, pts2]

Out[2]=
{9.281079204581667`*^-13, TransformationFunction[(|              |              |          |
| ------------ | ------------ | -------- |
| 0.694758     | -0.0245116   | -3.62636 |
| -0.0264695   | 0.531574     | 19.9583  |
| -0.000713898 | -0.000984443 | 1.       |)]}
```

Transform the image with ``ImagePerspectiveTransformation`` :

```wl
In[3]:= ImagePerspectiveTransformation[i2, t, DataRange -> Full, Padding -> 0]

Out[3]= [image]
```

---

Use ``FindGeometricTransform`` to find the geometric transformation that aligns images:

```wl
In[1]:= i1 = [image];i2 = [image];

In[2]:= {e, tr} = FindGeometricTransform[i1, i2]

Out[2]=
{0.341998, TransformationFunction[(|            |             |          |
| ---------- | ----------- | -------- |
| 0.537851   | -0.0101117  | 153.442  |
| -0.13068   | 0.916323    | 0.645807 |
| -0.0016047 | 0.000214252 | 1.       |)]}
```

Stitch images together by transforming one and composing on top of the other one:

```wl
In[3]:=
{w, h} = ImageDimensions[i2];
tmp = ImagePerspectiveTransformation[i2, tr, DataRange -> Full, PlotRange -> {{0, First@tr[{w, 0}]}, {0, h}}];
ImageCompose[tmp, {i1, .7}, Round@({w, h} / 2)]

Out[3]= [image]
```

## See Also

* [`ImageStitch`](https://reference.wolfram.com/language/ref/ImageStitch.en.md)
* [`ImageCorrespondingPoints`](https://reference.wolfram.com/language/ref/ImageCorrespondingPoints.en.md)
* [`FindGeometricTransform`](https://reference.wolfram.com/language/ref/FindGeometricTransform.en.md)
* [`ImageKeypoints`](https://reference.wolfram.com/language/ref/ImageKeypoints.en.md)
* [`ImageTransformation`](https://reference.wolfram.com/language/ref/ImageTransformation.en.md)
* [`ImageCorrelate`](https://reference.wolfram.com/language/ref/ImageCorrelate.en.md)

## Related Guides

* [Geometric Operations](https://reference.wolfram.com/language/guide/ImageGeometry.en.md)
* [Image Processing & Analysis](https://reference.wolfram.com/language/guide/ImageProcessing.en.md)
* [Computer Vision](https://reference.wolfram.com/language/guide/ComputerVision.en.md)
* [3D Images](https://reference.wolfram.com/language/guide/3DImages.en.md)
* [Image Composition](https://reference.wolfram.com/language/guide/ImageComposition.en.md)
* [Image Computation for Microscopy](https://reference.wolfram.com/language/guide/ImageComputationForMicroscopy.en.md)
* [Computational Photography](https://reference.wolfram.com/language/guide/ComputationalPhotography.en.md)
* [`Optimization`](https://reference.wolfram.com/language/guide/Optimization.en.md)
* [Image Computation: Update History](https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md)

## History

* [Introduced in 2010 (8.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn80.en.md) \| [Updated in 2014 (10.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn100.en.md) ▪ [2015 (10.2)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn102.en.md) ▪ [2016 (11.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn110.en.md) ▪ [2017 (11.1)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn111.en.md) ▪ [2021 (13.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn130.en.md)