1 | % Demonstration of feature matching via simple correlation, and then using |
---|
2 | % RANSAC to estimate the fundamental matrix and at the same time identify |
---|
3 | % (mostly) inlying matches |
---|
4 | % |
---|
5 | % Usage: testfund - Demonstrates fundamental matrix calculation |
---|
6 | % on two default images |
---|
7 | % testfund(im1,im2) - Computes fundamental matrix on two supplied images |
---|
8 | % |
---|
9 | % Edit code as necessary to tweak parameters |
---|
10 | |
---|
11 | % Copyright (c) 2004-2005 Peter Kovesi |
---|
12 | % School of Computer Science & Software Engineering |
---|
13 | % The University of Western Australia |
---|
14 | % http://www.csse.uwa.edu.au/ |
---|
15 | % |
---|
16 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
17 | % of this software and associated documentation files (the "Software"), to deal |
---|
18 | % in the Software without restriction, subject to the following conditions: |
---|
19 | % |
---|
20 | % The above copyright notice and this permission notice shall be included in |
---|
21 | % all copies or substantial portions of the Software. |
---|
22 | % |
---|
23 | % The Software is provided "as is", without warranty of any kind. |
---|
24 | |
---|
25 | % February 2004 |
---|
26 | % August 2005 Octave compatibility |
---|
27 | |
---|
28 | function [Ftrans,correlations]=testfund(im1,im2) |
---|
29 | |
---|
30 | if nargin == 0 |
---|
31 | im1 = imread('im02.jpg'); |
---|
32 | im2 = imread('im03.jpg'); |
---|
33 | end |
---|
34 | correlations = []; |
---|
35 | v = version; Octave=0;% Crude Octave test |
---|
36 | thresh = 500; % Harris corner threshold |
---|
37 | nonmaxrad = 3; % Non-maximal suppression radius |
---|
38 | dmax = 100; % Maximum search distance for matching |
---|
39 | w = 11; % Window size for correlation matching |
---|
40 | |
---|
41 | % Find Harris corners in image1 and image2 |
---|
42 | [cim1, r1, c1] = harris(im1, 1, thresh, 3); clear cim1; |
---|
43 | show(im1,1), hold on, plot(c1,r1,'r+'); |
---|
44 | |
---|
45 | [cim2, r2, c2] = harris(im2, 1, thresh, 3); clear cim2; |
---|
46 | show(im2,2), hold on, plot(c2,r2,'r+'); |
---|
47 | drawnow |
---|
48 | |
---|
49 | correlation = 0; % Change this between 1 or 0 to switch between the two |
---|
50 | % matching functions below |
---|
51 | |
---|
52 | if correlation % Use normalised correlation matching |
---|
53 | [m1,m2] = matchbycorrelation(im1, [r1';c1'], im2, [r2';c2'], w, dmax); |
---|
54 | |
---|
55 | else % Use monogenic phase matching |
---|
56 | nscale = 1; |
---|
57 | minWaveLength = 10; |
---|
58 | mult = 4; |
---|
59 | sigmaOnf = .2; |
---|
60 | [m1,m2] = matchbymonogenicphase(im1, [r1';c1'], im2, [r2';c2'], w, dmax,... |
---|
61 | nscale, minWaveLength, mult, sigmaOnf); |
---|
62 | end |
---|
63 | |
---|
64 | % Display putative matches |
---|
65 | show(im1,3), set(3,'name','Putative matches') |
---|
66 | if Octave, figure(1); title('Putative matches'), axis('equal'), end |
---|
67 | for n = 1:length(m1); |
---|
68 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)]) |
---|
69 | end |
---|
70 | |
---|
71 | % Assemble homogeneous feature coordinates for fitting of the |
---|
72 | % fundamental matrix, note that [x,y] corresponds to [col, row] |
---|
73 | x1 = [m1(2,:); m1(1,:); ones(1,length(m1))]; |
---|
74 | x2 = [m2(2,:); m2(1,:); ones(1,length(m1))]; |
---|
75 | |
---|
76 | t = .002; % Distance threshold for deciding outliers |
---|
77 | |
---|
78 | % Change the commenting on the lines below to switch between the use |
---|
79 | % of 7 or 8 point fundamental matrix solutions, or affine fundamental |
---|
80 | % matrix solution. |
---|
81 | % [F, inliers] = ransacfitfundmatrix7(x1, x2, t, 1); |
---|
82 | [F, inliers] = ransacfitfundmatrix(x1, x2, t, 1); |
---|
83 | % [F, inliers] = ransacfitaffinefund(x1, x2, t, 1); |
---|
84 | fprintf('Number of inliers was %d (%d%%) \n', ... |
---|
85 | length(inliers),round(100*length(inliers)/length(m1))) |
---|
86 | |
---|
87 | [Ftrans, transinliers] = ransacfittransfundmatrix(x1, x2, t, 1); |
---|
88 | correlations = [correlations;[m1(:,transinliers)',m2(:,transinliers)']]; |
---|
89 | [U,S,V]=svd(Ftrans); |
---|
90 | epipole=[V(2,3)/V(3,3);V(1,3)/V(3,3)] |
---|
91 | fprintf('Number of inliers was %d (%d%%) \n', ... |
---|
92 | length(inliers),round(100*length(transinliers)/length(m1))) |
---|
93 | fprintf('Number of putative matches was %d \n', length(m1)) |
---|
94 | |
---|
95 | % Display both images overlayed with inlying matched feature points |
---|
96 | |
---|
97 | if Octave |
---|
98 | figure(4); title('Inlying matches'), axis('equal'), |
---|
99 | else |
---|
100 | show(im1,4), set(4,'name','Inlying matches'), hold on |
---|
101 | end |
---|
102 | plot(m1(2,inliers),m1(1,inliers),'c+'); |
---|
103 | % plot(m2(2,inliers),m2(1,inliers),'g+'); |
---|
104 | |
---|
105 | for n = inliers |
---|
106 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1]) |
---|
107 | end |
---|
108 | |
---|
109 | show(im2,5), set(5,'name','Inlying matches'), hold on |
---|
110 | % plot(m1(2,inliers),m1(1,inliers),'c+'); |
---|
111 | plot(m2(2,inliers),m2(1,inliers),'g+'); |
---|
112 | |
---|
113 | for n = inliers |
---|
114 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1]) |
---|
115 | end |
---|
116 | |
---|
117 | |
---|
118 | if Octave |
---|
119 | figure(4); title('Inlying matches'), axis('equal'), |
---|
120 | else |
---|
121 | show(im1,6), set(6,'name','Translational Inlying matches'), hold on |
---|
122 | end |
---|
123 | plot(m1(2,transinliers),m1(1,transinliers),'c+'); |
---|
124 | % plot(m2(2,inliers),m2(1,inliers),'g+'); |
---|
125 | |
---|
126 | for n = transinliers |
---|
127 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1]) |
---|
128 | end |
---|
129 | |
---|
130 | show(im2,7), set(7,'name','translational Inlying matches'), hold on |
---|
131 | % plot(m1(2,inliers),m1(1,inliers),'c+'); |
---|
132 | plot(m2(2,transinliers),m2(1,transinliers),'g+'); |
---|
133 | |
---|
134 | for n = transinliers |
---|
135 | line([m1(2,n) m2(2,n)], [m1(1,n) m2(1,n)],'color',[0 0 1]) |
---|
136 | end |
---|
137 | |
---|
138 | % determine which picture is closer to the epipole |
---|
139 | p1 = [r1(transinliers)-epipole(1),c1(transinliers)-epipole(2)]'; |
---|
140 | p2 = [r2(transinliers)-epipole(1),c2(transinliers)-epipole(2)]'; |
---|
141 | dist1 = sum((sum(p1.^2)).^.5); |
---|
142 | dist2 = sum((sum(p2.^2)).^.5); |
---|
143 | cImage = (dist1>dist2)+1; |
---|
144 | |
---|
145 | DualMatches=guide([r1,c1]', [r2,c2]', epipole, 17, .85, .2,150,cImage,im1,im2 ); |
---|
146 | % correlations = [correlations;[r1(DualMatches(:,1)),c1(DualMatches(:,1)),r2(DualMatches(:,2)),c2(DualMatches(:,2))]]; |
---|
147 | correlations = [r1(DualMatches(:,1)),c1(DualMatches(:,1)),r2(DualMatches(:,2)),c2(DualMatches(:,2))]; |
---|
148 | fprintf('Number of guided matches was %d \n', size(DualMatches,1)) |
---|
149 | |
---|
150 | show(im1,9), set(9,'name','Dual matches'), hold on |
---|
151 | plot(c1(DualMatches(:,1)),r1(DualMatches(:,1)),'g+'); |
---|
152 | % plot(c2(DualMatches(:,2)),r2(DualMatches(:,2)),'g+'); |
---|
153 | |
---|
154 | %plot(epipole(2),epipole(1),'r*'); |
---|
155 | for n = 1:1:size(DualMatches,1) |
---|
156 | line([c1(DualMatches(n,1)) c2(DualMatches(n,2))], [r1(DualMatches(n,1)) r2(DualMatches(n,2))],'color',[0 0 1]) |
---|
157 | end |
---|
158 | |
---|
159 | show(im2,8), set(8,'name','Dual matches'), hold on |
---|
160 | % plot(c1(DualMatches(:,1)),r1(DualMatches(:,1)),'c+'); |
---|
161 | plot(c2(DualMatches(:,2)),r2(DualMatches(:,2)),'g+'); |
---|
162 | |
---|
163 | %plot(epipole(2),epipole(1),'r*'); |
---|
164 | for n = 1:1:size(DualMatches,1) |
---|
165 | line([c1(DualMatches(n,1)) c2(DualMatches(n,2))], [r1(DualMatches(n,1)) r2(DualMatches(n,2))],'color',[0 0 1]) |
---|
166 | end |
---|
167 | |
---|
168 | show(im1,11), set(11,'name','All matches'), hold on |
---|
169 | plot(correlations(:,2),correlations(:,1),'g+'); |
---|
170 | % plot(c2(DualMatches(:,2)),r2(DualMatches(:,2)),'g+'); |
---|
171 | |
---|
172 | %plot(epipole(2),epipole(1),'r*'); |
---|
173 | for n = 1:1:size(correlations,1) |
---|
174 | line([correlations(n,2) correlations(n,4)], [correlations(n,1) correlations(n,3)],'color',[0 0 1]) |
---|
175 | end |
---|
176 | |
---|
177 | show(im2,10), set(10,'name','All matches'), hold on |
---|
178 | % plot(c1(DualMatches(:,1)),r1(DualMatches(:,1)),'c+'); |
---|
179 | plot(correlations(:,4),correlations(:,3),'g+'); |
---|
180 | |
---|
181 | %plot(epipole(2),epipole(1),'r*'); |
---|
182 | for n = 1:1:size(correlations,1) |
---|
183 | line([correlations(n,2) correlations(n,4)], [correlations(n,1) correlations(n,3)],'color',[0 0 1]) |
---|
184 | end |
---|
185 | n |
---|