gusucode.com > Matlab基于金字塔结构的图像处理 GUI界面源码程序 > PydXcorr.m

    function [ybegin,xbegin,yend,xend] = PydXcorr(img,tmp,level,thresh)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function [ybegin,xbegin,yend,xend] = PydXcorr(img,tmp,level,thresh)
% 
% NOTE: 
% Image matching based on maxium cross-correlation algorithm.
% A Pyramid decomposition algorithm is applied to generate multi-
% resolution images. In order to decrease the time and calculation
% cost, firstly, a set of potential match points is got in low  
% resolution image, then a more precise match is played in the 
% ROI(Region of Interest) of higher resolution images. At last,
% the match point coordinate is got in the original image.
%
% ARGUMENT:
%      INPUT:  img,tmp,level,thresh
%           img   -   The source image in which we search for 
%                     the target.
%           tmp   -   The target image used as a template.
%           level -   The number of level to which the image is
%                     decomposited.
%           thresh-   A thresh used in low resolution image to 
%                     determine whether a point is a potential
%                     matching point. Default value is 0.7.
%      OUTPUT:  ybegin,xbegin,yend,xend
%               -  The coordinate of the match region
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
warning off;
% set default value
if nargin == 3
    thresh = 0.7;
end

% the size of the image
[Irows,Icolumns,Ilayers] = size(img);
[Trows,Tcolumns,Tlayers] = size(tmp);
% if is RGB, transform to GRAYSCALE
if (Ilayers==3)
    img = rgb2gray(img);
end
if (Tlayers==3)
    tmp = rgb2gray(tmp);
end

% time on
hbar = waitbar(0,'Image matching');
tic;

% pyramid decomposition
imgpyd = Pyramid_Decomposition(img,level);
tmppyd = Pyramid_Decomposition(tmp,level);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% match in low resolution image
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% calculate cross-correlation
c = normxcorr2(tmppyd{level+1},imgpyd{level+1});

% get peak value and position
[ymax,xmax] = find(abs(c)>thresh);  
offset = [ymax-size(tmppyd{level+1},1),...
          xmax-size(tmppyd{level+1},2)];

% delete repeated match point
offset1 = [];
offset1 = [offset1;offset(1,:)];
for n1 = 2:size(offset,1)
    m = 0;
    for n2 = 1:size(offset1,1)
        if (abs(offset(n1,1)-offset1(n2,1))>3 && ...
               abs(offset(n1,2)-offset1(n2,2))>3 )
            m = m+1;
        end
    end
    if m == size(offset1,1)
        offset1 = [offset1;offset(n1,:)];
    end
end
yoffset = offset1(:,1);
xoffset = offset1(:,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% match in high resolution images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ii = level;
while ii
    i = 1;
    n = length(yoffset);
    while i <= n
        % get the ROI
        yobj1 = 2*yoffset(i)-10;
        yobj2 = 2*yoffset(i)+size(tmppyd{ii},1)+10;
        xobj1 = 2*xoffset(i)-10;
        xobj2 = 2*xoffset(i)+size(tmppyd{ii},2)+10;
        if yobj1<=0
            yobj1 = 1;
        end
        if xobj1<=0
            xobj1 = 1;
        end
        if yobj2>size(imgpyd{ii},1)
            yobj2 = size(imgpyd{ii},1);
        end
        if xobj2>size(imgpyd{ii},2)
            xobj2 = size(imgpyd{ii},2);
        end
        objarea = imgpyd{ii}(yobj1:yobj2,xobj1:xobj2);

        % match in ROI
        c = normxcorr2(tmppyd{ii},objarea);
        [max_c,imax] = max(abs(c(:)));
        if max_c > thresh
            [ypeak,xpeak] = ind2sub(size(c),imax(1));
            corr_offset = [ypeak-size(tmppyd{ii},1),...
                        xpeak-size(tmppyd{ii},2)];
            offset = corr_offset;
            yoffset(i) = offset(1)+2*yoffset(i)-10-1;
            xoffset(i) = offset(2)+2*xoffset(i)-10-1;
            i = i+1;
        else
            yoffset(i) = [];
            xoffset(i) = [];
            n = n-1;
        end
    end
    ii = ii-1;
end

% get the offset in the source image
ybegin = yoffset;
yend = yoffset+size(tmppyd{1},1)-1;
xbegin = xoffset;
xend = xoffset+size(tmppyd{1},2)-1;

% time off
toc;
close(hbar);