1

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)
1
  • 1
    you should not shadow builtins such as min/max ... just an aside totally unrelated to your problem Commented Jun 24, 2015 at 23:00

2 Answers 2

3
total_img = 255*(min - total_img)/(min - max)

You literally just plug in total_img instead of px, and it does the operation elementwise. If you want to store the result into the original total_img instead of replacing it, you can slice-assign:

total_img[:] = 255*(min - total_img)/(min - max)

but note that this doesn't actually save any time or memory.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe you could directly do this:

total_img = 255*(min-total_img)/(min - max)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.