mlampros / OpenImageR

Image processing Toolkit in R
https://mlampros.github.io/OpenImageR/
57 stars 10 forks source link

Augmentation when shift_row or shift_col is negative. #7

Closed kota7 closed 7 years ago

kota7 commented 7 years ago

Hi, I find a weird behavior of Augmentation when shift_rows or shift_cols is negative.

library(OpenImageR)
a <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.157887008553284, 
0.506956425975552, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 
0.783377996402482, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0.848940934620609, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0.836092654454682, 
0.434308578980214, 0.434308578980214, 0.434308578980214, 0.434308578980214, 
0.434308578980214, 0.434308578980214, 0.434308578980214, 0.434308578980214, 
0.434308578980214, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.157887008553284, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(15L, 10L))

b <- Augmentation(a, shift_rows=-1, shift_cols=-1)
b
#      [,1] [,2]      [,3]     [,4]      [,5]      [,6] [,7]     [,8] [,9] [,10]
# [1,]    1    1 1.0000000 1.000000 1.0000000 1.0000000    1 1.000000    1     1
# [2,]    1    1 1.0000000 1.000000 1.0000000 1.0000000    1 1.000000    1     1
# [3,]    1    1 1.0000000 1.000000 0.8489409 0.8360927    1 1.000000    1     1
# [4,]    1    1 0.1578870 0.000000 0.0000000 0.4343086    1 1.000000    1     1
# [5,]    1    1 0.5069564 0.783378 0.0000000 0.4343086    1 1.000000    1     1
# [6,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
# [7,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
# [8,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
# [9,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
#[10,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
#[11,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
#[12,]    1    1 1.0000000 1.000000 0.0000000 0.4343086    1 1.000000    1     1
#[13,]    1    1 0.0000000 0.000000 0.0000000 0.0000000    0 0.157887    1     1
#[14,]    1    1 1.0000000 1.000000 1.0000000 1.0000000    1 1.000000    1     1
#[15,]    1    1 1.0000000 1.000000 1.0000000 1.0000000    1 1.000000    1     1
all(a==b)
# TRUE

So, no change in the image at all. It seems the function discounts shift_rows and shift_cols by 1 when it is negative. That is, it treats -1 as 0, -2 as -1, and so on.

mlampros commented 7 years ago

I'm sorry for the late reply,

Let me see that and If you are not in a hurry, I'll come with a solution either tomorrow or the day after tomorrow.

mlampros commented 7 years ago

thanks for spotting this out, it was a bug in the (image) translation function. Please test the new version of the package using devtools::install_github('mlampros/OpenImageR') (it will take some time till the new version of the package is on CRAN) and let me know.

kota7 commented 7 years ago

Hi, unfortunately, I get the same result by the above code. Can you quickly copy & paste it to see if you get the same result?

Assuming that you get the same result as I do, my humble guess is that this part is a problem:

https://github.com/mlampros/OpenImageR/blob/1ba1a99e571b35bccb6a101a649c6750f261f936/src/utils.cpp#L875-L878

It says if (shift_rows > 0.0 || shift_cols > 0.0) so shifting does not occur if both shift inputs are nonpositive. I am not sure why you have this if-clause, and any side effect occurs by removing it, though.

mlampros commented 7 years ago

I'm sorry, I totally forgot to change the inequality signs. I fixed it and I'll add some exception handling in the next few days. Please test it and let me know.

kota7 commented 7 years ago

Terrific. Thank you for your work!

Quick check code:

library(OpenImageR)
object <- matrix(0.5, nrow=3, ncol=4)
Augmentation(object, shift_rows=-1, padded_value=1)
Augmentation(object, shift_cols=-2, padded_value=1)
Augmentation(object, shift_row=-2, shift_cols=1, padded_value=1)
Augmentation(object, shift_row=-2, shift_cols=-3, padded_value=1)