I have an rgb image matrix (height*width*3) represented in doubles. After some manipulations on the matrix, some values went biger then 1 or smaller then 0. I need to normalize those valuse back to 1 and 0. Thanks.
Asked
Active
Viewed 1.2k times
2 Answers
6
Well, just use the indexing by condition. Let's say your matrix is called M. If you just want to set values bigger than 1 to 1 and smaller than 0 to zero, use:
M(M > 1) = 1;
M(M < 0) = 0;
However, if you want to proportionally normalize all the values to the interval [0; 1], then you have to do something similar to:
mmin = min(M(:));
mmax = max(M(:));
M = (M-mmin) ./ (mmax-mmin); % first subtract mmin to have [0; (mmax-mmin)], then normalize by highest value
You have to take account of the case when your matrix M is already in the interval [0; 1] and the normalization is not needed.

Marek Kurdej
- 1,459
- 1
- 17
- 36
-
Thanks, indeed it does what i asked. But the result is not good enough so i guess that i need realy to normalize the matrix. the bigst value should be 1, smalest 0 , and everything else betwin proportionaly. – Sanich Nov 13 '11 at 12:26
-
Sorry, I didn't get it right. I've edited my answer. Hope, it's what you wanted. – Marek Kurdej Nov 13 '11 at 12:35
-
@Curdeius: If you want to crop the values in the range [0,1] (your first solution), you can also write it vectorized as: `M = min(max(M,0),1)` – Amro Nov 13 '11 at 13:28
-
@Curdeius: Maybe the last action should be `(M = (M-mmin) ./ (mmax-mmin);`)? (instead of .*) – Sanich Nov 13 '11 at 21:37
0
if you just want to see the images you can use
imagesc(M);
it takes care of the range itself.
If you want to change the values manually and have full control over it,
M = M ./ max(M(:));
would do the trick if you only have positive values. To get a full contrast image you might want to:
m = m - min(m(:));
m = m ./ max(m(:));

Ali
- 18,665
- 21
- 103
- 138