Image pixels value exceeds 0-255 range, after arithmetic operations on image - How to solve it
6 views (last 30 days)
Show older comments
Patrick Star
on 22 Mar 2016
Answered: Image Analyst
on 22 Mar 2016
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?
0 Comments
Accepted Answer
Image Analyst
on 22 Mar 2016
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.
0 Comments
More Answers (1)
Roger Stafford
on 22 Mar 2016
I would suggest normalizing the matrix:
I = double(I).^3;
I = floor(I/max(I(:)));
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!