76 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Matlab
		
	
	
		
		
			
		
	
	
			76 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Matlab
		
	
	
|  | function [noiseModels,isam,result,nextPoseIndex] = VisualISAMInitialize(data,truth,options)
 | ||
|  | % VisualInitialize: initialize visualSLAM::iSAM object and noise parameters | ||
|  | % Authors: Duy Nguyen Ta, Frank Dellaert and Alex Cunningham | ||
|  | 
 | ||
|  | %% Initialize iSAM | ||
|  | import gtsam.* | ||
|  | params = gtsam.ISAM2Params; | ||
|  | if options.alwaysRelinearize | ||
|  |     params.setRelinearizeSkip(1); | ||
|  | end | ||
|  | isam = ISAM2; | ||
|  | 
 | ||
|  | %% Set Noise parameters | ||
|  | import gtsam.* | ||
|  | noiseModels.pose = noiseModel.Diagonal.Sigmas([0.001 0.001 0.001 0.1 0.1 0.1]'); | ||
|  | %noiseModels.odometry = noiseModel.Diagonal.Sigmas([0.001 0.001 0.001 0.1 0.1 0.1]'); | ||
|  | noiseModels.odometry = noiseModel.Diagonal.Sigmas([0.05 0.05 0.05 0.2 0.2 0.2]'); | ||
|  | noiseModels.point = noiseModel.Isotropic.Sigma(3, 0.1); | ||
|  | noiseModels.measurement = noiseModel.Isotropic.Sigma(2, 1.0); | ||
|  | 
 | ||
|  | %% Add constraints/priors | ||
|  | % TODO: should not be from ground truth! | ||
|  | import gtsam.* | ||
|  | newFactors = NonlinearFactorGraph; | ||
|  | initialEstimates = Values; | ||
|  | for i=1:2 | ||
|  |     ii = symbol('x',i); | ||
|  |     if i==1 | ||
|  |         if options.hardConstraint % add hard constraint | ||
|  |             newFactors.add(NonlinearEqualityPose3(ii,truth.cameras{1}.pose)); | ||
|  |         else | ||
|  |             newFactors.add(PriorFactorPose3(ii,truth.cameras{i}.pose, noiseModels.pose)); | ||
|  |         end | ||
|  |     end | ||
|  |     initialEstimates.insert(ii,truth.cameras{i}.pose); | ||
|  | end | ||
|  | 
 | ||
|  | nextPoseIndex = 3; | ||
|  | 
 | ||
|  | %% Add visual measurement factors from two first poses and initialize observed landmarks | ||
|  | import gtsam.* | ||
|  | for i=1:2 | ||
|  |     ii = symbol('x',i); | ||
|  |     for k=1:length(data.Z{i}) | ||
|  |         j = data.J{i}{k}; | ||
|  |         jj = symbol('l',data.J{i}{k}); | ||
|  |         newFactors.add(GenericProjectionFactorCal3_S2(data.Z{i}{k}, noiseModels.measurement, ii, jj, data.K)); | ||
|  |         % TODO: initial estimates should not be from ground truth! | ||
|  |         if ~initialEstimates.exists(jj) | ||
|  |             initialEstimates.insert(jj, truth.points{j}); | ||
|  |         end | ||
|  |         if options.pointPriors % add point priors | ||
|  |             newFactors.add(priorFactorPoint3(jj, truth.points{j}, noiseModels.point)); | ||
|  |         end | ||
|  |     end | ||
|  | end | ||
|  | 
 | ||
|  | %% Add odometry between frames 1 and 2 | ||
|  | import gtsam.* | ||
|  | newFactors.add(BetweenFactorPose3(symbol('x',1), symbol('x',2), data.odometry{1}, noiseModels.odometry)); | ||
|  | 
 | ||
|  | %% Update ISAM | ||
|  | import gtsam.* | ||
|  | if options.batchInitialization % Do a full optimize for first two poses | ||
|  |     batchOptimizer = LevenbergMarquardtOptimizer(newFactors, initialEstimates); | ||
|  |     fullyOptimized = batchOptimizer.optimize(); | ||
|  |     isam.update(newFactors, fullyOptimized); | ||
|  | else | ||
|  |     isam.update(newFactors, initialEstimates); | ||
|  | end | ||
|  | % figure(1);tic; | ||
|  | % t=toc; plot(frame_i,t,'r.'); tic | ||
|  | result = isam.calculateEstimate(); | ||
|  | % t=toc; plot(frame_i,t,'g.'); | ||
|  | 
 |