Reducing the Number of Gray Levels in an Image

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:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值