Image pixels value exceeds 0-255 range, after arithmetic operations on image - How to solve it

Illustration
Patrick Star - 2024-01-31T22:02:45+00:00
Question: Image pixels value exceeds 0-255 range, after arithmetic operations on image - How to solve it

I = imread('img.jpg'); I = (double(I).^3)   The problem is, when I do operations like I.^3, pixels go far away from 255 value. I need them to be in range 0 - 255, and be able to multiply image to any value, without exceeding range.   Anyone got idea how could I do it?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

You could use mat2gray() to map the min to 0 and the max to 1.

 

I = mat2gray(I);

Multiply by 255 if you want in the range 0-255 and cast to uint8 if you want

I = uint8(255 * mat2gray(I));

I would recommend using a more descriptive variable name than I. You don't want it to be mistaken for a 1 or an l, and you don't want your code to look like alphabet soup.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!