## Image Quantization

Question:

Write a function that takes a gray image and a target number of gray levels as input, andgenerates the quantized image as output. The function prototype is “quantize(input img,level) → output img”, where “level” is an integer in [1, 256] defining the number of gray levelsof output. You can modify the prototype if necessary.For the report, please load your input image and use your “quantize” function to:

1. Reduce gray level resolution to 128, 32, 8, 4 and 2 levels, then paste your results respectively.Note that, in practice computers always represent “white” via the pixel value of 255, soyou should also follow this rule. For example, when the gray level resolution is reduced to4 levels, the resulting image should contain pixels of 0, 85, 170, 255, instead of 0, 1, 2, 3.

2. Detailedly discuss how you implement the quantization operation, i.e., the “quantize”function, in less than 2 pages. Again, please focus on the algorithm part. Analyzingand discussing interesting experiment results are also welcomed, but please don’t widelycopy/paste your codes in the report.

```function quantize( I, GreyNum)
if isstr(I)
end
[row,col] = size(img);
new = zeros(row,col);
r = 256/GreyNum; %确定区间长度
r2 = 255/(GreyNum-1); %确定区间首尾
for j = 1:col
for i = 1:row
for n = 1:GreyNum
if (img(i,j) < n*r)
new(i,j) = 0 + (n-1) * r2;
break;
end
end
end
end
new = uint8(new);
figure
imshow(new);
axis on
title(['量化后的图像（大小： ',num2str(col),'*',num2str(row),'*',...
'， 灰度分辨率： ',num2str(GreyNum),'）']);
end```

Algorithm description: