how to denormalize image in python?
Asked Answered
A

2

5

I am working on a project where i have to first normalize the image to [0,1] and then perform dwt and idwt on the image after the processing. so first i convert the image to an array then i normalize it with this code

def normalization (array):    
    maxs = max([max(l) for l in array])
    mins = min([min(l) for l in array])
    range = max - mins
    A = []
    for x in array:
        m = [(float(xi) - mins)/range for xi in x]
        A.append(m)    
    return A

the code works well and now i have no idea how can i denormalize it back to the actual range. can anybody help?

Assiut answered 5/5, 2015 at 8:17 Comment(2)
Do you keep track of the original range anywhere? If not, you'll need to do so.Merilyn
Did you end up finding a solution?Sightless
S
8

I use the following to map to and from any interval [a, b] --> [c, d] and back:

import numpy as np

def interval_mapping(image, from_min, from_max, to_min, to_max):
    # map values from [from_min, from_max] to [to_min, to_max]
    # image: input array
    from_range = from_max - from_min
    to_range = to_max - to_min
    scaled = np.array((image - from_min) / float(from_range), dtype=float)
    return to_min + (scaled * to_range)

An example:

image = np.random.randint(0, 255, (3, 3))
image

returns:

array([[186, 158, 187],
       [172, 176, 232],
       [124, 167, 155]])

Now map this from [0, 255] to [0, 1]

norm_image = interval_mapping(image, 0, 255, 0.0, 1.0)
norm_image

returns:

array([[ 0.72941176,  0.61960784,  0.73333333],
       [ 0.6745098 ,  0.69019608,  0.90980392],
       [ 0.48627451,  0.65490196,  0.60784314]])

now from [0, 1] back to [0, 255]:

orig_image =interval_mapping(norm_image, 0.0, 1.0, 0, 255).astype('uint8')
orig_image

returns:

array([[186, 158, 187],
       [172, 176, 232],
       [124, 167, 155]], dtype=uint8)

You could also use it one a single column of image and map it to [-1.0, 1.0]:

col = image[:, 1]
print col
interval_mapping(col, 0, 255, -1.0, 1.0)

returns:

[158 176 167]
array([ 0.23921569,  0.38039216,  0.30980392])

or a scalar:

interval_mapping(1.0, 0, 255, -1.0, 1.0)

returns:

-0.99215686274509807
Sightless answered 5/5, 2015 at 18:40 Comment(1)
Thank you very much for this function. Converting integers to decimals, then undoing that conversion gives original integers as you shown. But I found that converting decimals to integers, then undoing that conversion does not give original decimals using this function.Freakish
O
1

You just need to do the inverse of the normalisation. So, multiply by the original range and add the minimum. Just typing untested code:

def denormalization (array, mins, range):    
    A = []
    for x in array:
        m = [(float(xi) * range) + mins for xi in x]
        A.append(m)    
    return A

Obviously you'd need to keep your original range and minimum as globals in order to use them in this function.

Open answered 5/5, 2015 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.