aoles / EBImage

:art: Image processing toolbox for R
71 stars 28 forks source link

Question about affine transformation #44

Open mkeays opened 5 years ago

mkeays commented 5 years ago

I am trying out the affine() function with a simple example matrix, and I noticed something that puzzled me. It seems to add an extra row and column to the transformed matrix even if the scaling factors are both 1:

> pre_aff <- matrix( rep( 1, 16 ), nrow = 4 )
> pre_aff
     [,1] [,2] [,3] [,4]
[1,]    1    1    1    1
[2,]    1    1    1    1
[3,]    1    1    1    1
[4,]    1    1    1    1
> mat <- matrix( c( 1, 0, 2, 0, 1, 3 ), nrow = 3 )
> mat
     [,1] [,2]
[1,]    1    0
[2,]    0    1
[3,]    2    3
> affine( pre_aff, mat, output.dim = c( 10, 10 ), antialias = FALSE )
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    0    0    1    1    1    1    1    0    0     0
 [3,]    0    0    1    1    1    1    1    0    0     0
 [4,]    0    0    1    1    1    1    1    0    0     0
 [5,]    0    0    1    1    1    1    1    0    0     0
 [6,]    0    0    1    1    1    1    1    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

Is this expected behaviour? I thought that the above transformation matrix would simply translate the matrix to a new position but leave the dimensions the same as before the transformation.

mkeays commented 5 years ago

I just noticed that applying the affine() function with antialias = TRUE does not do this:

> affine( pre_aff, mat, output.dim = c( 10, 10 ), antialias = TRUE )
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    0    0    0    0    0    0     0
 [2,]    0    0    0    0    0    0    0    0    0     0
 [3,]    0    0    0    1    1    1    1    0    0     0
 [4,]    0    0    0    1    1    1    1    0    0     0
 [5,]    0    0    0    1    1    1    1    0    0     0
 [6,]    0    0    0    1    1    1    1    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0
aoles commented 5 years ago

Thank you for reporting this! Might be some rounding issue occurring in bilinear filtering step, will need to investigate.

If you are interested in performing pixel transformations which have a 1:1 pixel mapping between the input and the output such as translation by whole pixel coordinates or rotation by multiplies of 90 degrees consider using affine() with filter = "none". Another option is to use wrapper functions translate() or rotate() which set the appropriate arguments to for you.

Cheers, Andrzej

mkeays commented 5 years ago

Hi Andrzej,

Thanks very much for looking at this! I am quite new to this sort of analysis so I was trying out the function with a simple example to see what the result would be. I have a dataset from a collaborator with images and a transformation matrix defining an affine transform to align them. I'll try out the function with filter = "none" as you suggest.

Thanks, Maria