Quantize image using lloyds algorithm

22 ビュー (過去 30 日間)
Twain Glaser
Twain Glaser 2015 年 10 月 24 日
編集済み: Walter Roberson 2017 年 1 月 3 日
I would like to quantize an image using lloyds algorithm in order to calculate the MSE of an image quantized using the lloyds algorithm. I understand that partition are the levels boundaries and codebook returns the values to be assigned to pixels in each partition range. But i do not understand how to implement the two in order to quantize my image. I've posted my code below, thank you in advance for your time and help!
[M,N] = size(Pic);
training_set = double(Pic(:));
training_set = reshape(training_set,N*M,1);
MSELloyd = zeros(1,7);
for s = 7:-1:1
len = 2.^s;
[partition, codebook] = lloyds(training_set, len);
[PicLloyd ,index] = imquantize(Pic,partition,codebook);
PicLloyd = im2uint8(PicLloyd);
MSELloyd(s) = immse(PicLloyd,Pic);
end
figure; plot(MSELloyd); title('MSE of image quantized by Lloyds');
  1 件のコメント
Naushad Varish
Naushad Varish 2017 年 1 月 3 日
編集済み: Walter Roberson 2017 年 1 月 3 日
for s = 7:-1:1
len = 2.^s;
[partition, codebook] = lloyds(training_set, len);
[PicLloyd ,index] = imquantize(Pic,partition,codebook);
PicLloyd = im2uint8(PicLloyd);
MSELloyd(s) = immse(PicLloyd,Pic);
end
You should remove the for loop and these two lines i.e.
PicLloyd = im2uint8(PicLloyd);
MSELloyd(s) = immse(PicLloyd,Pic); from your code

サインインしてコメントする。

回答 (1 件)

Naushad Varish
Naushad Varish 2017 年 1 月 3 日
編集済み: Walter Roberson 2017 年 1 月 3 日
Your correct code is
s = 7:-1:0
len = 2.^s;
[partition, codebook] = lloyds(training_set, len);
[PicLloyd ,index] = imquantize(Pic,partition,codebook);
Figure, imshow(PicLloyd),
max_t=double(max(gray(:)));
% PSNR calculation
squaredErrorImage = (double(gray) - double(PicLloyd)) .^ 2;
mse = sum(sum(squaredErrorImage)) / (M * N);
PSNR = 20 * log10(max_t/ mse)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by