Thursday, March 17, 2016

MATLAB code to Quantize the gray levels to 128, 64, 32,16, 8, 4 and 2 levels.

Exercise 1) Write a MATLAB program to quantize the gray levels to 128, 64, 32,16, 8, 4 and 2 levels. Read CAT.jpg image and display the image

Exercise 2) Obtain the above result using  grayslice() function.


 CODE :
clear
close all
Input = imread('CAT.jpg');
figure
m = 1;
subplot(4,2,1);
image(Input);
colormap(gray(256));
xlabel(num2str(2^8));
for i = 7:-1:1,
    m = m + 1;
    del = 256/(2^i);
    subplot(4,2,m);
    J = floor(double(Input)/del)*del+(del/2);
    image(J);
    colormap(gray(256));
    xlabel(num2str(2^i));
end;
gtext(' using code');

% using built in function
figure;
subplot(4,2,1);
image(Input);
colormap(gray(256));
xlabel(num2str(2^8));
for i=7:-1:1
    out=grayslice(Input,(2^7));
    subplot(4,2,8-i+1);
    image(uint8(out));
    xlabel(num2str(2^i));
end;
gtext('using built-in function');

Fig 1. Quantization using built-in function grayslice()

Fig 2. Quantization using Code


CONCLUSION:

Quantization was carried out on an input image, representing it with 256, 128, 64, 32, 16 ,8 ,4 and 2 gray levels. The results obtained are as shown in Fig.1 and Fig.2. It can be seen that there is no significant change in the quality of the image even when only 16 distinct levels are used for its representation, but after 8, the quality of the images starts to deteriorate. Thus only 16 bits can be used to effectively represent an image without a significant loss in its quality.

No comments:

Post a Comment