📄 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 + -