Borrar filtros
Borrar filtros

Quantize image using lloyds algorithm

4 visualizaciones (últimos 30 días)
Twain Glaser
Twain Glaser el 24 de Oct. de 2015
Editada: Walter Roberson el 3 de En. de 2017
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 comentario
Naushad Varish
Naushad Varish el 3 de En. de 2017
Editada: Walter Roberson el 3 de En. de 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

Iniciar sesión para comentar.

Respuestas (1)

Naushad Varish
Naushad Varish el 3 de En. de 2017
Editada: Walter Roberson el 3 de En. de 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