DSPRelated.com
Code

2D Quantizer with Error Output

Ron April 1, 2011 Coded in Matlab

A script that quantizes an input 2D matrix (greyscale image) and includes an error matrix (picture).

clf;
truncate = 5;
x = imread('input.jpg');
subplot(2,2,1);
imshow(x);
subplot(2,2,2);
x_t = (x/2^truncate) * 2^truncate;
%if above line doesn't work your MATLAB is old! use next line:
% x_t = uint8(double(uint8((double(x)/2^truncate))) * 2^truncate);
imshow(x_t);
subplot(2,2,3);
imshow(uint8(abs(double(x_t) - double(x))));