I need to uniformly re-quantize the dynamic range of an image based on the following pixel value conversions:
Pixel Value: Quantized Value
0 - 64 : 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:Another option is to create a 256-element look-up table and use the values in your image as indices into this table:
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 (likedouble) 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 classuint8.