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.

Answer:

function quantize( I, GreyNum)
if isstr(I)
    img = imread(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:

量化算法很簡單,就是通過將原圖像 256 個灰度值等分區域地映射到要 求下降的灰度分辨率上的某個值上。比如要將灰度分辨率降爲 4,那麼就將原圖 像灰度值爲 0—63 範圍內的像素點的灰度值映射爲 0,在 64—127 範圍內的像 素點的灰度值映射爲 85,在 128—191 內的映射爲 170,在 192—255 範圍內的映射爲 255。以此類推。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章