Image resizing while preserving average

Image resizing is actually surprisingly complicated, both from a performance perspective and an accuracy perspective. I’ve taken a couple of computer vision classes, but I suspect I would get it wrong a few times if I had to implement it myself. You can see the implementation of imresize here: https://github.com/JuliaImages/ImageTransformations.jl/blob/master/src/resizing.jl including some helpful documentation about what it’s trying to do.

In answer to your question, I suspect the easiest way to handle this would be to record the previous average, perform the resize, and then add an offset to the resized image to fix its mean. E.g.

julia> a = randn(8, 8);

julia> b = imresize(a, (2, 2));

julia> b .+= mean(a) - mean(b)
2×2 Array{Float64,2}:
 0.189495  -0.207325
 0.744529   0.351593

julia> mean(b) ≈ mean(a)
true

Does that work for your case?

By the way, you can format your code nicely using the instructions at PSA: how to quote code with backticks

1 Like