source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/3dRecon/grappleFmatrix.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 5.8 KB
Line 
1%% File: grappleFmatrix
2%% A4 2003 handout code
3%% Uses RANSAC to estimate F matrix from corresponding points.
4%%
5%% ADJ, Nov. 03
6
7clear
8close all
9FALSE = 1 == 0;
10TRUE = ~FALSE;
11global matlabVisRoot
12
13%% We need to ensure the path is set for the iseToolbox.
14if length(matlabVisRoot)==0
15  dir = pwd;
16  cd /h/51/jepson/pub/matlab   %% CHANGE THIS
17  startup;
18  cd(dir);
19end
20
21reconRoot = [matlabVisRoot '/utvisToolbox/tutorials/3dRecon'];
22addpath([reconRoot '/data/wadham']);
23addpath([reconRoot '/utils']);
24
25
26% Random number generator seed:
27seed = round(sum(1000*clock));
28rand('state', seed);
29seed0 = seed;
30% We also need to start randn. Use a seedn generated from seed:
31seedn = round(rand(1,1) * 1.0e+6);
32randn('state', seedn);
33
34nTrial = 10;  % Number of ransac trials to try.
35
36%% Wadham left image: use  wadham/001.jpg
37imPath = 'data/wadham/'; fnameLeft = '001';
38im = imread([imPath fnameLeft],'jpg');
39im = double(im(:,:,2));
40imDwn = blurDn(im,1)/2;
41imLeft = imDwn;
42
43%% Read correspondence data
44load data/wadham/corrPnts5
45%% Wadham right image data/wadham002-5.jpg use for corrPnts2-5 respectively
46fnameRight = '005';
47im = imread([imPath fnameRight],'jpg');
48im = double(im(:,:,2));
49imDwn = blurDn(im,1)/2;
50imRight = imDwn;
51
52clear imPts;
53imPts = cat(3, im_pos1', im_pos2');
54nPts = size(imPts,2);
55if size(imPts,1) == 2
56  imPts = cat(1, imPts, ones(1,nPts,2));
57end
58
59%% RANSAC for F
60seeds = {};
61sigma = 2.0; rho = 2;
62for kTrial = 1: nTrial
63  %% Test out F matrix on a random sample of 8 points
64  idTest = randperm(nPts);
65  nTest = min(8, nPts);
66  idTest = idTest(1:nTest);
67
68  %% Solve for F matrix on the random sample
69  [F Sa Sf] = linEstF(imPts(:,idTest,1), imPts(:,idTest,2),1);
70 
71  %% Compute perpendicular error of all points to epipolar lines
72  perpErrL = zeros(1,nPts);
73  for k = 1:nPts
74    lk = imPts(:,k,2)' * F';
75    perpErrL(k) = (lk* imPts(:,k,1))/norm(lk(1:2));
76  end
77 
78  %% Detect inliers
79  idInlier = abs(perpErrL) < rho*sigma;
80 
81  %% Count inliers
82  nInlier = sum(idInlier);
83  if nInlier > 20
84    %% Store sets of sampled points with at least 20 inliers
85    seed.id = idTest;
86    seed.idInlier = idInlier;
87    seed.nInlier = nInlier;
88    seed.F = F;
89   
90    kSeed = length(seeds)+1
91    seeds{kSeed} = seed;
92  end
93end
94%% Done RANSAC trials
95
96%% Extract best solution
97nInliers = zeros(1, length(seeds));
98for ks = 1:length(seeds)
99  nInliers(ks) = seeds{ks}.nInlier;
100end
101[nM ks] = max(nInliers);
102nInliers(ks)
103
104%%  Refine estimate of F using all inliers.
105F = seeds{ks}.F;
106idInlier = seeds{ks}.idInlier;
107
108idInlierOld = idInlier;
109sum(idInlier)
110%% Do at most 10 iterations attempting to entrain as many points as possible.
111for kIt = 1: 10
112  %% Fit F using all current inliers
113  [F Sa Sf] = linEstF(imPts(:,idInlier,1), imPts(:,idInlier,2),1);
114 
115  %% Compute perpendicular error to epipolar lines
116  perpErrL = zeros(1,nPts);
117  for k = 1:nPts
118    lk = imPts(:,k,2)' * F';
119    perpErrL(k) = (lk* imPts(:,k,1))/norm(lk(1:2));
120  end
121  idInlier = abs(perpErrL) < rho*sigma;
122  nInlier = sum(idInlier)
123 
124  %% If we have the same set of inliers as the previous iteration then stop.
125  if all(idInlier == idInlierOld)
126    break;
127  end
128  idInlierOld = idInlier;
129end
130 
131%%%%%%%%%%%%%%%%%%%%% Plot results
132nTest = 64;  %% Number of epipolar lines to plot
133nCol = 16;   %% Number of different colours to use.
134col = hsv(nCol);  %% Colour map.
135
136%% Random sample the lines to plot
137idLines = randperm(nPts); 
138idLines = idLines(1:nTest);
139
140%% Show left image
141SUPERIMPOSE = TRUE;
142hFig = figure(2);
143clf;
144if SUPERIMPOSE
145  image(imLeft);
146  colormap(gray(256));
147end
148resizeImageFig(hFig, size(imDwn), 1); hold on;
149set(get(hFig,'CurrentAxes'),'Ydir','reverse');
150hold on;
151% Plot all interest point locations as blue .'s
152plot(imPts(1,:,1), imPts(2,:,1), '.b');
153set(gca,'YDir', 'reverse');
154ax = axis;
155cropBox = [ax(1) ax(3) ax(2) ax(4)];
156title('Epipolar Lines in Left Image');
157for kl = 1:length(idLines)
158  % Plot interest point location corresponding to epipolar line as a "o"
159  % in the same colour as the epipolar line.
160  k = idLines(kl);
161  plot(imPts(1,k,1), imPts(2,k,1), 'o', 'Color', col(mod(k,nCol)+1,:));
162  % Plot epipolar line.
163  lk = imPts(:,k,2)' * F';
164  epk = cropLineInBox(lk(1:2), lk(3), cropBox);
165  set(line(epk(:,1), epk(:,2)), 'Color', col(mod(k,nCol)+1,:));
166end
167
168
169%% Show right image
170SUPERIMPOSE = TRUE;
171hFig = figure(3);
172clf;
173if SUPERIMPOSE
174  image(imRight);
175  colormap(gray(256));
176end
177resizeImageFig(hFig, size(imDwn), 1); hold on;
178set(get(hFig,'CurrentAxes'),'Ydir','reverse');
179hold on;
180% Plot all interest point locations as blue .'s
181plot(imPts(1,:,2), imPts(2,:,2), '.b');
182set(gca,'YDir', 'reverse');
183ax = axis;
184cropBox = [ax(1) ax(3) ax(2) ax(4)];
185title('Epipolar Lines in Left Image');
186perpErrR = [];
187for kl = 1:length(idLines)
188  % Plot interest point location corresponding to epipolar line as a "o"
189  % in the same colour as the epipolar line.
190  k = idLines(kl);
191  plot(imPts(1,k,2), imPts(2,k,2), 'o', 'Color', col(mod(k,nCol)+1,:));
192  % Plot epipolar line.
193  lk = imPts(:,k,1)' * F;
194  epk = cropLineInBox(lk(1:2), lk(3), cropBox);
195  set(line(epk(:,1), epk(:,2)), 'Color', col(mod(k,nCol)+1,:));
196  perpErrR = [perpErrR (lk * imPts(:,k,2)/norm(lk(1:2)))];
197end
198
199%% Compute perpendicular distance to epipolar lines in left and right images.
200perpErrL = [];
201for k = 1:nPts
202  lk = imPts(:,k,2)' * F';
203  perpErrL = [perpErrL (lk * imPts(:,k,1))/norm(lk(1:2))];
204end
205perpErrR = [];
206for k = 1:nPts
207  lk = imPts(:,k,1)' * F;
208  perpErrR = [perpErrR (lk * imPts(:,k,2)/norm(lk(1:2)))];
209end
210
211%% Plot a histogram of the perpendicular distances
212err = [perpErrL perpErrR];
213err = min(err, 10);
214err = max(err, -10);
215[n b] = histo(err, 64);
216figure(4); clf;
217plot(b,n);
218title('Distance to epipolar line');
219xlabel('Error in pixels');
220ylabel('Frequency');
221
222%% Count inliers
223idL = abs(perpErrL)< rho*sigma;
224idR = abs(perpErrR) < rho*sigma;
225idInlier = idL & idR;
226sum(idInlier)
227sum(idInlier)/nPts
228
229%% save 'Fcorr' F Sa Sf idInlier nInliers
Note: See TracBrowser for help on using the repository browser.