1 | % Demonstration of feature matching via simple correlation, and then using |
---|
2 | % RANSAC to estimate the homography matrix and at the same time identify |
---|
3 | % (mostly) inlying matches |
---|
4 | |
---|
5 | % Peter Kovesi |
---|
6 | % School of Computer Science & Software Engineering |
---|
7 | % The University of Western Australia |
---|
8 | % pk at csse uwa edu au |
---|
9 | % http://www.csse.uwa.edu.au/~pk |
---|
10 | % |
---|
11 | % February 2004 |
---|
12 | |
---|
13 | % Adapted to use vgg functions by Peter Kovesi and Andrew Zisserman |
---|
14 | |
---|
15 | function H = testhomog_vgg |
---|
16 | |
---|
17 | close all |
---|
18 | |
---|
19 | thresh = 500; % Harris corner threshold |
---|
20 | nonmaxrad = 3; % Non-maximal suppression radius |
---|
21 | dmax = 100; |
---|
22 | w = 11; % Window size for correlation matching |
---|
23 | |
---|
24 | im1 = rgb2gray(imread('keble.000.png')); |
---|
25 | im2 = rgb2gray(imread('keble.003.png')); |
---|
26 | |
---|
27 | % Find Harris corners in image1 and image2 |
---|
28 | [cim1, r1, c1] = harris(im1, 1, thresh, 3); |
---|
29 | show(im1,1), hold on, plot(c1,r1,'r+'); |
---|
30 | |
---|
31 | [cim2, r2, c2] = harris(im2, 1, thresh, 3); |
---|
32 | show(im2,2), hold on, plot(c2,r2,'r+'); |
---|
33 | |
---|
34 | drawnow |
---|
35 | |
---|
36 | tic |
---|
37 | [m1,m2] = matchbycorrelation(im1, [r1';c1'], im2, [r2';c2'], w, dmax); |
---|
38 | toc |
---|
39 | % Display putative matches |
---|
40 | show(im1,3), set(3,'name','Putative matches'), hold on |
---|
41 | for n = 1:length(m1); |
---|
42 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)]) |
---|
43 | end |
---|
44 | |
---|
45 | % Assemble homogeneous feature coordinates for fitting of the |
---|
46 | % homography matrix, note that [x,y] corresponds to [col, row] |
---|
47 | x1 = [m1(2,:); m1(1,:); ones(1,length(m1))]; |
---|
48 | x2 = [m2(2,:); m2(1,:); ones(1,length(m1))]; |
---|
49 | |
---|
50 | t = .001; % Distance threshold for deciding outliers |
---|
51 | [H, inliers] = ransacfithomography_vgg(x1, x2, t); |
---|
52 | |
---|
53 | fprintf('Number of inliers was %d (%d%%) \n', ... |
---|
54 | length(inliers),round(100*length(inliers)/length(m1))) |
---|
55 | fprintf('Number of putative matches was %d \n', length(m1)) |
---|
56 | |
---|
57 | % Display both images overlayed with inlying matched feature points |
---|
58 | show(double(im1)+double(im2),4), set(4,'name','Inlying matches'), hold on |
---|
59 | plot(m1(2,inliers),m1(1,inliers),'r+'); |
---|
60 | plot(m2(2,inliers),m2(1,inliers),'g+'); |
---|
61 | |
---|
62 | % Step through each matched pair of points and display the |
---|
63 | % line linking the points on the overlayed images. |
---|
64 | |
---|
65 | for n = inliers |
---|
66 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1]) |
---|
67 | end |
---|
68 | |
---|
69 | return |
---|
70 | |
---|
71 | |
---|