2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$
    – Sycorax
    Nov 3, 2020 at 0:00
  • $\begingroup$ 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 $\endgroup$ Nov 3, 2020 at 1:11
  • $\begingroup$ @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. $\endgroup$
    – machinery
    Nov 3, 2020 at 9:51
  • $\begingroup$ @EricPerkerson What kind of non-linear transformation do you propose? I searched but I only found linear transformations in Python. $\endgroup$
    – machinery
    Nov 3, 2020 at 10:03
  • $\begingroup$ @machinery did you look at the examples in that that Wikipedia article? $\endgroup$ Nov 4, 2020 at 0:06

1 Answer 1

2
$\begingroup$

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

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.

On the other hand, if the data only take on a small number of values (e.g. only 1, 2 or 3), then there's not much that can be done to "smooth out" the values.

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$.

A third option is to exclude the most extreme values and then compute the min/max on the remaining data, similar to Winsorizing or trimming.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.