# Scaling values to range -1 and 1 with large outliers

I have a set of images of size 64 x 64 with 3 color channels each. Each image contains a heat map (describing the browser usage).

I'm scaling each pixel of this dataset to a range of -1 to 1 using the following formula:

new_value = ((old_value - min) * (new_max - new_min) / (max - min)) + new_min


max and min are the maximum and minimum values and new_max = 1 and new_min = -1.

This approach has the disadvantage that if the variance is high, i.e. the max and min are very large/small compared to the other values, most of the values are close to zero (which is a problem for visualizing the image).

Is there another approach of scaling the images to the range -1 to 1 not taking into (or discarding) outliers so that only the central values are kept? I thought about shifting the distribution so that the mean is around zero and then clipping all values smaller -1 and greater 1 to -1 and 1, respectively.

• What problem does the scaling solve? Is it just related to visualization, or is there some other goal? If the problem you're trying to solve is visualization, why not rescale the data to the original form before visualization?
– Sycorax
Nov 3, 2020 at 0:00
• This sounds like the image equivalent of dynamic range compression for audio. The solution is to use a non-linear transformation, instead of the affine linear transformation you are currently using. See the Wikipedia page for details: en.wikipedia.org/wiki/Dynamic_range_compression Nov 3, 2020 at 1:11
• @Sycorax I scale because I feed the images into a CNN and it assumes values between -1 and 1. I think for the CNN it should not matter how I scale but I also would like to visualize it. For visualization, I also have to scale the values. What do you mean by rescaling to the original form? The problem is that the images are not like e.g. cat images but heat maps. So the values might range in one channel from 0 to 100 and in another channel from -4 to 10 and so on. Nov 3, 2020 at 9:51
• @EricPerkerson What kind of non-linear transformation do you propose? I searched but I only found linear transformations in Python. Nov 3, 2020 at 10:03
• @machinery did you look at the examples in that that Wikipedia article? Nov 4, 2020 at 0:06

If you have a large number of distinct values, you could do something like a quantile scaling (e.g. percentile). This would make the transformed data uniform on $$[0,1]$$. Multiply by 2 and subtract 1 to get values in $$[-1,1]$$. This solves the "clusters around 0" problem because the data are spread out to be uniform.
A second option is to compose a nonlinear and affine transformation like $$\tanh(ax+b)$$. The $$\tanh$$ function will always give values in $$[-1,1]$$ and you can control how that happens by choosing suitable $$a,b$$.