How to resize a very large array or image without changing its number type

Hi,

I would like to resize an array (or perhaps an image) and get a result of the same type. For instance, in my code below:

N = 10
img = rand(Int8,N,N,3)
img = imresize(img,ratio=(0.7, 0.7, 1))

the outupt img changes from Array{Int8,3} to Array{Float64,3}. If my N was small, my naïve attempt

img = Int8.(round.(imresize(img,ratio=(0.7, 0.7, 1))))

would solve the problem, but I am struggling with memory because my N is ~40000.

Please, how this could be done in a better way?

Thanks in advance

imresize does interpolation (https://github.com/JuliaImages/ImageTransformations.jl/blob/a83895e0b1386aee833c8337d2b8029842c61b1b/src/resizing.jl#L316)
so, it naturally outputs floating point numbers.

Maybe the best option is to use normed fixed-point numbers (instead of floating-point numbers).
For example the type N0f8 is a 8-bit number which represents numbers between 0 and 1. [1]

The equivalent code is

using ImageTransformations, Colors, ImageCore
N = 10
img = rand( RGB{N0f8}, N, N)
img = imresize(img, ratio=(0.7,0.7))

or, if you want to keep the channels as a third dimension:

img = rand( N0f8, N, N, 3)
img = imresize(img, ratio=(0.7,0.7,1.0))

However, due to the memory arrangement of multidimensional arrays, it is better to have the channel in the first dimension and it is best to use RGB{N0f8}. You can use channelview(img) [2] if you want to access only particular channels.

Links: [1] GitHub - JuliaMath/FixedPointNumbers.jl: fixed point types for julia
[2] Views · ImageCore

2 Likes