Step 3 - Inverse Zig Zag
inv_zigzag = inverseZigZag(recov_run_length,macrobock_size,rvec,cvec,y,x)
Step 3 - Inverse Zig Zag
inv_zigzag = inverseZigZag(recov_run_length,macrobock_size,rvec,cvec,y,x)
First we have to divide the one long vector into blocks.(macroblock x macroblock)
Below example the vector [1 2 3 4 5 6 7 8] is divided in to 2x2 blocks. Then an block contains 4 elements. Now we have to order them in inverse zigzag order.
function inv_zigzag = inverseZigZag(recov_run_length,macrobock_size)
a=1;
while(a<size(recov_run_length,2))
one_block = recov_run_length(a:a+macrobock_size*macrobock_size-1)
a=a+macrobock_size*macrobock_size;
end
end
Fig 1
Fig 2
Fig 3
From zig zag scanning (Fig 3) the matrix we obtained by inverse zigzag scanning (Fig 2) we can see that we obtained the vector (1:18) which we initially used in Fig 1.
function inv_zigzag = inverseZigZag(recov_run_length,macrobock_size,rvec,cvec,y,x)
a=1;
inv_zigzag = {};
col=1;row=1;
while(a<size(recov_run_length,2))
one_block = recov_run_length(a:a+macrobock_size*macrobock_size-1);
temp_block=ones(macrobock_size);
for elemant=1:macrobock_size*macrobock_size
temp_block( rvec(elemant), cvec(elemant) )=one_block(elemant);
end
if(col<=x)
inv_zigzag(row,col) = {temp_block};
col=col+1;
else
col=1;
row=row+1;
inv_zigzag(row,col) = {temp_block};
col=col+1;
end
a=a+macrobock_size*macrobock_size;
end
end
%recov_run_length - One long vector.this is to e converted to a 2d matrix
%macro block size - numer of pixels in a side of a macroblock
%rvec,cvec - how zigzag indices are organized
%y - number of macroblocks in height direction of the image.(row )
%y - number of macroblocks in length direction of the image.(column )
%check --->inv_zigzag = inverseZigZag((1:18),3,[1,1,2,3,2,1,],[1,2,1,2],2,1)
The DCT_transformed_image is fully recovered from this. We can see that both signals match.
sum(sum(cell2mat(recov_DCT_tf_img)-cell2mat(DCT_tf_img))) %answer------->0