mlampros / OpenImageR

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

Default values for shifted images by Augmentation? #6

Closed kota7 closed 7 years ago

kota7 commented 7 years ago

Hi, I have a matrix representing a gray-scale image like below. In this matrix, 0 represents black and 1 represents white.

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

This looks like 1

When I used Augmentation function with shift_rows or shift_cols options, I get the following.

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

This looks like below. 1b

So, the padded value is always 0, where I want to use 1 to fill and I could not find a way to choose it. It would be great if I can freely chose the default value to be filled in.

Thanks.

mlampros commented 7 years ago

I'm sorry for the late reply,

If you are not in a hurry, I'll come with a fix either tomorrow or the day after tomorrow.

mlampros commented 7 years ago

I added the padded_value parameter to both the Augmentation and the translation functions. Besides 0 it can take posiitve, negative or float values. Please test the new version 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, thanks for your quick response! It now works good for my case (matrix image).

I have a suggestion for 3d-array case.

library(OpenImageR)
object <- array(1, dim = c(10, 10, 3))
Augmentation(object, shift_rows=2, padded_value=0.5)

The code above pads a certain value into all three dimension. Assuming each dimension corresponds to R,G,B, this means we can pad black, white, or any gray colors.

Some may wish to pad by red, i.e. (1,0,0), but the code below does not work.

Augmentation(object, shift_rows=2, padded_value=c(1,0,0))
#Error in augment_transf_array(image, flip_mode, crop_height, crop_width,  : 
#  expecting a single value

Allowing padded values of vector of size equal to the channel dimension size can be a good enhancement.

mlampros commented 7 years ago

I added the functionality that you suggested in both translation and Augmentation functions. The padded_value parameter can be either a numeric value (of length 1) or a numeric vector (of length equal to the dimensions of the array). I'll add exception handling in the next few days. Please test it and let me know.

kota7 commented 7 years ago

Beautiful. It works great.

Quick check I have run:

library(OpenImageR)
object <- matrix(1, nrow=2, ncol=5)
Augmentation(object, shift_cols=1, padded_value=-5)

object <- array(1, dim = c(4, 2, 3))
Augmentation(object, shift_rows=1, padded_value=0.5)
Augmentation(object, shift_rows=1, padded_value=c(3,4,5))