I need to uniformly re-quantize the dynamic range of an image based on the following pixel value conversions:
Pixel Value: Quantized Value
0 - 6开发者_如何学Python4 : 31
65 - 128 : 95
129 - 192 : 159
193 - 255 : 223
I want to replace all the pixel values in the above ranges with the quantized values. How can I code this in MATLAB?
One way is to use logical indexing. Given a image matrix img
(which could be 2-D grayscale or 3-D RGB), this will replace all your values:
img(img >= 0 & img <= 64) = 31;
img(img >= 65 & img <= 128) = 95;
img(img >= 129 & img <= 192) = 159;
img(img >= 193 & img <= 255) = 223;
Another option is to create a 256-element look-up table and use the values in your image as indices into this table:
lookupTable = [31.*ones(1,65) 95.*ones(1,64) 159.*ones(1,64) 223.*ones(1,63)];
img = uint8(lookupTable(double(img)+1));
Note that with this solution you will have to be mindful of the class of your image matrix. Many images are of class uint8
, spanning values 0 to 255. To use these values as an index you have to convert them to a class that can store larger integers (like double
) to avoid saturation at the maximum value of 255, then add one since you need an index from 1 to 256. You would then want to convert the resulting image matrix back to class uint8
.
精彩评论