您的位置:首页 > 其它

Reducing the Number of Gray Levels in an Image

2011-10-02 00:47 435 查看

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);


Answer:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐