What is MinMaxScaler in Python

Why does MinMaxScaler add lines to the image? - numpy, image processing, machine learning, scikit-learning

I want to normalize the pixel values ​​of an image for each channel (R, G, B) to the range [0, 1].

Minimal example

What happens

Take https://commons.wikimedia.org/wiki/File:Crocodylus-johnsoni-3.jpg, I get the following "normalized" result:

As you can see there are lines going from top to bottom on the right. What happened there? It seems to me that the normalization went wrong. If so, how do I fix it?


4 for the answer № 1

In scikit-learn, a two-dimensional array with shape (m, n) is usually interpreted as a collection of m Have samples, with every sample n Properties.

transforms everyone featureso that each column of your array is transformed independently of the others. This leads to the vertical "stripes" in the picture.

It looks like you intend to scale each color channel independently. To do that, change the input so that each channel becomes a column. That is, if the original image is of the shape (m, n, 3) it will be reshaped to (m * n, 3) before being passed to the method and then restore the shape of the result to the create transformed array.

For example,

With this one, looks like this:

The image looks exactly like the original, because that results in the array.The minimum and maximum are 0 and 255 in each channel:

So everything In this case, transform all input values ​​to the floating point area [0.0, 1.0] uniformly. If the minimum or maximum in one of the channels were different, the transformed image would look different.

By the way, it's not difficult to do transform with pure numpy. (I'm using Python 3 so in the following case the result is automatically converted to floating point numbers. If you are using Python 2 you will need to convert or use one of the arguments to floating point numbers.)

(A possible problem with this method is that if one of the channels is constant it will generate an error because then one of the values ​​in becomes 0.)