Matlab编写最佳宏块匹配:二维对数搜索法、三步搜索法和对偶搜索法

先看结果:

二维对数搜索法:

三步搜索法:

对偶搜索法:

运动向量和算法所用时间:

直接上代码:

二维对数搜索法:

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

再分享一篇详细介绍三种算法的文章

https://pan.baidu.com/s/1eS3mNYo2KMYrCQ5DElh5iA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值