feat(slam): add rtabmap_ros

This commit is contained in:
X-lanni
2025-07-14 11:34:38 +08:00
parent 3b6641c1fb
commit 943ce5b06f
1635 changed files with 603092 additions and 0 deletions
@@ -0,0 +1,128 @@
function [ PR ] = getPrecisionRecall( LogI, LogF, GT_file, LoopThr )
%GETPRECISIONRECALL Calculate the precision-recall results from the log
%files of RTAB-Map and a Ground Truth file (a bmp).
% PR(:,1) = Precision
% PR(:,2) = Recall
% PR(:,3) = Precision with verification
% PR(:,4) = Recall with verification
%
% LogI: The 'LogI.txt' generated file
% LogF: The 'LogF.txt' generated file
% GT_file: The related Ground truth file of the dataset ('GT.bmp')
% LoopThr: Display false positives over the loop thr (>=0.0 && < 1.0)
GroundTruth = [];
if exist(GT_file, 'file')
display('--- getPrecisionRecall ---');
display(['Loading GroundTruth ''' GT_file ''' ...']);
GroundTruth = imread(GT_file);
if max(max(GroundTruth)) == 1
GroundTruth=GroundTruth*255;
end
else
error(['The ground truth ''' GT_file '''doesn''t exist.'])
end
if ~isempty(GroundTruth)
%display('Calculating Precision-Recall graph')
%figure
%imshow(GroundTruth)
%title('GroundTruth')
if size(GroundTruth, 1) ~= length(LogF(:,1)) || size(GroundTruth, 1) ~= length(LogI(:,1))
error(['The ground truth size doesn''t match the log files (LogI=' num2str(length(LogI(:,1))) ', LogF=' num2str(length(LogF(:,1))) ', GT=' num2str(size(GroundTruth, 1)) ')'])
end
%[highestHypot, CorrespondingID, GT, Accepted, Good, Index, UnderLoopRatio] descending order
if(sum(LogI(:,8) == 10) > 0)
%OLD
warning('Detected old Log format...');
lc = [LogF(:,10) LogI(:,2) sum(GroundTruth == 255, 2)>0 (LogI(:, 8) == 10 | LogI(:, 8) == 11) zeros(length(LogI(:,1)),1) (1:length(LogF(:,10)))' LogI(:, 8) == 3];
else
%NEW
lc = [LogF(:,10) LogI(:,2) sum(GroundTruth == 255, 2)>0 LogI(:, 1) > 0 zeros(length(LogI(:,1)),1) (1:length(LogF(:,10)))' LogI(:, 8) == 1];
end
%eliminate loops on diagonal
ignored = 0;
for i=1:length(lc)
index = find(GroundTruth(:,i) > 0 & GroundTruth(:,i) < 255);
if ~isempty(index)
row = GroundTruth(index(1), :);
if lc(i,2) >= min(index) && lc(i,2) <= max(index)
%display(['i=' NUM2STR(i) ' loop=' NUM2STR(LogI(i,2)) ' min(index)=' NUM2STR(min(index)) ' max(index)' NUM2STR(max(index))])
lc(i,1) = 0;
ignored = ignored + 1;
end
end
end
lc = sortrows(lc, -1);
GT_total_positives = sum(sum(GroundTruth == 255, 2) > 0)
if GT_total_positives == 0
error(['The ground truth ''' GT_file '''doesn''t have any white pixels!?'])
end
%figure
%plot(sum(GroundTruth > 0, 2)>0)
%title('Ground truth (timeline)')
sizeNonZero = sum(lc(:,1) > 0);
PR = zeros(sizeNonZero, 4);
for i=1:length(lc)
if lc(i,1) == 0
break;
end
id = lc(i,2);
if id && sum(GroundTruth(lc(i,6), id)) > 0
lc(i,5) = 1;
end
%Recall = Loop closures detected / GT loop closures
PR(i,2) = sum(lc(1:i,5) & ~lc(1:i,7) & lc(1:i,2)) / GT_total_positives;
%Precision = Good loop closures / total loop closure detected
PR(i,1) = sum(lc(1:i,5) & ~lc(1:i,7) & lc(1:i,2)) / sum(~lc(1:i, 7) & lc(1:i,2));
if ~lc(i,5) && ~lc(i,7) && id && lc(i,1) >= LoopThr
display(['False positive! id=' num2str(lc(i,6)) ' with old=' num2str(id) ' (p=' num2str(lc(i,1)) ')'] )
end
%Recall = Loop closures detected / GT loop closures
PR(i,4) = sum(lc(i,4) & lc(1:i,5) & ~lc(1:i,7) & lc(1:i,2)) / GT_total_positives;
%Precision = Good loop closures / total loop closure detected
if sum(~lc(1:i, 7) & lc(1:i,2) & lc(i,4)) > 0
PR(i,3) = sum(lc(i,4) & lc(1:i,5) & ~lc(1:i,7) & lc(1:i,2)) / sum(~lc(1:i, 7) & lc(1:i,2) & lc(i,4));
else
PR(i,3) = 0;
end
if lc(i,4) && ~lc(i,5) && ~lc(i,7) && id && lc(i,1) >= LoopThr
display(['False positive accepted! id=' num2str(lc(i,6)) ' with old=' num2str(id) ' (p=' num2str(lc(i,1)) ')'] )
end
end
index = find(PR(:,1) == 1);
if ~isempty(index)
maxRecall = PR(index(end),2) * 100;
display(['Recall max (Precision=100%) = ' num2str(maxRecall) '% (p=' num2str(lc(index(end),1)) '), accepted=' num2str(sum(lc(1:index(end),5) & ~lc(1:index(end),7) & lc(1:index(end),2))) '/' num2str(GT_total_positives)])
else
display('Recall max (Precision=100%) = 0')
end
indexAccepted = find(PR(:,3) == 1);
if ~isempty(indexAccepted)
maxRecall = PR(indexAccepted(end),2) * 100;
display(['Recall max accepted (Precision=100%) = ' num2str(maxRecall) '% (p=' num2str(lc(indexAccepted(end),1)) '), accepted=' num2str(sum(lc(1:indexAccepted(end),5) & ~lc(1:indexAccepted(end),7) & lc(1:indexAccepted(end),2))) '/' num2str(GT_total_positives)])
else
display('Recall max accepted (Precision=100%) = 0')
end
display(['ignored = ' num2str(ignored)])
end
end
@@ -0,0 +1,25 @@
function [Data] = importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 07-Oct-2009 10:10:26
% Import the file
rawData1 = importdata(fileToRead1);
if isstruct(rawData1)
rawData1=rawData1.data;
end
% For some simple files (such as a CSV or JPEG files), IMPORTDATA might
% return a simple array. If so, generate a structure so that the output
% matches that from the Import Wizard.
[unused,name] = fileparts(fileToRead1); %#ok
newData1.(genvarname(name)) = rawData1;
vars = fieldnames(newData1);
if length(vars) > 0
Data = newData1.(vars{1});
end
@@ -0,0 +1,17 @@
fprintf(_foutFloat, "%f %f %f %f %f %f %f %f %f\n",
totalTime,
timeMemoryUpdate,
timeReactivations,
timeLikelihoodCalculation,
timePosteriorCalculation,
timeHypothesesCreation,
timeHypothesesValidation,
timeRealTimeLimitReachedProcess,
timeStatsCreation);
fprintf(_foutInt, "%d %d %d %d\n",
lcHypothesisId,
mostLikelihoodId,
signaturesRemoved,
hessianThr,
wordsNewSign,
dictionarySize);
@@ -0,0 +1,56 @@
#set(0,'defaultAxesFontName', 'Times')
#set(0,'defaultTextFontName', 'Times')
Prefix = 'loop_closure_detection_datasets';
Dataset= 'CityCentre'
Detectors = {'Surf'; 'Sift'; 'CudaSift'; 'GfttBrief'};
% The Ground Truth is a squared bmp file (size must match the log files
% length) where white dots mean loop closures.
% Grey dots mean 'loop closures to ignore', this happens when the rehearsal
% doesn't match consecutive images together.
GroundTruthFile = [Prefix '/' Dataset '.png'];
colors = 'kbgrcm';
figure
xlabel('Recall (%)')
ylabel('Precision (%)')
hold on;
Results = {};
TimeResults = {};
for i=1:length(Detectors)
LogI = importfile([Prefix '/' Dataset '/' Detectors{i} 'LogI.txt']);
LogF = importfile([Prefix '/' Dataset '/' Detectors{i} 'LogF.txt']);
PR = getPrecisionRecall(LogI, LogF, GroundTruthFile, 0.07);
plot(100*PR(:,2), 100*PR(:,1), colors(mod(i,6)+1));
% hold on;
Results{i} = PR;
time = sum(LogF(:,2:7),2)+LogF(:,17);%LogF(:,1)
TimeResults{i} = time;
meanTime = mean(time)
meanWm = mean(LogI(:,7))
meanDict = mean(LogI(:,6))
maxTime = max(time)
maxWm = max(LogI(:,7))
maxDict = max(LogI(:,6))
%figure(2)
%plot(PR(:,4), PR(:,3), colors(mod(i,6)+1));
%hold on;
end
legend(Detectors)
title(Dataset)
figure
rows=floor(length(Detectors)/2 )+ mod(length(Detectors), 2)
for i=1:length(TimeResults)
subplot(rows, 2, i)
plot(TimeResults{i})
ylabel('Time (s)')
title([Detectors{i} ' (' num2str(mean(TimeResults{i})) 's)'])
end
xlabel('Location indexes')
@@ -0,0 +1,421 @@
function [LogF LogI] = showlogs(PathPrefix, GT_file, LogPrefix)
% [LogF LogI] = showlogs(PathPrefix, GT_file)
% SHOWLOGS Plot a RTAB-Map results (LogI.txt, LogF.txt). Just put this
% file in the same directory as LogF.txt and LogI.txt files
% generated by RTAB-Map (RTAB-Map's working directory). The files
% must have the same number of lines.
%
% SHOWLOGS(PathPrefix, GT_file)
% PathPrefix (optional) is a path prefix to put before the loaded
% files (LogI.txt and LogF.txt).
% GT_file (optional) is the Ground Truth file. The Ground Truth is a
% squared bmp file (size must match the log files length) where
% white dots mean loop closures. Grey dots mean 'loop closures to
% ignore', this happens when the rehearsal doesn't match consecutive
% images together.
%
% Dependency : importfile.m
%--------------------
% Parameters
%--------------------
set(0,'defaultAxesFontName', 'Times')
set(0,'defaultTextFontName', 'Times')
close all
if nargin < 3, LogPrefix = ''; end
if nargin < 2, GT_file = ''; end
if nargin < 1, PathPrefix = '.'; end
%---------------------------------------------------------
display(' ');
display('Loading log files...');
LogF = importfile([PathPrefix '/' LogPrefix 'LogF.txt']);
% COLUMN HEADERS :
% 1 totalTime
% 2 timeMemoryUpdate,
% 3 timeReactivations,
% 4 timeLikelihoodCalculation,
% 5 timePosteriorCalculation,
% 6 timeHypothesesCreation,
% 7 timeHypothesesValidation,
% 8 timeRealTimeLimitReachedProcess,
% 9 timeStatsCreation
% 10 highestHypothesisValue
% 11 vpLikelihood
% 12 maxLikelihood
% 13 sumLikelihoods
% 14 mean likelihood
% 15 stddev likelihood
% 16 vp hypothesis
% 17 timeJoiningTrash
% 18 rehearsalValue
% 19 timeEmptyingTrash
% 20 timeRetrievalDbAccess
LogI = importfile([PathPrefix '/' LogPrefix 'LogI.txt']);
% COLUMN HEADERS :
% 1 lcHypothesisId,
% 2 highestHypothesisId,
% 3 signaturesRemoved,
% 4 hessianThr,
% 5 wordsNewSign,
% 6 dictionarySize,
% 7 this->getWorkingMem().size(),
% 8 rejectedHypothesis?,
% 9 processMemoryUsed,
% 10 databaseMemoryUsed
% 11 signaturesReactivated
% 12 lcHypothesisReactivated
% 13 refUniqueWordsCount
% 14 _reactivateId
% 15 nonNulls.size()
% 16 rehearsalMaxId
% 17 rehearsalNbMerged
if isempty(LogI) || isempty(LogF)
error('Log files are empty')
end
if size(LogI, 1) ~= size(LogF, 1)
error('Log files are not the same size')
end
% figure
% subplot(211)
% H1 = plot(LogF(:,1)*1000);
% hold on
% % H2 = plot(1:length(LogF(:,1)), ones(length(LogF(:,1)),1).*mean(LogF(:,1))*1000, 'r-')
% %title('Total process time / Location')
% ylabel('Time (ms)')
% xlabel('Location indexes')
% meanTime = mean(LogF(:,1))*1000
% %plot([1 length(LogF(:,1))], [800 800], 'r')
% %plot([1 length(LogF(:,1))], [1000 1000], 'k')
% subplot(212)
% plot(sum(LogF(:,2:7),2)*1000);
% ylabel('Time (ms)')
% xlabel('Location indexes')
figure
plot(LogF(:,1), 'g'); % to verify that we have all timings below
hold on
if size(LogF, 2) == 21
plot((sum(LogF(:,2:7),2)+LogF(:,17)+LogF(:,21)));
else
plot((sum(LogF(:,2:7),2)+LogF(:,17)+sum(LogF(:,21:26),2)));
end
ylabel('Time (s)')
xlabel('Node indexes')
meanTimeMS = mean(LogF(:,1))*1000
plot([1 length(LogF(:,1))], [0.7 0.7], 'r')
plot([1 length(LogF(:,1))], [1 1], 'k')
%plot([1 length(LogF(:,1))], [350 350], 'r')
%legend('Processing time', 'Time limit')%, 'Acquisition rate (1 Hz)')
%title('Processing time')
maxTimeMS = max(sum(LogF(:,2:7),2)+LogF(:,17))*1000
maxDict = max(LogI(:, 6))
maxWM = max(LogI(:,7))
%%
% -------------------------
% Time details
figure
subplot(8,1,1)
plot(LogF(:,2)*1000)
title('timeMemoryUpdate (ms)')
subplot(8,1,2)
plot(LogF(:,3)*1000)
title('timeReactivations (ms)')
subplot(8,1,3)
plot(LogF(:,4)*1000)
title('timeLikelihoodCalculation (ms)')
subplot(8,1,4)
plot(LogF(:,5)*1000)
title('timePosteriorCalculation (ms)')
subplot(8,1,5)
plot(LogF(:,6)*1000)
title('timeHypothesesCreation (ms)')
subplot(8,1,6)
plot(LogF(:,7)*1000)
title('timeHypothesesValidation (ms)')
subplot(8,1,7)
plot(LogF(:,8)*1000)
title('timeStatsCreation (ms)')
if size(LogF, 2) > 16
subplot(8,1,8)
plot(LogF(:,17)*1000)
title('timeJoiningTrash (ms)')
end
xlabel('Location indexes')
%% -------------------------
figure
plot([LogF(:,2) sum(LogF(:,2:3),2) sum(LogF(:,2:4),2) sum(LogF(:,2:5),2) sum(LogF(:,2:6),2) sum(LogF(:,2:7),2) sum(LogF(:,2:8),2), sum(LogF(:,2:8),2)+LogF(:,17)]);
legend('timeMemoryUpdate', 'timeReactivations', 'timeLikelihoodCalculation', 'timePosteriorCalculation', 'timeHypothesesCreation', 'timeHypothesesValidation', 'timeRealTimeLimitReachedProcess', 'timeJoiningTrash')
title('Process time details')
ylabel('s')
xlabel('Location indexes')
figure
subplot(211)
plot(LogF(:,3));
title('Reactivation time (s)')
ylabel('s')
subplot(212)
plot(LogI(:,11),'.')
ylabel('Locations reactivated')
xlabel('Location indexes')
%% -------------------------
figure
subplot(211)
plot(LogI(:, 6));
title('dictionary size')
ylabel('words')
subplot(212)
plot([LogI(:, 9) LogI(:, 10)]);
title('Memory usage (in MB)')
legend('Process', 'Database')
ylabel('MB')
xlabel('Location indexes')
% -------------------------
if size(LogI, 2) >= 18
LTMsize = zeros(1,length(LogI(:,16)));
for i=1:length(LogI(:,16))
LTMsize(i) = sum(LogI(1:i,16) == 0);
end
LTM = LTMsize(end)
figure
% subplot(211)
H2 = plot(LTMsize, 'r'); % global graph
hold on
H1 = plot(LogI(:,7)); % WM
H3 = plot(LogI(:,17), 'g'); % Local graph
% H2 = plot(1:length(LogI(:,7)), ones(length(LogI(:,7)),1).*mean(LogI(:,7)), 'r--')
%title('Graph size')
legend('Global graph', 'WM', 'Local graph')
ylabel('Nodes')
xlabel('Node indexes')
%set(H1,'color',[0.3 0.3 0.3])
%set(H2,'color',[0 0 0])
%set(H3,'color',[0 0 0])
% subplot(212)
% plot(LogI(:,6));
meanWM = mean(LogI(:,7))
meanDict = mean(LogI(:,6))
% ylabel('Dictionary size')
% xlabel('Location indexes')
else
figure
% subplot(211)
H1 = plot(LogI(:,7));
% hold on
% H2 = plot(1:length(LogI(:,7)), ones(length(LogI(:,7)),1).*mean(LogI(:,7)), 'r--')
title('Working memory size')
meanWM = mean(LogI(:,7))
ylabel('WM size (locations)')
xlabel('Location indexes')
% set(H1,'color',[0.3 0.3 0.3])
% set(H2,'color',[0 0 0])
% subplot(212)
% plot(LogI(:,6));
meanDict = mean(LogI(:,6))
% ylabel('Dictionary size')
% xlabel('Location indexes')
end
meanWordsPerSign = mean(LogI(:,5))
%% -------------------------
if size(LogF, 2) > 19
figure
subplot(211)
plot(LogF(:,20) + LogF(:,17)); %17 join or 19 empty trash
ylabel('Time (s)')
xlabel('Location indexes')
subplot(212)
plot(LogI(:,7));
ylabel('WM size (locations)')
xlabel('Location indexes')
end
%% -------------------------
% Detected/Accepted/Rejected loop closures
figure;
subplot(311)
plot(LogF(:,10), '.')
title('Highest posterior, green=accepted, red=rejected, blue=under T_{Loop}')
hold on
ylabel('p')
%rejected (by T_loop) hypotheses
y = LogF(:,10);
x = 1:length(y);
y(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
x(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
plot(x,y, 'b.')
%rejected (by ratio) hypotheses
y = LogF(:,10);
x = 1:length(y);
y(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
x(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
plot(x,y, 'r.')
%Accepted hypotheses
y = LogF(:,10);
x = 1:length(y);
y(LogI(:, 1) == 0) = [];
x(LogI(:, 1) == 0) = [];
plot(x,y, 'g.')
subplot(312)
plot(LogI(:,2), '.')
title('Id corresponding to highest posterior + lc accepted and rejected')
hold on
ylabel('Matched location index')
%rejected hypotheses
y = LogI(:,2);
x = 1:length(y);
y(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
x(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
plot(x,y, 'b.')
%rejected (by ratio) hypotheses
y = LogI(:,2);
x = 1:length(y);
y(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
x(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
plot(x,y, 'r.')
%Accepted hypotheses
y = LogI(:,2);
x = 1:length(y);
y(LogI(:, 1) == 0) = [];
x(LogI(:, 1) == 0) = [];
plot(x,y, 'g.')
subplot(313)
plot(LogI(:,5),'.')
title('wordsNewSign')
hold on
ylabel('words')
xlabel('Location indexes')
%rejected hypotheses
y = LogI(:,5);
x = 1:length(y);
y(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
x(LogI(:, 1) == 0 & LogI(:, 8) ~= 0) = [];
plot(x,y, 'b.')
%rejected (by ratio) hypotheses
y = LogI(:,5);
x = 1:length(y);
y(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
x(LogI(:, 8) == 0 | LogI(:, 1) > 0) = [];
plot(x,y, 'r.')
%Accepted hypotheses
y = LogI(:,5);
x = 1:length(y);
y(LogI(:, 1) == 0) = [];
x(LogI(:, 1) == 0) = [];
plot(x,y, 'g.')
%set(datacursormode,'UpdateFcn',@(Y,X){sprintf('X: %0.2f',X.Position(1)),sprintf('Y: %0.2f',X.Position(2))})
% %matched sign words
% y = LogI(:,2);
% x = 1:length(y);
% mask = zeros(1,length(y));
% y(LogI(:, 8) ~= 11) = [];
% for i=1:length(y)
% mask(y(i)) = 1;
% end
% y = LogI(:,5);
% y(~mask) = [];
% x(~mask) = [];
% plot(x,y, 'c.')
% %matched sign words for rejected
% y = LogI(:,2);
% x = 1:length(y);
% mask = zeros(1,length(y));
% y(LogI(:, 8) < 12) = [];
% for i=1:length(y)
% mask(y(i)) = 1;
% end
% y = LogI(:,5);
% y(~mask) = [];
% x(~mask) = [];
% plot(x,y, 'm.')
lcAccepted = sum(LogI(:, 1) > 0)
lcReactivated = sum(LogI(:, 12) == 1)
lcIgnored = sum(LogI(:, 1) == 0 & LogI(:, 8) == 0)
lcRejected = sum(LogI(:, 8) == 1)
%figure;
%plot([1.0 * (LogI(:, 8) == 10) ...
% 1.01 * (LogI(:, 8) == 11) ...
% 1.02 * (LogI(:, 8) == 14) ...
% 1.03 * (LogI(:, 8) == 15)], '.');
%title('Reject loop reason')
%legend('UNDEFINED', 'ACCEPTED', 'NOT ENOUGH MATCHING PAIRS', 'EPIPOLAR CONSTRAINT FAILED')
% -----------------
% Squared matrix
%%
%Precision-Recall graph
GroundTruthFile = [GT_file];
if ~isempty(GT_file) && exist(GroundTruthFile, 'file')
PR = getPrecisionRecall(LogI, LogF, GroundTruthFile, 0.07);
Precision = PR(:,1);
Recall = PR(:,2);
PrecisionVerified = PR(:,3);
RecallVerified = PR(:,4);
%plot the Precision-Recall
figure
plot(Recall*100, Precision*100)
%plot([Recall RecallVerified], [Precision PrecisionVerified])
%legend('Without verification', 'With verification')
%title('Precision-Recall curve')
xlabel('Recall (%)')
ylabel('Precision (%)')
else
display('Precision-recall curve is not computed...');
end
%%
% count = 0;
% for i=2:length(LogF(:,10))
% if(LogF(i,10) >= 0.03723 && LogF(i,10) < LogF(i-1,10)*0.90)
% display(['i=' num2str(i) ' with=' num2str(LogI(i,2)) ' ratio=' num2str(LogF(i,10)/LogF(i-1,10))])
% count = count +1;
% end
% end
% count
%%
% figure
% hold on
% K=100;
% %plot(1./(K*LogF(:,15)), 'r')
% %plot(log10(1./(LogF(:,15))), 'c')
% scale=1;
% %plot(log10(1./(LogF(:,15))).^2 ./ ((LogF(:,12)-LogF(:,15))./LogF(:,14)), 'k')
% %plot(log10(1./(LogF(:,15))), 'm')
% plot((LogF(:,12)-LogF(:,15))./LogF(:,14), 'g')
% plot([0, length(LogF(:,15))], [1 1], 'k:')
% plot(LogF(:,11), 'b')
% %legend(['K=' num2str(K)], 'ln', 'ln scaled', 'log10', 'max sim', '1', 'Vp likelihood')