?? getmv2.asv
字號:
function [x1,y1,flag]=GetMV2(x0,y0,m,n,size)
%正方形四步搜索
global CURIMAGE PREIMAGE
m=m*2;
n=n*2;
Point=struct('x',0,'y',0,'diff',0);
MatchPoint=struct('x',0,'y',0,'nextP',[0,0,0.01]);
toComparePoint(1:8)=MatchPoint;
A=Point;
A.x=x0;
A.y=y0;
A.diff=GetDiff(A, A);
flag=0;
matchBlock=MatchPoint ;
matchBlock.x=x0;
matchBlock.y=y0;
matchBlock.nextP=A;
%初始化待匹配點
toMatchPoint(1).x=A.x;
toMatchPoint(1).y=A.y+2;%*16;
toMatchPoint(2).x=A.x+2;%*16;
toMatchPoint(2).y=A.y;
toMatchPoint(6).x=A.x;
toMatchPoint(6).y=A.y-2;%*16;
toMatchPoint(7).x=A.x-2;%*16;
toMatchPoint(7).y=A.y;toMatchPoint(3).x=A.x-1;%*16;
toMatchPoint(3).y=A.y-1;%*16;
toMatchPoint(4).x=A.x+1;%*16;
toMatchPoint(4).y=A.y-1;%*16;
toMatchPoint(5).x=A.x-1;%*16;
toMatchPoint(5).y=A.y+1;%*16;
toMatchPoint(8).x=A.x+1;%*16;
toMatchPoint(8).y=A.y+1;%*16;
%記錄搜索次數
state=0;
while state<20
state=state+1;
if state==1
for i=1:8
%如果超過邊界,繼續下一個循環
if toMatchPoint(i).x>(m-1)*size | toMatchPoint(i).x<1 | toMatchPoint(i).y>(n-1)*size | toMatchPoint(i).y<1
continue;%如果超過邊界,繼續下一個循環
else
toMatchPoint(i).diff=GetDiff(A,toMatchPoint(i));
%尋找Diff最小的點
if toMatchPoint(i).diff<matchBlock.nextP.diff
matchBlock.nextP.diff=toMatchPoint(i).diff;
matchBlock.nextP.x=toMatchPoint(i).x;
matchBlock.nextP.y=toMatchPoint(i).y;
end
end
end
%如果中心點為最小
elseif matchBlock.x==matchBlock.nextP.x & matchBlock.y==matchBlock.nextP.y
%更新搜索窗口的中心點
matchBlock.x=matchBlock.nextP.x;
matchBlock.y=matchBlock.nextP.y;
matchBlock.diff=matchBlock.nextP.diff;
%計算所有點的Diff值,并找到Diff最小的點
toMatchPoint(1).x=matchBlock.x+1;%*16;
toMatchPoint(1).y=matchBlock.y;
toMatchPoint(2).x=matchBlock.x-1;%*16;
toMatchPoint(3).y=matchBlock.y;
toMatchPoint(3).x=matchBlock.x;
toMatchPoint(3).y=matchBlock.y-1;%*16;
toMatchPoint(4).x=matchBlock.x;
toMatchPoint(4).y=matchBlock.y+1;%*16;
for i=1:4
%尋找Diff最小的點
if toMatchPoint(i).x>(m-1)*size | toMatchPoint(i).x<1 | toMatchPoint(i).y>(n-1)*size | toMatchPoint(i).y<1
continue;%如果超過邊界,繼續下一個循環
else
toMatchPoint(i).diff=GetDiff(A,toMatchPoint(i));
if toMatchPoint(i).diff<matchBlock.nextP.diff
matchBlock.nextP.diff=toMatchPoint(i).diff;
matchBlock.nextP.x=toMatchPoint(i).x;
matchBlock.nextP.y=toMatchPoint(i).y;
end
end
end
%diff=matchBlock.nextP.diff
break; %計算運動矢量完成
%如果中心點不為最小
else
%更新搜索窗口的中心點
matchBlock.x=matchBlock.nextP.x;
matchBlock.y=matchBlock.nextP.y;
matchBlock.diff=matchBlock.nextP.diff;
toMatchPoint=SettoMatchPoint(matchBlock);
%計算所有點的Diff值,并找到Diff最小的點
for i=1:8
%尋找Diff最小的點
if toMatchPoint(i).x>(m-1)*size | toMatchPoint(i).x<1 | toMatchPoint(i).y>(n-1)*size | toMatchPoint(i).y<1
continue; %如果超過邊界,繼續下一個循環
else
toMatchPoint(i).diff=GetDiff(A,toMatchPoint(i));
if toMatchPoint(i).diff<matchBlock.nextP.diff
matchBlock.nextP.diff=toMatchPoint(i).diff;
matchBlock.nextP.x=toMatchPoint(i).x;
matchBlock.nextP.y=toMatchPoint(i).y;
end
end
end
end
end
%未找到運動矢量,說明是剛出現的點
if state>=25 | matchBlock.diff>32
flag=1;
end
x1=matchBlock.nextP.x-x0;
y1=matchBlock.nextP.y-y0;
function diffRes=GetDiff(point1, point2,size)
global CURIMAGE PREIMAGE
diffRes=0;
A=PREIMAGE(point1.x:(point1.x+size-1),point1.y:(point1.y+size-1));
B=CURIMAGE(point2.x:(point2.x+15),point2.y:(point2.y+size-1));
C=A-B;
diffRes=sum(C(:));
function [toMatchPoint]=SettoMatchPoint(A)
toMatchPoint(1).x=A.x;
toMatchPoint(1).y=A.y+2;%*16
toMatchPoint(2).x=A.x+2;%*16
toMatchPoint(2).y=A.y;
toMatchPoint(3).x=A.x-1;%*16
toMatchPoint(3).y=A.y-1;%*16
toMatchPoint(4).x=A.x+1;%*16
toMatchPoint(4).y=A.y-1;%*16
toMatchPoint(5).x=A.x-1;%*16
toMatchPoint(5).y=A.y+1;%*16
toMatchPoint(6).x=A.x;
toMatchPoint(6).y=A.y-2;%*16
toMatchPoint(7).x=A.x-2;%*16
toMatchPoint(7).y=A.y;
toMatchPoint(8).x=A.x+1;%*16
toMatchPoint(8).y=A.y+1;%*16
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -