Sanich
Sanich

Reputation: 1845

normalizing matrix in matlab

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.

Upvotes: 3

Views: 12362

Answers (2)

Marek Kurdej
Marek Kurdej

Reputation: 1509

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.

Upvotes: 6

Ali
Ali

Reputation: 19722

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(:));

Upvotes: 0

Related Questions