How do I quantize the colors for a displayed grayscale image with user-specified gray values?

I am working with grayscale images. I would like to quantize the colors in my image to specific values, say around 15 or 20 values. I would like to know if this is possible with MATLAB.
For example, if my image contains gray values from 0 to 255, I would like to quantize them as follows:
0 - 30 => 0 (black)
31 - 150 => 60 (dark gray)
151 - 200 => 120 (light gray)
201 - 255 => 255 (white)
I would like the method to be general and not restricted to four levels of quantization and I would like the freedom to specify which values I want to quantize at.

 Accepted Answer

It is possible to quantize the colors for a grayscale as well as a color image using MATLAB. This involves redefining the colormap of the image. An example of this procedure is attached to this solution as an M-function called 'createColormap.m'.
For the given quantization problem, you can call this function as follows:
cmap = createColormap([30, 150, 200, 255], [0, 60, 120, 255]);
Once the quantized colormap is created, this can be applied to the current figure using the following statement:
colormap(cmap);

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!