Video Compression Results
Video Compression Results
With No Quantization
frame 1
frame 2
motion compesated (frame 2 predicted using frame 1)
residual
Encoded and then decoded residual
reconstructed image
With Quantization
Q matrix
Q = [3 2 2 3 5 8 10 12;
2 2 3 4 5 12 12 11;
3 3 3 5 8 11 14 11;
3 3 4 6 10 17 16 12;
4 4 7 11 14 22 21 15;
5 7 11 13 16 12 23 18;
10 13 16 17 21 24 24 21;
14 18 19 20 22 20 20 20];
frame 1
frame 2
motion compensated
residual
Compressed residual
Reconstructed image
Compress Image - Function
function [code,dict,rvec,cvec,y,x]=compressImage(img,macrobock_size)
%important - change Q according to macroblok size
Q = [3 2 2 3 5 8 10 12;2 2 3 4 5 12 12 11;3 3 3 5 8 11 14 11;3 3 4 6 10 17 16 12;4 4 7 11 14 22 21 15;5 7 11 13 16 12 23 18;10 13 16 17 21 24 24 21;14 18 19 20 22 20 20 20]; %Q=ones(8);
[IMG,y,x]=macroblocks(img,macrobock_size);
DCT_tf_img = dct_full_image(IMG,macrobock_size,Q);
%-----------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);
end
Decompress Image - Function
function recov_IMG = decoderesidual(code,dict,rvec,cvec,y,x,macroblock_size)
%decoding recieved signal - huffman
recovered_signal = huffmandeco(code,dict);
%decoding runlength _____(run_L_coded ------->img_One_D_vector)
recov_run_length = decod_runLength(recovered_signal);
%inverse zigzag ______(img_One_D_vector--------->DCT_tf_img)
recov_DCT_tf_img =inverseZigZag(recov_run_length,macroblock_size,rvec,cvec,y,x);
%inverse dct_____(recovered_dct_tf---->recovered img)
recov_IMG = inverse_DCT(recov_DCT_tf_img);
end
Encoding Algorithm
macroblock_size = 8;
%read image
v = VideoReader("E:\sem 7\image vvideo coding\project\video_trim.mp4");
frame1 = rgb2gray(readFrame(v));
frame1 = imcrop(frame1,[0 0 macroblock_size*floor(size(frame1,2)/macroblock_size) macroblock_size*floor(size(frame1,1)/macroblock_size) ]);
first_frame = frame1;
%[compress_frame1,dict,rvec,cvec,y,x] = compressImage(frame1,macroblock_size);
%IMG1 = decoderesidual(compress_frame1,dict,rvec,cvec,y,x,macroblock_size);
%[IMG1,~,~] = macroblocks(uint8(cell2mat(IMG1)),macroblock_size);
[IMG1,~,~] = macroblocks(frame1,macroblock_size);
transmitRES={};
transmitMV={};
transmitDICT={};
for i=1:10
[IMG1,~,~] = macroblocks(uint8(frame1),macroblock_size);
frame2 = rgb2gray(readFrame(v));
frame2 = imcrop(frame2,[0 0 macroblock_size*floor(size(frame2,2)/macroblock_size) macroblock_size*floor(size(frame2,1)/macroblock_size) ]);
[IMG2,~,~] = macroblocks(frame2,macroblock_size);
mv_array = MotionVecArray(IMG1,IMG2,4);
motionCompensatedIMG = motionCompensation(IMG1,mv_array);
residual = double(frame2)- double(cell2mat(motionCompensatedIMG));
frame1 = residual+double(cell2mat(motionCompensatedIMG));
[code,dict,rvec,cvec,y,x] = compressImage(residual,macroblock_size);
transmitRES(i)= {code};
transmitMV(i) = {mv_array};
transmitDICT(i) = {dict};
%recov_residual = decoderesidual(code,dict,rvec,cvec,y,x,macroblock_size);
%reconstructed_new_ref_image = double(cell2mat(recov_residual))+double(cell2mat(motionCompensatedIMG));
end
transmit = {rvec cvec y x first_frame};
% imshow((cell2mat(motionCompensatedIMG)));
% imshow((residual));
% pic = residual+double(cell2mat(motionCompensatedIMG));
% imshow(uint8(pic));
% imshow(cell2mat(recov_residual))
% imshow(uint8(reconstructed_new_ref_image))
Decoding Algorithm
rvec = cell2mat(transmit(1));
cvec = cell2mat(transmit(2));
y = cell2mat(transmit(3));
x = cell2mat(transmit(4));
first_frame = cell2mat(transmit(5));
[IMG1,~,~] = macroblocks(first_frame,macroblock_size);
v = VideoWriter('newfile.avi');
open(v)
for i=1:size(transmitRES,2)
mv_array = transmitMV{1,i};
code = cell2mat(transmitRES(i));
dict = transmitDICT{1,i};
recov_residual = decoderesidual(code,dict,rvec,cvec,y,x,macroblock_size);
motionCompensatedIMG = motionCompensation(IMG1,mv_array);
frame = uint8(double(cell2mat(motionCompensatedIMG))+cell2mat(recov_residual));
[IMG1,~,~] = macroblocks(frame,macroblock_size);
writeVideo(v,frame);
end
close(v);
Initially Available Data
Frame 1
One time transmitted configuration vector.
These data should be available on the other side to decode the encoded signa.
transmit = {rvec , cvec ,y ,x ,first_frame};
Above mentioned first frame is also included here.
Transmitted Data
Then for each iteration/frames these 3 data should be transmitted. (10 Frames)
transmitRES(i)= {code}; -----------> Residual
transmitMV(i) = {mv_array}; -------->Motion Vector array
transmitDICT(i) = {dict}; ----------> Huffman dictionary
Huffman dictionary - This is required to decode the Huffman encoded signal
Motion Vector Array
Compressed residual
Data transmitted per frame = number of Huffman dictionary variables per frame + number of MV array variables per frame + number of Residual array per variables per frame
Number of average Huffman dictionary variables per frame - 1018
Number of MV array variables - 10560
Number of average residual array variables - 1561538
Data transmitted per frame = 1565784
Size of a single frame = 5406720
Compression ratio = 5406720/1561538 = 3.4624