DSPRelated.com
Code

Image Quantizer

Ron April 1, 2011 Coded in Matlab

A script that quantizes an input image and displays both the original and quantized version. t(color) represents the number of bits to truncate from the respective color stream.

**Press any key to go from original to quantized on figure output.

%number of bits to truncate
tred = 4;
tgreen = 2;
tblue = 3;

clf;
x = imread('input.jpg');
red = x(:,:,1);
green = x(:,:,2);
blue = x(:,:,3);

red_t = (red/2^tred) * 2^tred;
green_t = (green/2^tgreen) * 2^tgreen;
blue_t = (blue/2^tblue) * 2^tblue;

x_t(:,:,1) = red_t;
x_t(:,:,2) = green_t;
x_t(:,:,3) = blue_t;

imshow(x);
pause;
imshow(x_t)

size = (24 - tred - tgreen - tblue) / 24