DSPRelated.com

Image Quantizer Demo

Ron April 1, 2011 Coded in Matlab
clf;
for (truncate = 0:8)
x = imread('aqua.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))));
pause(0.5);
end

Signal-to-Distortion SDR Ratio

Ron April 1, 2011 Coded in Matlab
[f, fs, nbits] = wavread('input.wav');
[f2, fs2, nbits2] = wavread('input-truncated.wav');

top = 1/size(f,1)*(sum(f.^2))
bottom = 1/size(f2,1)*(sum(f2.^2))

Y = 10*log10(top/bottom)

RGB (JPEG) Image Separator

Ron April 1, 2011 Coded in Matlab
clf;
x = imread('input.jpg');
red = x(:,:,1);
green = x(:,:,2);
blue = x(:,:,3);

subplot(2,2,1);
imshow(x);
title('Original');
subplot(2,2,2);
imshow(red);
title('Red');
subplot(2,2,3);
imshow(green);
title('Green');
subplot(2,2,4);
imshow(blue);
title('Blue');

Image Quantizer

Ron April 1, 2011 Coded in Matlab
%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

2D Quantizer with Error Output

Ron April 1, 2011 Coded in Matlab
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))));

WAV Quantizer with Error Output

Ron April 1, 2011 Coded in Matlab
truncate = 6;
[f, fs, nbits] = wavread('in.wav');
f_int = int16(f*2^(nbits-1-truncate));
f_back_to_float = (double(f_int))/2^(nbits-1-truncate);
f_diff = f - f_back_to_float;
wavwrite(f_back_to_float, fs, nbits, 'out.wav');
wavwrite(f_diff, fs, nbits, 'error.wav');

Quantization Demo

Ron April 1, 2011 Coded in Matlab
a = 10;
x = 0:2*pi/1000:2*pi;
y = a*sin(x);
y_int = int8(y);

subplot(3,1,1);
plot(x,y);
grid;
axis([0 2*pi -a*1.2 a*1.2]);
title('Continuous');

subplot(3,1,2);
plot(x,y_int);
grid;
axis([0 2*pi -a*1.2 a*1.2]);
title('Quantized');

subplot(3,1,3);
plot(x,abs(double(y_int)-y));
grid;
axis([0 2*pi 0 1.2/2]);
title('Error');

WAV Subsample

Ron April 1, 2011 Coded in Matlab
factor = 8;
[X, fs, nbits] = wavread('in.wav');
Y = downsample(X,factor);
wavwrite (Y, fs/factor, nbits, 'out.wav');

2D Image Subsample

Ron April 1, 2011 Coded in Matlab
A = imread('in.jpg');

M =   [ 1 4  6  4  1;
        4 16 24 16 4;
        6 24 36 24 6;
        4 16 24 16 4;
        1 4  6  4  1 ] / 256;

B = uint8(conv2(double(A),M));
imwrite(B,'out.jpg','JPG');

2D Image FIR Subsample

Ron April 1, 2011 Coded in Matlab
A = imread('in.jpg');

M =   [ 1 4  6  4  1;
        4 16 24 16 4;
        6 24 36 24 6;
        4 16 24 16 4;
        1 4  6  4  1 ] / 256;

Y = filter2(M,A);
B = uint8(conv2(double(Y),M));

imwrite(B,'out.jpg','JPG');