# Zed2 localization (enable area map) with MATLAB

**URL:** <https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145>\
**Category:** Uncategorized\
**Created:** [May 14, 2022, 12:54am UTC](https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145 "2022-05-14T00:54:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bao](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bao](https://community.stereolabs.com/u/bao)\
**Post date:** [May 14, 2022, 12:54am UTC](https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145/1 "2022-05-14T00:54:03Z")

</div>

i have positioned the zed2 2D according to the matlab instructions on the page. but i don’t know how to build a map and locate it in it using matlab. Hope that someone can help me, thank you very much!

---

<div class="post-metadata">

**Author:** ![SL-PY](https://community.stereolabs.com/user_avatar/community.stereolabs.com/sl-py/32/3_2.png) [@SL-PY](https://community.stereolabs.com/u/SL-PY)\
**Post date:** [May 20, 2022, 1:19pm UTC](https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145/2 "2022-05-20T13:19:47Z")

</div>

Hi,

I guess you want to save an area map and then reload it.  
Here is a sample to do so:

```auto
clc;
disp('========= ZED SDK PLUGIN =========');
disp('-- Get ZED Camera Position --');
close all;
clear mex; clear functions; clear all;

% initial parameter structure, the same as sl::InitParameters
% values as enum number, defines in : sl/defines.hpp
% or from https://www.stereolabs.com/docs/api/structsl_1_1InitParameters.html

InitParameters.svo_input_filename = 'D:/SVO/castle/full.svo'; % Enable SVO playback
result = mexZED('open', InitParameters);

if(strcmp(result,'SUCCESS'))        
    %enable Tracking
    PositionalTrackingParameters.enable_spatial_memory = 1;
    mexZED('enablePositionalTracking', PositionalTrackingParameters);
    frame = 0;
    reloc_frame = 200;
    
    while (frame < (reloc_frame + 20))
        % grab the current image and compute the positional tracking
        result = mexZED('grab');
        if(strcmp(result,'SUCCESS'))
            
            % retrieve camera Path
            [position, state] = mexZED('getPosition');            
            
            frame = frame+1;
            
            if(frame >= reloc_frame) 
                % display Camera Position around the requested reloc
                position
            end
        end
    end
        
    % disable Tracking and export area file
    mexZED('disablePositionalTracking' , 'MyAreaMap.area');
    % get back to a previous frame
    frame = reloc_frame;
    mexZED('setSVOPosition', frame);
    mexZED('grab');
    % re enable Tracking and load the saved Area Map
    PositionalTrackingParameters.enable_spatial_memory = 1;
    PositionalTrackingParameters.area_file_path = 'MyAreaMap.area';
    mexZED('enablePositionalTracking', PositionalTrackingParameters);
        
    while (frame < (reloc_frame + 20))
        % grab the current image and compute the positional tracking
        result = mexZED('grab');
        if(strcmp(result,'SUCCESS'))
            
            % retrieve camera Path
            [position, state] = mexZED('getPosition');
            
            % check the state of the Tracking to know when it is relocated
            state
             if(strcmp(state,'OK'))
                 % print the position, should be similar to the saved one
                 position
             end
            frame = frame+1;            
        end
    end
end

% Make sure to call this function to free the memory before use this again
mexZED('close')
disp('========= END =========');
clear mex;

```

I have also updated [the mex file](https://github.com/stereolabs/zed-matlab/commit/695e916c283c43799d66944cc3b287c11bdbb95a) to export the Positional Tracking state to check if the camera is well relocated in the given area file.

---

<div class="post-metadata">

**Author:** ![bao](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bao](https://community.stereolabs.com/u/bao)\
**Post date:** [May 23, 2022, 10:47am UTC](https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145/3 "2022-05-23T10:47:27Z")

</div>

Thanks you.  
And so, this is my purpose:

1. Tracking position (stream) and then save an area map, after I press ESC. (
2. Next, I turn on again tracking in this area and get its position.  
But I can’t get my area file, this is an error.  
Hope so can help me.

* * *

clc;  
disp(’========= ZED SDK PLUGIN =========’);  
disp(’-- Get ZED Camera Position --’);  
close all;  
clear mex; clear functions; clear all;

InitParameters.camera\_resolution = 2; %HD720  
InitParameters.camera\_fps = 60;  
InitParameters.coordinate\_units = 2; %METER  
InitParameters.depth\_mode = 1; %PERFORMANCE  
InitParameters.coordinate\_system = 3; %COORDINATE\_SYSTEM\_RIGHT\_HANDED\_Z\_UP

%InitParameters.svo\_input\_filename = ‘MySVO.svo’; % Enable SVO playback  
result = mexZED(‘open’, InitParameters);

if(strcmp(result,‘SUCCESS’))  
%enable Tracking  
PositionalTrackingParameters.enable\_spatial\_memory = 1;  
mexZED(‘enablePositionalTracking’, PositionalTrackingParameters);

```
% for tracking informations storage
PositionArray1 = [];
PositionArray2 = [];

% Create Figure and wait for keyboard interruption to quit
f = figure('name','ZED SDK : Positional Tracking','NumberTitle','off','keypressfcn',@(obj,evt) 0);
%create 2 sub figure
ha1 = axes('Position',[0.05,0.7,0.9,0.25]);
ha2 = axes('Position',[0.05,0.05,0.9,0.6]);
xlabel('Tx (M)');
ylabel('Tz (M)');
zlabel('Ty (M)');
xlim(ha2, [-2 2]);
ylim(ha2, [-2 2]);
axis equal, grid on;
hold on;   
% init 3d display
h = plot3(0,0,0, 'r');

key = 1;
% loop over frames, till Esc is pressed
while (key ~= 27)       
    % grab the current image and compute the positional tracking
    result = mexZED('grab');
    if(strcmp(result,'SUCCESS'))
        % retrieve letf image
        image_left = mexZED('retrieveImage', 0); %left
        %displays it
        axes(ha1);
        imshow(image_left);

        % retrieve camera Path
        
        [position, state] = mexZED('getPosition');
        
        %stack positions
        PositionArray1 = [PositionArray1; position(1,4) position(2,4) position(3,4)];

        axes(ha2);
        set(h,'XData',PositionArray1(:,1))
        set(h,'YData',PositionArray1(:,2))
        set(h,'ZData',PositionArray1(:,3))
        drawnow; %this checks for interrupts
        key = uint8(get(f,'CurrentCharacter'));
        length(key);
        if(~length(key))
            key=0;
        end
    end
end
        
% disable Tracking and export area file
mexZED('disablePositionalTracking' ,'Myymap.area');

disp('End tracking 1st');
disp('Relocalization');

% re enable Tracking and load the saved Area Map
% Loading an area file will start a search phase, during which the camera will try to position itself in the previously learned area.

PositionalTrackingParameters.enable_spatial_memory = 1;
PositionalTrackingParameters.enable_pose_smoothing = 1;

PositionalTrackingParameters.area_file_path = 'Myymap.area';

mexZED('enablePositionalTracking', PositionalTrackingParameters);
pause(3);

key = 1;
flag = 1;
while (key ~= 27)
    result = mexZED('grab');
    % grab the current image and compute the positional tracking
    if(strcmp(result,'SUCCESS'))
        
        image_left = mexZED('retrieveImage', 0); %left
        %displays it
        axes(ha1);
        imshow(image_left);
        
        % retrieve camera Path
        [position, state] = mexZED('getPosition');
        
        
        % check the state of the Tracking to know when it is relocated
        state
         if(strcmp(state,'OK'))
            % print the position, should be similar to the saved one
            PositionArray2 = [PositionArray2; position(1,4) position(2,4) position(3,4)]
            if (flag == 1)
               xo = position(1,4);
               yo = position(2,4);
               zo = position(3,4);
               h = plot3(xo,yo,zo,'g');
               flag = 0
            end
            axes(ha2);
            set(h,'XData',PositionArray2(:,1))
            set(h,'YData',PositionArray2(:,2))
            set(h,'ZData',PositionArray2(:,3))
            drawnow; %this checks for interrupts
         end
                 
    end
    key = uint8(get(f,'CurrentCharacter'));
    if(~length(key))
       key=0;
    end
end

```

end  
% Make sure to call this function to free the memory before use this again  
mexZED(‘close’)  
disp(’========= END =========’);  
clear mex;

---

<div class="post-metadata">

**Author:** ![bao](https://community.stereolabs.com/letter_avatar_proxy/v4/letter/b/eb9ed0/32.png) [@bao](https://community.stereolabs.com/u/bao)\
**Post date:** [May 24, 2022, 5:23am UTC](https://community.stereolabs.com/t/zed2-localization-enable-area-map-with-matlab/1145/4 "2022-05-24T05:23:43Z")

</div>

In this code, I can’t find “Myymap.area” file.  
:(((
