Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale a numpy array with from -0.1 - 0.2 to 0-255

I have an numpy array in python that represent an image its size is 28x28x3 while the max value of it is 0.2 and the min is -0.1. I want to scale that image between 0-255. How can I do so?

like image 992
Jose Ramon Avatar asked Apr 19 '18 13:04

Jose Ramon


People also ask

How do I resize an array in NumPy?

With the help of Numpy numpy. resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing.

What does size () do in NumPy?

In Python, numpy. size() function count the number of elements along a given axis. Parameters: arr: [array_like] Input data.

Is NumPy array fixed size?

NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original. The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory.

What is NumPy array size change in Python?

Resize() resize() function is used to create a new array of different sizes and dimensions. resize() can create an array of larger size than the original array.


1 Answers

new_arr = ((arr + 0.1) * (1/0.3) * 255).astype('uint8')

This first scales the vector to the [0, 1] range, multiplies it by 255 and then converts it to uint8, which is a common format for images (opencv uses it, for example)

In general you can use:

new_arr = ((arr - arr.min()) * (1/(arr.max() - arr.min()) * 255)).astype('uint8')
like image 90
Fred Avatar answered Oct 19 '22 03:10

Fred