Quantize image using lloyds algorithm

20 visualizzazioni (ultimi 30 giorni)
Twain Glaser
Twain Glaser il 24 Ott 2015
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 Commento
Naushad Varish
Naushad Varish il 3 Gen 2017
Modificato: Walter Roberson il 3 Gen 2017
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

Accedi per commentare.

Risposte (1)

Naushad Varish
Naushad Varish il 3 Gen 2017
Modificato: Walter Roberson il 3 Gen 2017
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