-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimageAlignTutorial.m
More file actions
57 lines (49 loc) · 1.74 KB
/
Copy pathimageAlignTutorial.m
File metadata and controls
57 lines (49 loc) · 1.74 KB
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
% imageAlignTutorial
%
% The long-term goal of this is to figure out fast ways
% to do image aligment. This is a simple start using
% Matlab's cross correlation function, and provides
% an upper bound.
%
% Assumes that test image is a subset of full iamge, and
% differs only in xy translation.
%
% Options to think about. GPU computation, map-seeking circuit
% algorithm, sparsifying image and using a sparse representation.
%
% 3/19/12 dhb Wrote it.
%% Clear
clear all; close all
%% Parameters
fullImageRows = 64;
fullImageCols = 128;
testImageRows = 16;
testImageCols = 64;
testImageRowOffset = 5;
testImageColOffset = 10;
%% Generate full test image. Filtered random noise.
probOnes = 0.2;
fullImage = binornd(1,probOnes,fullImageRows,fullImageCols);
convKernalHalfSize = 5;
convKernalSigma = 6;
[X1,X2] = meshgrid((-convKernalHalfSize:convKernalHalfSize)',(-convKernalHalfSize:convKernalHalfSize)');
X = [X1(:) X2(:)];
convKernal = mvnpdf(X, [0 0], [convKernalSigma 0 ; 0 convKernalSigma]);
convKernal = convKernal/sum(convKernal(:));
convKernal = reshape(convKernal,length((-convKernalHalfSize:convKernalHalfSize)),length((-convKernalHalfSize:convKernalHalfSize)));
fullImage = conv2(fullImage,convKernal,'same');
figure; clf;
imshow(fullImage);
%% Extract reference image and test image
testImage = fullImage(testImageRowOffset:testImageRowOffset+testImageRows,...
testImageColOffset:testImageColOffset+testImageCols);
[testRows,testCols] = size(testImage);
%% Figure out where test is, using cross-correlation
% This is right out of the documentation for normxcorr2
tic;
cc = normxcorr2(testImage,fullImage);
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-testRows+1) (xpeak-testCols+1) ];
toc
corr_offset