Quality Analysis
Quality Analysis
Quant levels = 2
PSNR = 3.8706
SNR = -7.3856
MSE = 137.0327
Distortion (1/psnr) = 0.2584
Quant levels = 4
PSNR = 4.3529
SNR = -6.9033
MSE = 128.9053
Distortion (1/psnr) = 0.2297
Quant levels = 6
PSNR = 5.6940
SNR = -5.5622
MSE = 114.7516
Distortion (1/psnr) = 0.1756
Quant levels = 8
PSNR = 13.3115
SNR = 2.0553
MSE = 7.3324
Distortion (1/psnr) = 0.0751
Quant levels = 10
PSNR =13.3294
SNR = 2.0732
MSE = 6.6789
Distortion (1/psnr) = 0.0750
Quant levels = 12
PSNR =13.3372
SNR = 2.0809
MSE = 6.1603
Distortion (1/psnr) = 0.0750
Quant levels = 14
PSNR = 13.3371
SNR = 2.0809
MSE = 5.7363
Distortion (1/psnr) = 0.0750
Quant levels = 20
PSNR = 13.3301
SNR = 2.0739
MSE = 4.7854
Distortion (1/psnr) = 0.0750
Quant levels = 32
PSNR = 13.3204
SNR = 2.0642
MSE = 4.3597
Distortion (1/psnr) = 0.0751
Quant levels = 128
PSNR = 13.2824
SNR = 2.0262
MSE = 2.7607
Distortion (1/psnr) = 0.0753
image=imread("E:\sem 7\image vvideo coding\project\SLguitarGUY.png");
img=rgb2gray(image);
macrobock_size = 8;
[IMG,y,x]=macroblocks(img,macrobock_size);
qp=511;
s=dct2(img);
s1=dct2(img(1:macrobock_size,1:macrobock_size));
%thresh = multithresh(s1 ,qp);
%valuesMax = [thresh max(s1(:))];
thresh = (1:qp);
valuesMax = [thresh max(s1(:))];
DCT_tf_img = dct_full_image(IMG,macrobock_size,thresh,valuesMax);
%-----------zig zag-------------------------------------------------------
[rvec,cvec] = zigzag(cell2mat(DCT_tf_img(1,1)));
img_One_D_vector = oneMblockVec(DCT_tf_img,macrobock_size,rvec,cvec);
%-------run length code----------------------------------------------------
run_L_coded = run_length_coding(cell2mat(img_One_D_vector));
%--------huffman------------------------------------------------------------
[g,~,intensity_val] = grp2idx(run_L_coded);
Frequency = accumarray(g,1);
[intensity_val Frequency];
proability = Frequency./sum(Frequency);
dict = huffmandict(intensity_val,proability);
code = huffmanenco(run_L_coded,dict);
recov_IMG = decoderesidual(code,dict,rvec,cvec,y,x,macroblock_size);
imshow(uint8(cell2mat(recov_IMG)))
[peaksnr, snr] = psnr(uint8(cell2mat(recov_IMG)),img)
ms = mse(uint8(cell2mat(recov_IMG)), img)
Dis = [1/peaksnr, 1/snr]
% distort = [0.2584 0.2297 0.1756 0.0751 0.0750 0.0750 0.0750 0.0750 0.0751 0.0753];
% quantizationLVL=[2 4 6 8 10 12 14 20 32 128]
% plot(quantizationLVL,distort);
% xlabel("quantization levels"); ylabel("distortion");
% title("distortion vs bitrate");
%Had to change this function to obtain above results
function transformed=dct_full_image(img,macroblock_size,thresh,valuesMax)
%k1 = macroblock row index
%k2 = macroblock column index
%macroblock_size = number of side pixels in a macroblock
for i = 1:size(img,1)
for j = 1:size(img,2)
DCTtransformed=dct2(cell2mat(img(i,j)));
[quant_image,index] = imquantize(DCTtransformed ,thresh,valuesMax);
transformed(i,j)={quant_image};
end
end
end
•When quantizing the residual the data type changes and when reconstructed total different output came. By typecasting the residual in to uint8 I was able to obtain the expected image
•Selection of quantization was not the optimum hence a pixelated output is obtained.
•From the graph we can see that the best point to minimize the distortion and minimize the bitrate is when quantization level is 8.
•Imquant function is limited in matlab to a specific number of levels.
Because imquant is limited I coded a custom function. But then an error occurs saying that huffman encoding library doesn't have that much codes to represent data.
function transformed=dct_full_image(img,macroblock_size,Q)
%k1 = macroblock row index
%k2 = macroblock column index
%macroblock_size = number of side pixels in a macroblock
for i = 1:size(img,1)
for j = 1:size(img,2)
DCTtransformed=dct2(cell2mat(img(i,j)));
uni=unique(reshape(DCTtransformed,1,[]));
Q = rem(Q,size(uni,1));
uni2 = flip(uni);
thresh = [uni(1:floor(Q/2)) uni2(1:floor(Q/2))];
maxval = [thresh max(DCTtransformed(:))];
[quant_image,index] = imquantize(DCTtransformed ,thresh,maxval);
transformed(i,j)={quant_image};
end
end
end