先看結果:
二維對數搜索法:
三步搜索法:
對偶搜索法:
運動向量和算法所用時間:
直接上代碼:
二維對數搜索法:
function [ Motion_vector,MB_center ] = Mylog( Reference,Target,N,p,x0,y0 )
[row,col] = size(Reference);
Motion_vector = zeros(2,1);
MB_center = zeros(2,1);
costs = ones(3,3) * 6553799999;
x = x0-N/2;
y = y0-N/2; %指定宏塊的左上角爲原點
L = floor(log10(p+1)/log10(2));
stepmax = 2^(L-1);
disp('最大步長:'),disp(stepmax);
stepsize = stepmax;
MB_center(1,1) = x0;
MB_center(2,1) = y0;
costs(2,2) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y:y+N),N);%計算x0,y0的MAD
while(stepsize >= 1)
for m = -stepsize : stepsize : stepsize
for n = -stepsize : stepsize : stepsize
dispX = x + m;
dispY = y + n;
if ( dispX < 1 || dispX+N > row || dispY < 1 || dispY+N > col)
continue;
end
costsx = m/stepsize + 2;
costsy = n/stepsize + 2;
if (costsx == 2 && costsy == 2)
continue;
end
% if(costsx ~=2 && costsy ~=2) %只算4個點的值
% continue;
% end
%if(costsx == 2 || costsy == 2)
costs(costsx,costsy) = costFuncMAD(Reference(x:x+N,y:y+N)...
,Target(dispX:dispX+N , dispY:dispY+N),N);
%end
end
end
[dx,dy] = minCost(costs);
x = x+(dx-2)*stepsize;
y = y+(dy-2)*stepsize;
if (dx==2 && dy==2)
stepsize = stepsize/2;
end
costs(2,2) = costs(dx,dy);
end
Motion_vector(1,1) = x - (x0-N/2);
Motion_vector(2,1) = y - (y0-N/2);
motionVect = Motion_vector;
disp('運動向量:');
disp(motionVect); %展示運動矢量
figure;
subplot(2,1,1);
imshow(Target,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
title('Target frame');
subplot(2,1,2);
imshow(Reference,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
rectangle('position',[x,y,N,N],'edgecolor','r');
title('Reference frame');
end
三步搜索法:
function [ Motion_vector,MB_center ] = Mytri( Reference,Target,N,p,x0,y0 )
[row,col] = size(Reference);
Motion_vector = zeros(2,1);
MB_center = zeros(2,1);
costs = ones(3,3) * 65537;
x = x0-N/2;
y = y0-N/2; %指定宏塊的左上角爲原點
L = floor(log10(p+1)/log10(2));
stepmax = 2^(L-1);
stepsize = stepmax;
MB_center(1,1) = x0;
MB_center(2,1) = y0;
costs(2,2) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y:y+N),N);%計算x0,y0的MAD
while(stepsize >= 1)
for m = -stepsize : stepsize : stepsize %計算9個點的MAD值
for n = -stepsize : stepsize : stepsize
dispX = x + m;
dispY = y + n;
if ( dispX < 1 || dispX+N > row || dispY < 1 || dispY+N > col)
continue;
end
costsx = m/stepsize + 2;
costsy = n/stepsize + 2;
if (costsx == 2 && costsy == 2)
continue;
end
costs(costsx,costsy) = costFuncMAD(Reference(x:x+N,y:y+N)...
,Target(dispX:dispX+N , dispY:dispY+N),N);%計算此步長周圍其餘8個點的MAD
end
end
[dx,dy] = minCost(costs);
x = x+(dx-2)*stepsize;
y = y+(dy-2)*stepsize;
%if (dx==2 && dy==2)
stepsize = stepsize/2;
%end
costs(2,2) = costs(dx,dy);
end
Motion_vector(1,1) = x - (x0-N/2);
Motion_vector(2,1) = y - (y0-N/2);
motionVect = Motion_vector;
disp('運動向量:');
disp(motionVect); %展示運動矢量
figure;
subplot(2,1,1);
imshow(Target,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
title('Target frame');
subplot(2,1,2);
imshow(Reference,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
rectangle('position',[x,y,N,N],'edgecolor','r');
title('Reference frame');
end
對偶搜索法:
function [ Motion_vector,MB_center ] = Mycon( Reference,Target,N,x0,y0 )
Motion_vector = zeros(2,1);
MB_center = zeros(2,1);
x = x0-N/2;
y = y0-N/2; %指定宏塊的左上角爲原點
step = 1;
costsx = ones(1,3) * 6553799999; %存放i方向
costsy = ones(1,3) * 6553799999; %存放j方向
costsx(1,1) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x-1:x-1+N,y:y+N),N);
costsx(1,2) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y:y+N),N);%計算x0,y0的MAD
costsx(1,3) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x-1:x-1+N,y:y+N),N);
while (costsx(1,1) < costsx(1,2) || costsx(1,3) < costsx(1,2))%(1,2)最小開始y方向尋找
if(costsx(1,1) < costsx(1,3))%(1,1)最小
costsx(1,2) = costsx(1,1);
x = x-1; %向左移動
y=y;
else %(1,3)最小
costsx(1,2) = costsx(1,3);
x = x+1; %向右移動
y =y;
end
costsx(1,1) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x-1:x-1+N,y:y+N),N);
costsx(1,3) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x+1:x+1+N,y:y+N),N);%計算x0,y0的MAD
end
costsy(1,1) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y-1:y-1+N),N);%向下
costsy(1,2) = costsx(1,2);
costsy(1,3) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y+1:y+1+N),N);%向上
while (costsy(1,1) < costsy(1,2) || costsy(1,3) < costsy(1,2))%(1,2)最小開始y方向尋找
if(costsy(1,1) < costsy(1,3))%(1,1)最小
costsy(1,2) = costsy(1,1);
x = x; %向下移動
y=y-1;
else %(1,3)最小
costsx(1,2) = costsx(1,3);
x = x; %向上移動
y =y+1;
end
costsy(1,1) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y-1:y-1+N),N);
costsy(1,3) = costFuncMAD(Reference(x:x+N,y:y+N),...
Target(x:x+N,y+1:y+1+N),N);%計算x0,y0的MAD
end
Motion_vector(1,1) = x - (x0-N/2);
Motion_vector(2,1) = y - (y0-N/2);
motionVect = Motion_vector;
disp('運動向量:');
disp(motionVect); %展示運動矢量
figure;
subplot(2,1,1);
imshow(Target,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
title('Target frame');
subplot(2,1,2);
imshow(Reference,[]);
rectangle('position',[x0-N/2,y0-N/2,N,N],'edgecolor','b');
rectangle('position',[x,y,N,N],'edgecolor','r');
title('Reference frame');
end
完整代碼下載:
https://download.csdn.net/download/qq_40167046/12005689
再分享一篇詳細介紹三種算法的文章