Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to reverse scale a scaled numpy array?

I have a numpy array, example:

signal =array([[    0,     0,     0,  3485,  3480,  3474],
               [    0,     0,     0,  5644,  5642,  5655],
               [    0,     0,     0,  6622,  6541,  6623],
               [ 4555,  4656, 11232, 11265, 11564, 11553],
               [ 3450,  3455,  3536,  3510,  3564,  3375]])

I used the scaling ((signal) / np.max(signal)*255).astype(np.uint8)
It’s became

f = array([[  0,   0,   0,  76,  76,  76],
           [  0,   0,   0, 124, 124, 124],
           [  0,   0,   0, 146, 144, 146],
           [100, 102, 247, 248, 255, 254],
           [ 76,  76,  77,  77,  78,  74]], dtype=uint8)

so, now I want to generate the signal array from the f array.
I tried the reverse scale like,
(f * np.max(signal)/255).astype(np.uint32)
but It’s giving me

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array([[  0,   0,   0, 105, 105, 105],
       [  0,   0,   0, 226, 226, 226],
       [  0,   0,   0, 195, 105, 195],
       [165, 256, 150, 195, 255, 210],
       [105, 105, 150, 150, 196,  14]])

which is not appropriate.
so what should I do to reverse the scaling of the array to get the previous original input data?

>Solution :

You are multiplying an array with dtype uint8 with maximum value of signal. This data type can’t hold an integer that large so overflow happens and the numbers are wrapped to the range of 0->255. you have to convert it to uint32 first and then scale:

signal = f.astype(np.uint32) * np.max(signal)/255

After this, the output will still be a little different than original and that is because of converting floating point results to uint8 which will cause your numbers to lose their fraction.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading