matlab的grayscale,Matlab Grayscale Normalization

All of the people above in the comments have raised very good points, but if you want a tl;dr answer, here are the most important points:

If your minimum and maximum values are 0 and 255 respectively for all colour channels, this means that the minimum and maximum colour values are black and white respectively. This is the same situation if your image was a single channel image / grayscale. As such, if you try and normalize your output image, it will look the same as you would be multiplying and dividing by the same scale. However, just as a minor note, your above code will work if your image is grayscale. I would also get rid of the superfluous nested min/max calls.

You need to make sure that your image is cast to double before you do this scaling as you will most likely generate floating point numbers. Should your scale be < 1, this will inadvertently be truncated to 0. In general, you will lose precision when you're trying to normalize the intensities as the type of the image is most likely uint8. You also need to remember to cast back to uint8 when you're done, as that is what the original type of the image was before you cast. You can do this casting, or you can use im2double as this essentially does what you want under the hood, but normalizes the image's intensities to the range of [0,1].

As such, if you really really really really... really... want to use your code above, you'd have to do something like this:

lim3 = double(lim3); %// Cast to double

minvalue = min(lim3(:)); %// Note the change here

maxvalue = max(lim3(:)); %// Got rid of superfluous nested min/max calls

normimg = uint8((lim3-minvalue)*255/(maxvalue-minvalue)); %// Cast back to uint8

This code will work if the image you are reading in is grayscale.

Bonus - For Colour Images

However, if you want to apply the above for colour images, I don't recommend you use the above approach. The reason being is you will only see a difference if the minimum and maximum values for each colour plane are the same - 0 and 255 respectively. What I would recommend you do is normalize each colour plane separately so that you'll push each colour plane to the range of [0,1], not being bound to the minimum and maximum of just one colour plane.

As such, I would recommend you do something like this:

lim3 = double(lim3); %// Cast to double

normimg = uint8(zeros(size(lim3))); %// Allocate output image

for idx = 1 : 3

chan = lim3(:,:,idx);

minvalue = min(chan(:));

maxvalue = max(chan(:));

normimg(:,:,idx) = uint8((chan-minvalue)*255/(maxvalue-minvalue)); %// Cast back to uint8

end

The above code accesses each colour plane individually, normalizes the plane, then puts the result in the output image normimg. I would recommend you use the above approach instead if you want to see any contrast differences for colour images.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值