I have a numpy array of type np.int64, to which I am trying to apply a formula.
Let's say the array is a 2D array called total_img
which has dimensions 400 X 300 pixels. For each pixel I want to compute the following formula. px = 255*(min - px)/(min - max)
. I want these rescaled pixels to be stored always in total_img
. How can I efficiently implement this using numpy arrays?
Note, min
and max
are simply the 1th percentile and 99th percentile values and are actually stored as floats. Should I convert them to ints for better accuracy (remember total_img is of type np.int64 - No Overflow will ever occur). Also min will most likely be negative.
I was using:
for row in total_img:
for px in row:
px = 255*(min-px)/(min - max)
You literally just plug in
total_img
instead ofpx
, and it does the operation elementwise. If you want to store the result into the originaltotal_img
instead of replacing it, you can slice-assign:but note that this doesn't actually save any time or memory.