code repository
Function to calculate Mean Absolute Difference the function used in video coding:
function [M_A_D,dx1,dy1]=MAD_OBJECT(x,y,dx,dy,M_A_D,dx1,dy1,ref,current)
if (x+dx)<1|(y+dy)<1
break
elseif((x+dx+15)>size(ref,1))|((y+dy+15)>size(ref,2))
break
else
% [ref]= padding(x,y,dx,dy,current,ref);
M_A_D1=0;
for i=0:15
for j=0:15
if(current(x+i,y+j)~=0) %if the search range is outside the matrix dimension search will be stopped.
M_A_D1=M_A_D1+abs(current(x+i,y+j)-ref(x+dx+i,y+dy+j));
end
end
end
if(M_A_D>M_A_D1)
M_A_D=M_A_D1;
dx1=dx;
dy1=dy;
% elseif (M_A_D=M_A_D1)
% dx1_1=dx;
% dy1_1=dy;
% end
end
MOTION ESTIMATION FUNCTION
clear all
close all
[ref map]=imread('c:\matlabR12\testing\claire011.jpeg'); %previous frame to find motion vector.
%ref=rgb2gray(ref);
ref=imresize(ref,[176 176],'bilinear');
ref=255*im2double(ref);
[current map]=imread('c:\matlabR12\testing\claire012.jpeg'); %current frame which motion vectors to be found.
current=imresize(current,[176 176],'bilinear');
current=255*im2double(current);
% current=rgb2gray(current);
% ref=double(ref); % conversion in to double image to do mathematiscal operations.
% current=double(current);
MOTION_VECTOR_x=[]; %initialization motion vectors.
MOTION_VECTOR_y=[];
%initialization of initial MAD value.
M_A_D_INITIALIZE=abs(max(max(ref))*size(ref,1)-min(min(current))*size(current,1));
m=0;
n=0;
tic;
for x=1:16:size(current,1)
for y=1:16:size(current,2)
M_A_D=M_A_D_INITIALIZE;
dx1=1; %initial motion vector pointer.
dy1=1;
%step1 of three step motion estimation algorithm.
for L=1:9
M_A_D1=0;
switch(L)
case(1)
dx=-4;dy=-4;
case(2)
dx=-4;dy=0;
case(3)
dx=-4;dy=4;
case(4)
dx=0;dy=-4 ;
case(5)
dx=0;dy=0; %step size of 4 from the center is taken.
case(8)
dx=4;dy=0;
case(9)
dx=4;dy=4;
case(6)
dx=0;dy=4;
case(7)
dx=4;dy=-4;
end
for i=0:15
for j=0:15
if (x+dx)<1|(y+dy)<1 %if the search range is outside the matrix dimension search will be stopped.
break
elseif(x+dx+15>size(current,1))|(y+dy+15>size(current,2))
break
else
M_A_D1=M_A_D1+abs(current(x+i,y+j)-ref(x+dx+i,y+dy+j));
end
end
end
M_A_D1=M_A_D1/256;
if (M_A_D1==0)
M_A_D=M_A_D;
dx1=dx1;
dy1=dy1;
elseif(M_A_D>M_A_D1)
M_A_D=M_A_D1;
dx1=dx;
dy1=dy;
end
end
dx2=dx1;
dy2=dy1;
%step2 of three step motion estimation algorithm.
for L=1:8
M_A_D1=0;
switch(L)
case(1)
dx=dx2-2; dy=dy2-2;
case(2)
dx=dx2-2; dy=dy2;
case(3)
dx=dx2-2; dy=dy2+2;
case(4)
dx=dx2; dy=dy2-2; %step size of 2 from the center is taken.
case(5)
dx=dx2; dy=dy2+2;
case(6)
dx=dx2+2; dy=dy2-2;
case(7)
dx=dx2+2; dy=dy2;
case(8)
dx=dx2+2; dy=dy2+2;
end
for i=0:15
for j=0:15
if (x+dx)<1|(y+dy)<1 %if the search range is outside the matrix dimension search will be stopped.
break
elseif(x+dx+15>size(current,1))|(y+dy+15>size(current,2))
break
else
M_A_D1=M_A_D1+abs(current(x+i,y+j)-ref(x+dx+i,y+dy+j));
end
end
end
M_A_D1=M_A_D1/256;
if (M_A_D1==0)
M_A_D=M_A_D;
dx1=dx1;
dy1=dy1;
elseif (M_A_D>M_A_D1)
M_A_D=M_A_D1;
dx1=dx;
dy1=dy;
end
end
dx2=dx1;
dy2=dy1;
%step3 of three step motion estimation algorithm.
for L=1:8
M_A_D1=0;
switch(L)
case(1)
dx=dx2-1;dy=dy2-1;
case(2)
dx=dx2-1;dy=dy2;
case(3)
dx=dx2-1;dy=dy2+1;
case(4)
dx=dx2;dy=dy2-1;
case(5)
dx=dx2;dy=dy2+1; %step size of 1 from the center is taken.
case(6)
dx=dx2+1;dy=dy2-1;
case(7)
dx=dx2+1;dy=dy2;
case(8)
dx=dx2+1;dy=dy2+1;
end
for i=0:15
for j=0:15
if (x+dx)<1|(y+dy)<1 %if the search range is outside the matrix dimension search will be stopped.
break
elseif(x+dx+15>size(current,1))|(y+dy+15>size(current,2))
break
else
M_A_D1=M_A_D1+abs(current(x+i,y+j)-ref(x+dx+i,y+dy+j));
end
end
end
M_A_D1=M_A_D1/256;
if (M_A_D1==0)
M_A_D=M_A_D;
dx1=dx1;
dy1=dy1;
elseif (M_A_D>M_A_D1)
M_A_D=M_A_D1;
dx1=dx;
dy1=dy;
end
end
MOTION_VECTOR_x(x-m,y-n)=dx1; %motion vectors in horizontal direction.
MOTION_VECTOR_y(x-m,y-n)=dy1; %motion vectors in vertical direction.
n=n+15;
end
m=m+15;
n=0;
end
toc;
%MOTION COMPENSATION
% m=0;
% n=0;
% dx=[];
% dx=MOTION_VECTOR_x;
% dy=[];
% dy=MOTION_VECTOR_y;
% ESTIMATED_FRAME=zeros(size(current)); %initialization of ESTIMATED_FRAME.
% for x=1:16:size(current,1)
% for y=1:16:size(current,2)
% for i=0:15
% for j=0:15
% ESTIMATED_FRAME(x+i,y+j)=ref(x+i+dx(x-m,y-n),y+j+dy(x-m,y-n)); %ESTIMATED_FRAME.
% end
% end
% n=n+15;
% end
% n=0;
% m=m+15;
% end
%
% DIFF=current-ESTIMATED_FRAME; %frame difference between current frame and ESTIMATED_FRAME.
% COMPENSATED_FRAME=DIFF+ESTIMATED_FRAME;
% error=sum(sum((current-COMPENSATED_FRAME).^2));
% MSE=sqrt(error/(size(current,1)*size(current,2)));
% PSNR=20*log10(255/MSE) %peak signal to noise ratio.
% current=uint8(current);
% ref=uint8(ref);
% COMPENSATED_FRAME=uint8(COMPENSATED_FRAME);
% ESTIMATED_FRAME=uint8(ESTIMATED_FRAME);
% subplot(221),imshow(current),title('original image'); % original(current) frame.
% subplot(222),imshow(COMPENSATED_FRAME),title('motion compensated image'); %MOTION COMPENSATED_FRAME.
% subplot(223),imshow(ref),title('reference image'); %reference(previous) frame.
% subplot(224),imshow(ESTIMATED_FRAME),title('predicted frame'); %MOTION PREDICTED_FRAME.
% toc;
% %time taken to execute programme.
C EXAMPLAES
/*
This file has basic codes for the tricks on linked list developed by me
It helps for the people who are looking for the simple code to understand linked list data structure
*/
#include <stdio.h>
#include <malloc.h> `
typedef struct
{
int data;
struct node *link;
}node;
void append(node **first_node, int num)
{
node *tmp, *tmp1;
if (*first_node==NULL)
{
tmp = (node *)malloc(sizeof(node));
tmp->data = num;
tmp->link = NULL;
*first_node = tmp;
//printf()
}
else
{
tmp1=*first_node;
while(tmp1!=NULL)
{
tmp=tmp1;
tmp1 = tmp1->link;
}
tmp1 = (node *)malloc(sizeof(node));
tmp->link = tmp1;
tmp1->link=NULL;
tmp1->data = num;
}
}
void append_start(node **new_node, int num)
{
node *tmp;
if (*new_node==NULL)
{
tmp = (node *) malloc(sizeof(node));
}
tmp->data = num;
tmp->link = NULL;
*new_node=tmp;
}
void print_linkedlist(node **head)
{
node *tmp;
tmp = *head;
while(tmp)
{
printf("Data is %d\n", tmp->data);
printf("Node is %x\n", tmp->link);
tmp = tmp->link;
}
}
void reverselist(node **head)
{
node *tmp, *tmp1, *tmp2;
int i=0;
tmp = *head;
tmp2 = NULL;
while(tmp)
{
tmp1 = tmp->link;
tmp->link = tmp2;
tmp2 = tmp;
tmp = tmp1;
//tmp = tmp->link;
i++;
//printf("Iteration number is %d\n", i);
}
*head = tmp2;
//printf("Reverse Linked list is %x\n", tmp1);
}
void appendinmid(node **head, int num, int pos)
{
node *tmp, *tmp1, *tmp2;
int i=pos;
tmp = *head;
while(i)
{
tmp1 = tmp;
tmp = tmp->link;
--i;
}
tmp2 = (node *)malloc(sizeof(node));
tmp2->data = num;
tmp2->link = tmp;
tmp1->link = tmp2;
}
void deleteinmid(node **head, int pos)
{
node *tmp, *tmp1, *tmp2;
int i=pos-1;
tmp = *head;
while(i)
{
tmp1 = tmp;
tmp = tmp->link;
--i;
}
tmp2 = tmp->link;
tmp1->link = tmp2;
}
void main()
{
node *p;
append_start(&p,1);
append(&p,2);
append(&p,3);
append(&p,4);
append(&p,5);
append(&p,6);
print_linkedlist(&p);
appendinmid(&p, 1000,4);
//reverselist(&p);
printf("\n***********ReverseLink List is ********************\n");
print_linkedlist(&p);
deleteinmid(&p, 5);
printf("\n***********ReverseLink List is ********************\n");
print_linkedlist(&p);
}