一、简介

在这里我想探讨一下“互相关”中的一些概念。正如卷积有线性卷积(linear convolution)和循环卷积(circular convolution)之分;互相关也有线性互相关(linear cross-correlation)和循环互相关(circular cross-correlation)。线性互相关和循环互相关的基本公式是一致的,不同之处在于如何处理边界数据。其本质的不同在于它们对原始数据的看法不同。通过这篇文章,我想整理一下相关概念,并给出示例。

1 线性相关(Linear Cross-Correlation)的定义和计算【图像配准】基于互相关图像配准matlab源码含GUI_分享【图像配准】基于互相关图像配准matlab源码含GUI_分享_02【图像配准】基于互相关图像配准matlab源码含GUI_分享_03【图像配准】基于互相关图像配准matlab源码含GUI_分享_04
用一个实际的应用例子来验证一下吧。如图3的第一个子图表示雷达声纳发射了一个探测信号。经过一段时间之后,收到了如图3的第二个子图所示的回波(带有一定的噪声)。此时我们关注的是如何确定回波中从何时开始是对探测信号的响应,以便计算目标距雷达的距离,这就需要用到线性互相关。在第三个子图中的‘Valid’曲线即是有效互相关数据,其中清晰地呈现出两处与探测信号相似的回波的位置。【图像配准】基于互相关图像配准matlab源码含GUI_分享_05
线性互相关中,还有一些概念值得注意:

1 补零。由线性相关的计算式不难发现,为了计算出个完整的相关系数序列(包含那些“无效数据”在内的所有结果),需要用到一些“不存在”的点。这就需要人为地对这些值进行补充,在线性相关的计算中,对这些超出原始数据储存的区域取值为零。
2 末端效应。由图1可以发现,一头一尾的个互相关数据并没有完全“嵌入”两个原始数组的全部信息,它们或多或少地受到了人为补零的影响。因此一般认为这些数据是不可用的。
3 计算模式的选择。这个问题其实是由问题二衍生而来的,就Python语言中的函数而言,至少有两个可以直接计算线性相关:【图像配准】基于互相关图像配准matlab源码含GUI_分享_06
2 循环互相关(Circular Cross-Correlation)的定义和计算【图像配准】基于互相关图像配准matlab源码含GUI_分享_07【图像配准】基于互相关图像配准matlab源码含GUI_分享_08【图像配准】基于互相关图像配准matlab源码含GUI_分享_09【图像配准】基于互相关图像配准matlab源码含GUI_分享_10

二、源代码

function varargout = xiangguan(varargin)
% XIANGGUAN M-file for xiangguan.fig
%      XIANGGUAN, by itself, creates a new XIANGGUAN or raises the existing
%      singleton*.
%
%      H = XIANGGUAN returns the handle to a new XIANGGUAN or the handle to
%      the existing singleton*.
%
%      XIANGGUAN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in XIANGGUAN.M with the given input arguments.
%
%      XIANGGUAN('Property','Value',...) creates a new XIANGGUAN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before xiangguan_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to xiangguan_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help xiangguan

% Last Modified by GUIDE v2.5 20-Apr-2014 10:31:06

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @xiangguan_OpeningFcn, ...
                   'gui_OutputFcn',  @xiangguan_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before xiangguan is made visible.
function xiangguan_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to xiangguan (see VARARGIN)

% Choose default command line output for xiangguan
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes xiangguan wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = xiangguan_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in first_picture.
function first_picture_Callback(hObject, eventdata, handles)
% hObject    handle to first_picture (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
subplot(3,3,2)
[filename,pathname]=uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'open');    %打开路径下要检索的图像
if isequal([filename,pathname],[0,0])
    return
else
    %读取图片
    pic = fullfile(pathname,filename);
   global onion;
   onion = imread(pic);
     imshow(onion); 
end

% --- Executes on button press in second_picture.
function second_picture_Callback(hObject, eventdata, handles)
% hObject    handle to second_picture (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

subplot(3,3,3)
[filename,pathname]=uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'open');    %打开路径下要检索的图像
if isequal([filename,pathname],[0,0])
    return
else
    %读取图片
    pict = fullfile(pathname,filename);
   global peppers;
   peppers = imread(pict);
     imshow(peppers); 
end

% --- Executes on button press in correlation.
function correlation_Callback(hObject, eventdata, handles)
% hObject    handle to correlation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
subplot(3,3,5)
global onion;
global peppers;
% non-interactively
rect_onion = [111 33 65 58];
rect_peppers = [163 47 143 151];
sub_onion = imcrop(onion,rect_onion);
sub_peppers = imcrop(peppers,rect_peppers);
c = normxcorr2(sub_onion(:,:,1),sub_peppers(:,:,1));
 surf(c), shading flat

% --- Executes on button press in overlay.
function overlay_Callback(hObject, eventdata, handles)
% hObject    handle to overlay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
subplot(3,3,6)
global onion;
global peppers;
% non-interactively
rect_onion = [111 33 65 58];
rect_peppers = [163 47 143 151];
sub_onion = imcrop(onion,rect_onion);
sub_peppers = imcrop(peppers,rect_peppers);
c = normxcorr2(sub_onion(:,:,1),sub_peppers(:,:,1));
 
 % offset found by correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(sub_onion,2)) 
               (ypeak-size(sub_onion,1))];
% relative offset of position of subimages
rect_offset = [(rect_peppers(1)-rect_onion(1)) 
               (rect_peppers(2)-rect_onion(2))];
% total offset
offset = corr_offset + rect_offset;
xoffset = offset(1);
yoffset = offset(2);
xbegin = round(xoffset+1);
xend   = round(xoffset+ size(onion,2));
ybegin = round(yoffset+1);
yend   = round(yoffset+size(onion,1));
% extract region from peppers and compare to onion
extracted_onion = peppers(ybegin:yend,xbegin:xend,:);
if isequal(onion,extracted_onion) 
   disp('onion.png was extracted from peppers.png')
end
recovered_onion = uint8(zeros(size(peppers)));
recovered_onion(ybegin:yend,xbegin:xend,:) = onion;
imshow(recovered_onion)


% --- Executes on button press in transparent.
function transparent_Callback(hObject, eventdata, handles)
% hObject    handle to transparent (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
subplot(3,3,8)
global onion;
global peppers;
% non-interactively
rect_onion = [111 33 65 58];
rect_peppers = [163 47 143 151];
sub_onion = imcrop(onion,rect_onion);
sub_peppers = imcrop(peppers,rect_peppers);
c = normxcorr2(sub_onion(:,:,1),sub_peppers(:,:,1));
 % offset found by correlation
[max_c, imax] = max(abs(c(:)));
[ypeak, xpeak] = ind2sub(size(c),imax(1));
corr_offset = [(xpeak-size(sub_onion,2)) 
               (ypeak-size(sub_onion,1))];
% relative offset of position of subimages
rect_offset = [(rect_peppers(1)-rect_onion(1)) 
               (rect_peppers(2)-rect_onion(2))];
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  •  

三、运行结果

【图像配准】基于互相关图像配准matlab源码含GUI_分享_11