Quantize image using lloyds algorithm
9 次查看(过去 30 天)
显示 更早的评论
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
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
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)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!