# Keypoint based image registration using ImageTransformations.jl warp

**URL:** <https://discourse.julialang.org/t/keypoint-based-image-registration-using-imagetransformations-jl-warp/119158>\
**Category:** Signal and Image Processing\
**Tags:** coordinate-transform, juliaimages\
**Created:** [September 7, 2024, 5:12am UTC](https://discourse.julialang.org/t/keypoint-based-image-registration-using-imagetransformations-jl-warp/119158 "2024-09-07T05:12:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LorMam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lormam/32/211828_2.png) [@LorMam](https://discourse.julialang.org/u/LorMam)\
**Post date:** [September 7, 2024, 5:12am UTC](https://discourse.julialang.org/t/keypoint-based-image-registration-using-imagetransformations-jl-warp/119158/1 "2024-09-07T05:12:19Z")

</div>

I have given two images that I want to warp on top of on another based on a number (here 6) of key points. The key point coordinates are measured using ImageJ.

![test_img_1](https://global.discourse-cdn.com/julialang/original/3X/1/5/15527b13d369a218a06585db7a95e8c41fea0baf.png)  
 ![test_img_2](https://global.discourse-cdn.com/julialang/original/3X/1/0/10c56fb6e7cdb021624116cd686e35a36149dfab.png)

First, I load the images, and the key points.

```julia
using Images 
img1 = Images.load("test_img_1.png");
img2 = Images.load("test_img_2.png");

img1_points = [209.1667 164.5; 139.5 124.1667; 141.1667 201.5; 88.1667 80.8333; 83.1667 164.8333; 82.1667 231.8333]
img2_points = [203.8333 277.1667; 247.5 196.1667; 158.5 193.1667; 296.1667 124.1667; 201.1667 125.8333; 123.8333 124.8333];

```

Using Julia’s linear solve, I get the affine linear transform:

```julia
using ImageTransformations, PyPlot, StaticArrays, CoordinateTransformations
X = hcat(img1_points, ones(6,1))
Y = img2_points

c = (X \ Y)'
b = c[:, 3]
A = c[:, 1:2]

```

For the images and keypoints I gave above, I get `A = [0.0197 -1.1425; 1.2220 0.03]; b = [386.975, 17.312];`

Now I define an AffineMap using CoordinateTransformations.jl `tform = AffineMap(A, SVector(b...))` and confirm that it maps the original points onto one another.

```julia
figure(figsize=(2,2))
for i in 1:6
    scatter(img2_points[i,:]..., c="blue", s=2)
    scatter(tform(img1_points[i,:])..., c="red", s=2)
end
xlim(0,446); ylim(410,0);

```

![image_2024-09-07_050745088](https://global.discourse-cdn.com/julialang/original/3X/3/a/3af04b46c78e207ad83415c9d9a859ee528c6217.png)

However, when I now try to warp my images onto one another, the key points do not match (bottom-right image).

`img1w = warp(img1, tform, axes(img1))`

output for `img1w .+ img2`  
 ![image_2024-09-07_050959828](https://global.discourse-cdn.com/julialang/original/3X/e/4/e43ba3380738f2223197755b5815513e3ead002f.png)

I tried many different images, and it seems that the rotation generally matches, but the scale and translation are off. Maybe I am missing something extremely simple here, but I would appreciate any help to why the image warping does not work in the way I expect it to.

---

<div class="post-metadata">

**Author:** ![LorMam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lormam/32/211828_2.png) [@LorMam](https://discourse.julialang.org/u/LorMam)\
**Post date:** [September 12, 2024, 10:23am UTC](https://discourse.julialang.org/t/keypoint-based-image-registration-using-imagetransformations-jl-warp/119158/2 "2024-09-12T10:23:50Z")

</div>

OK, I found the solution myself now.  
Basically this: [Ordering of axes & dimensions · Issue #173 · JuliaImages/juliaimages.github.io · GitHub](https://github.com/JuliaImages/juliaimages.github.io/issues/173)  
So apparently the x and y axes of the warp function are swapped.  
Therefore creating the linear transformation using `img1_points[:, [2,1]]` and `img2_points[:, [2,1]]` works.
