A list,

Here I would like to explore some of the concepts of cross-correlation. Just as convolution can be divided into linear convolution and circular convolution; There are also linear cross-correlation and circular cross-correlation. The basic formula of linear and cyclic cross-correlation is the same, the difference is how to deal with boundary data. The essential difference lies in their view of the raw data. In this article, I want to sort through the concepts and give examples.

Definition and calculation of Linear cross-correlation









Verify this with a practical example. The first subgraph in Figure 3 shows that the radar sonar emits a detection signal. After some time, an echo (with some noise) as shown in the second subgraph of Figure 3 is received. At this point, we focus on how to determine when the echo is the response to the detection signal, so as to calculate the distance between the target and the radar, which requires the use of linear cross-correlation. The “Valid” curve in the third subgraph is the effective cross-correlation data, which clearly shows the position of two echoes similar to the detection signal.



In linear cross-correlation, some other concepts are worth noting:

1 zero padding. It is not difficult to see from the linear correlation formula that in order to compute a complete sequence of correlation coefficients (including all the “invalid data”), some “non-existent” points need to be used. This requires artificially supplementing the values to zero for the regions beyond the original data store in linearly dependent calculations.

2. End effect. It can be seen from Figure 1 that the cross-correlation data of one end and one end are not completely “embedded” in all the information of the two original arrays, and they are more or less affected by artificial zeroing. Therefore, these data are generally considered unavailable.

3 Selection of computing mode. This problem is actually derived from question 2. In Python, there are at least two functions that can directly calculate linear dependence:



Definition and calculation of Circular cross-correlation







Ii. Source code

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))];
Copy the code
  •  

3. Operation results

Complete code or simulation consulting to add QQ1575304183