Problem:
(a) Write a computer program capable of reducing the number of gray levels in a image from 256 to 2, in integer powers of 2. The desired number of gray levels needs to be a variable input to your program.
(b) Download Fig. 2.21(a) and duplicate the results shown in Fig. 2.21 of the book.
Solution:
reduceGrayLevel.m
function img = reduceGrayLevel(image, level)
imageSize = size(image);
num = 256 / level;
img = uint8(zeros(imageSize(1), imageSize(2)));
for r = 1:1:imageSize(1)
for c = 1:1:imageSize(2)
img(r, c) = fix(double(image(r, c)) / num) * 255 / (level-1);
end
end
end
proj0201.m
clear;
clc;
image = imread('Fig0221(a)(ctskull-256).tif');
img1 = reduceGrayLevel(image, 128);
img2 = reduceGrayLevel(image, 64);
img3 = reduceGrayLevel(image, 32);
img4 = reduceGrayLevel(image, 16);
img5 = reduceGrayLevel(image, 8);
img6 = reduceGrayLevel(image, 4);
img7 = reduceGrayLevel(image, 2);
subplot(2, 4, 1), imshow(image), subplot(2, 4, 2), imshow(img1), ...
subplot(2, 4, 3), imshow(img2), subplot(2, 4, 4), imshow(img3), ...
subplot(2, 4, 5), imshow(img4), subplot(2, 4, 6), imshow(img5), ...
subplot(2, 4, 7), imshow(img6), subplot(2, 4, 8), imshow(img7);