Main Content

hasNewKeyFrame

Check if new key frame added in visual SLAM object

Since R2023b

    Description

    hasAdded = hasNewKeyFrame(vslam) returns a logical value that indicates if a new key frame has been added within the visual simultaneous localization and mapping (vSLAM) object vslam.

    example

    Examples

    collapse all

    Perform monocular visual simultaneous localization and mapping (vSLAM) using the data from the TUM RGB-D Benchmark. You can download the data to a temporary directory using a web browser or by running this code:

    baseDownloadURL = "https://cvg.cit.tum.de/rgbd/dataset/freiburg3/rgbd_dataset_freiburg3_long_office_household.tgz"; 
    dataFolder = fullfile(tempdir,"tum_rgbd_dataset",filesep); 
    options = weboptions(Timeout=Inf);
    tgzFileName = dataFolder+"fr3_office.tgz";
    folderExists = exist(dataFolder,"dir");
    
    % Create a folder in a temporary directory to save the downloaded file
    if ~folderExists  
        mkdir(dataFolder) 
        disp("Downloading fr3_office.tgz (1.38 GB). This download can take a few minutes.") 
        websave(tgzFileName,baseDownloadURL,options); 
        
        % Extract contents of the downloaded file
        disp("Extracting fr3_office.tgz (1.38 GB) ...") 
        untar(tgzFileName,dataFolder); 
    end

    Create an imageDatastore object to store all the RGB images.

    imageFolder = dataFolder+"rgbd_dataset_freiburg3_long_office_household/rgb/";
    imds = imageDatastore(imageFolder);

    Specify your camera intrinsic parameters, and use them to create a monocular visual SLAM object.

    intrinsics = cameraIntrinsics([535.4 539.2],[320.1 247.6],[480 640]);
    vslam = monovslam(intrinsics,TrackFeatureRange=[30,120]);

    Process each image frame, and visualize the camera poses and 3-D map points. Note that the monovslam object runs several algorithm parts on separate threads, which can introduce a latency in processing of an image frame added by using the addFrame function.

    for i = 1:numel(imds.Files)
        addFrame(vslam,readimage(imds,i))
    
        if hasNewKeyFrame(vslam)
            % Display 3-D map points and camera trajectory
            plot(vslam);
        end
    
        % Get current status of system
        status = checkStatus(vslam);
    end 

    Figure contains an axes object. The axes object with xlabel X, ylabel Y contains 12 objects of type line, text, patch, scatter. This object represents Camera trajectory.

    Plot intermediate results and wait until all images are processed.

    while ~isDone(vslam)
        if hasNewKeyFrame(vslam)
            plot(vslam);
        end
    end

    Figure contains an axes object. The axes object with xlabel X, ylabel Y contains 12 objects of type line, text, patch, scatter. This object represents Camera trajectory.

    After all the images are processed, you can collect the final 3-D map points and camera poses for further analysis.

    xyzPoints = mapPoints(vslam);
    [camPoses,addedFramesIdx] = poses(vslam);
    
    % Reset the system
    reset(vslam)

    Compare the estimated camera trajectory with the ground truth to evaluate the accuracy.

    % Load ground truth
    gTruthData = load("orbslamGroundTruth.mat");
    gTruth     = gTruthData.gTruth;
    
    % Evaluate tracking accuracy
    mtrics = compareTrajectories(camPoses, gTruth(addedFramesIdx), AlignmentType="similarity");
    disp(['Absolute RMSE for key frame location (m): ', num2str(mtrics.AbsoluteRMSE(2))]);
    Absolute RMSE for key frame location (m): 0.093645
    
    % Plot the absolute translation error at each key frame
    figure
    ax = plot(mtrics, "absolute-translation");
    view(ax, [2.70 -49.20]); 

    Figure contains an axes object. The axes object with title Absolute Translation Error, xlabel X, ylabel Y contains 2 objects of type patch, line. These objects represent Estimated Trajectory, Ground Truth Trajectory.

    Input Arguments

    collapse all

    Visual SLAM object, specified as a monovslam object.

    Output Arguments

    collapse all

    New key frame status, returned as 0 (false) or 1 (true). A value of true indicates that a new key frame has been added within the visual SLAM object vslam. A value of false indicates that no new key frame has been added. The object considers an image frame to be a key frame if the number of tracked feature points in the current frame satisfies one of these conditions:

    • The number of tracked feature points is within the limits specified by the TrackFeatureRange property of the monovslam object, vslam.

    • The number of tracked feature points is greater than the upper limit specified by the TrackFeatureRange property of the monovslam object, vslam, and the number of skipped frames since adding the last key frame equals the value of the SkipMaxFrames.

    The hasNewKeyFrame object function returns true once per new key frame added. For example, if the monovslam object adds only one key frame, then two consecutive calls to hasNewKeyFrame return true and false, respectively.

    Version History

    Introduced in R2023b

    See Also

    Objects

    Functions