| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | function pts2dTracksMono = points2DTrackMonocular(K, cameraPoses, imageSize, cylinders)
 | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | % Assess how accurately we can reconstruct points from a particular monocular camera setup.  | 
					
						
							|  |  |  | % After creation of the factor graph for each track, linearize it around ground truth.  | 
					
						
							|  |  |  | % There is no optimization | 
					
						
							|  |  |  | % @author: Zhaoyang Lv | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import gtsam.* | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | %% create graph | 
					
						
							|  |  |  | graph = NonlinearFactorGraph; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | %% create the noise factors | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  | pointNoiseSigma = 0.1; | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | poseNoiseSigmas = [0.001 0.001 0.001 0.1 0.1 0.1]'; | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | measurementNoiseSigma = 1.0; | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | posePriorNoise  = noiseModel.Diagonal.Sigmas(poseNoiseSigmas); | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | pointPriorNoise = noiseModel.Isotropic.Sigma(3, pointNoiseSigma); | 
					
						
							|  |  |  | measurementNoise = noiseModel.Isotropic.Sigma(2, measurementNoiseSigma); | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | cameraPosesNum = length(cameraPoses); | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% add measurements and initial camera & points values | 
					
						
							|  |  |  | pointsNum = 0; | 
					
						
							|  |  |  | cylinderNum = length(cylinders); | 
					
						
							|  |  |  | for i = 1:cylinderNum | 
					
						
							|  |  |  |     pointsNum = pointsNum + length(cylinders{i}.Points); | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | pts3d = cell(cameraPosesNum, 1); | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | initialEstimate = Values; | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | initialized = false; | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | for i = 1:cameraPosesNum | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |     % add a constraint on the starting pose     | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |     cameraPose = cameraPoses{i}; | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |     pts3d{i} = cylinderSampleProjection(K, cameraPose, imageSize, cylinders); | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  |     | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |     if ~initialized | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |         graph.add(PriorFactorPose3(symbol('x', 1), cameraPose, posePriorNoise)); | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |         k = 0; | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |         if ~isempty(pts3d{i}.data{1+k}) | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |             graph.add(PriorFactorPoint3(symbol('p', 1), ... | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |                 pts3d{i}.data{1+k}, pointPriorNoise)); | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |         else | 
					
						
							|  |  |  |             k = k+1; | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         initialized = true; | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |     for j = 1:length(pts3d{i}.Z) | 
					
						
							|  |  |  |         if isempty(pts3d{i}.Z{j}) | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |             continue; | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |         graph.add(GenericProjectionFactorCal3_S2(pts3d{i}.Z{j}, ... | 
					
						
							|  |  |  |             measurementNoise, symbol('x', i), symbol('p', j), K) );     | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | %% initialize cameras and points close to ground truth  | 
					
						
							|  |  |  | for i = 1:cameraPosesNum | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |     pose_i = cameraPoses{i}.retract(0.1*randn(6,1)); | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  |     initialEstimate.insert(symbol('x', i), pose_i);     | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | ptsIdx = 0; | 
					
						
							|  |  |  | for i = 1:length(cylinders) | 
					
						
							|  |  |  |     for j = 1:length(cylinders{i}.Points) | 
					
						
							|  |  |  |         ptsIdx = ptsIdx + 1; | 
					
						
							|  |  |  |         point_j = cylinders{i}.Points{j}.retract(0.1*randn(3,1)); | 
					
						
							|  |  |  |         initialEstimate.insert(symbol('p', ptsIdx), point_j); | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-13 05:10:49 +08:00
										 |  |  | %% Print the graph | 
					
						
							|  |  |  | graph.print(sprintf('\nFactor graph:\n')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | %marginals = Marginals(graph, initialEstimate); | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  | %% get all the points track information | 
					
						
							| 
									
										
										
										
											2015-01-13 12:27:06 +08:00
										 |  |  | % currently throws the Indeterminant linear system exception | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | ptx = 1; | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  | for k = 1:cameraPosesNum | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for i = 1:length(cylinders) | 
					
						
							|  |  |  |         for j = 1:length(cylinders{i}.Points) | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |             if isempty(pts3d{k}.index{i}{j}) | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  |                 continue; | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  |             idx = pts3d{k}.index{i}{j}; | 
					
						
							|  |  |  |             pts2dTracksMono.pt3d{ptx} = pts3d{k}.data{idx}; | 
					
						
							|  |  |  |             pts2dTracksMono.Z{ptx} = pts3d{k}.Z{idx}; | 
					
						
							|  |  |  |             %pts2dTracksMono.cov{ptx} = marginals.marginalCovariance(symbol('p',idx)); | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  |             ptx = ptx + 1; | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  | %% plot the result with covariance ellipses | 
					
						
							|  |  |  | hold on; | 
					
						
							| 
									
										
										
										
											2015-01-14 05:33:47 +08:00
										 |  |  | %plot3DPoints(initialEstimate, [], marginals); | 
					
						
							|  |  |  | %plot3DTrajectory(initialEstimate, '*', 1, 8, marginals); | 
					
						
							|  |  |  | plot3DTrajectory(initialEstimate, '*', 1, 8); | 
					
						
							|  |  |  | view(3); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 00:34:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 12:20:50 +08:00
										 |  |  | end |