File: ApriltagsDemo.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (303 lines) | stat: -rw-r--r-- 11,286 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function ApriltagsDemo(camId, colordepth, tagSize, tagFamily, targetMarkers)
% ApriltagsDemo([camId][, colordepth=1][, tagSize=0.017][, tagFamily='tag16h5'][, targetMarkers])
%
% Use ApriltagsDemo to track April tag markers and visualize them in live-video.
%
% Minimalistic demo on how to capture video data and use PsychCV's builtin
% apriltag marker tracking to detect and track the rigid position and orientation
% of such fiducial markers, then visualize corresponding 3D objects on top of the
% live video of the markers via OpenGL.
%
% Note that this demo can only act as a starting point. There are too many parameters
% which need to be set for your specific use case, and we can't pass all of them via
% function parameters. Read the code, think about it, hack it accordingly.
%
% Optional parameters:
%
% camId = Id of the camera to use, [] selects default cam.
% colordepth = Video capture mode: 1 = Mono/Gray, 3 = RGB color, 4 = RGBA color.
% tagSize = Size of April tag (edge length) in meters. Needed for 3D pose estimation.
% tagFamily = April tag family to use.
% targetMarkers = List of marker id's to track and return. Default is [] for "all markers".
%
%
% Press any key to finish the demo.
%

% History:
% 15.03.2024  mk  Derived from ARToolkitDemo.

% Setup defaults and normalized color range:
PsychDefaultSetup(2);

if ~IsLinux
    fprintf('Sorry, this demo is currently only supported on Linux.\n');
    return;
end

if nargin < 1 || isempty(camId)
    % Use default camera, unless if running on machine "touchy", then use cam 2:
    camId = [];
    computer = Screen('Computer');
    if strcmp(computer.machineName, 'touchy')
        camId = 2;
    end
end

% Use 1 channel mono/grayscale video for highest efficiency by default:
if nargin < 2 || isempty(colordepth)
    colordepth = 1;
end

if nargin < 3 || isempty(tagSize)
    tagSize = 0.017; % April tag size in meters. Default to 1.7 cm.
end

% Need to have some default tagFamily:
if nargin < 4 || isempty(tagFamily)
    tagFamily = 'tag16h5';
end

% Try to detect all markers by default:
if nargin < 5 || isempty(targetMarkers)
    targetMarkers = [];
end

try
    % No sync tests for this simple demo:
    olsync = Screen('Preference', 'SkipSyncTests', 2);

    % Setup OpenGL for rendering:
    InitializeMatlabOpenGL;

    % Open onscreen window for diplay:
    screenid = max(Screen('Screens'));

    PsychImaging('PrepareConfiguration');
    % Flip image horizontally to avoid confusing the viewer due to mirror view:
    % PsychImaging('AddTask', 'AllViews', 'FlipHorizontal');
    [win, winrect] = PsychImaging('OpenWindow', screenid, 0);

    % Open videocapture device camId, at default resolution, return 1 channel grayscale images:
    grabber = Screen('OpenVideoCapture', win, camId, [], colordepth);

    % Start video capture at maximum possible framerate:
    Screen('StartVideoCapture', grabber, realmax, 1);

    % Get a single texture that we can query for the image format:
    vidtex = Screen('GetCapturedImage', win, grabber);
    [w, h] = Screen('WindowSize', vidtex)
    channels = Screen('PixelSize', vidtex) / 8

    % Compute scaling factor for upscaling all content to fill window:
    [winw, winh] = Screen('WindowSize', win);
    winscale = [winw / w, winh / h];

    % Debug level for tracker:
    PsychCV('Verbosity', 3);

    % Initialize Apriltags:

    % Camera intrinsics. Should come from calibration, but here we just make stuff up:
    cx = w / 2;
    cy = h / 2;
    fx = w * 0.555;
    fy = w * 0.555;
    camIntrinsics = [ cx, cy, fx, fy ];

    % View frustum for 3D rendering near and far distance, 0.01 - 5 meters:
    zmin = 0.01;
    zmax = 5;

    % Setup parameters for 3D marker 6-DoF pose reconstruction, and retrieve the
    % GL_PROJECTION_MATRIX to use for rendering tags superimposed onto the video:
    projectionMatrix = PsychCV('April3DSettings', camIntrinsics, tagSize, zmin, zmax);

    % Assign April tag family, input image size, and properties for raw video input
    % images, get the imgbuffer memory buffer handle where the Psychtoolbox video
    % capture engine should store camera images as input for marker detection:
    imgbuffer = PsychCV('AprilInitialize', tagFamily, w, h, channels);

    % Request multi-threaded operation with 8 threads, instead of the default 1 thread single-threaded:
    PsychCV('AprilSettings', 8);

    % Tunable parameters for 2D tag marker detection:
    % [nrThreads, imageDecimation, quadSigma, refineEdges, decodeSharpening] = PsychCV('AprilSettings', 1, 2, 0.5, 1, 1)

    % Some markers to visualize:
    marker = [9, 11, 23];

    % Size of rendered objects in meters:
    sizeM = 0.01;

    % Setup OpenGL 3D rendering for our simple test:
    Screen('BeginOpenGL', win);

    % Set projection matrix to the one provided by apriltags:
    glMatrixMode(GL.PROJECTION);
    glLoadMatrixd(projectionMatrix);

    % Init modelview matrix to identity: Will be overwritten on each
    % object draw operation by the modelview matrix from apriltag:
    glMatrixMode(GL.MODELVIEW);
    glLoadIdentity;

    % Position the light source:
    glEnable(GL.LIGHTING);
    glEnable(GL.LIGHT0);
    glLightfv(GL.LIGHT0, GL.POSITION, [ 0, 0, 1, 0 ]);

    % Change the color - or better: The light reflection properties of the
    % material - of the following objects to greenish. We only change ambient and
    % diffuse reflection properties. The color for specular reflection is left
    % to its default of "white":
    glMaterialfv(GL.FRONT_AND_BACK,GL.AMBIENT, [ 0.0 0.6 0.0 1 ]);
    glMaterialfv(GL.FRONT_AND_BACK,GL.DIFFUSE, [ 0.0 0.6 0.0 1 ]);

    % Set proper viewport:
    glViewport(0, 0, w * winscale(1), h * winscale(2));

    % Define background clearcolor:
    glClearColor(0,0,0,0);

    % Enable depth test for depth correct 3D rendering:
    glEnable(GL.DEPTH_TEST);

    % OpenGL 3D setup done:
    Screen('EndOpenGL', win);

    % Init stats:
    count = 0;
    tstart = GetSecs;
    KbReleaseWait;

    % Run tracking and viz loop until keypress:
    while ~KbCheck
        % Retrieve next captured image in vidtex: The 'specialMode'= 4 flag
        % requests video data to be put into the PsychCV() memory buffer
        % referenced by 'imgbuffer':
        vidtex = Screen('GetCapturedImage', win, grabber, 1, vidtex, 4, imgbuffer);

        % Draw captured video image as backdrop:
        Screen('DrawTexture', win, vidtex, [], ScaleRect(Screen('Rect', vidtex), winscale(1), winscale(2)));

        % Clear depth buffer before rendering 3D objects for this frame:
        Screen('BeginOpenGL', win);
        glClear(GL.DEPTH_BUFFER_BIT);
        Screen('EndOpenGL', win);

        %fprintf('\n=================================\n');

        % Detect apriltags in captured image:
        detectedMarkers = PsychCV('AprilDetectMarkers', targetMarkers);

        % For each candidate marker do...
        j = 0;
        for i = 1:length(detectedMarkers)
            % i'th candidate detected reliably?
            score = detectedMarkers(i).MatchQuality;
            hamming = detectedMarkers(i).HammingErrorBits;

            if score > 100 && hamming <= 2
                % Yes. Get info about it:
                j = j + 1;
                id = detectedMarkers(i).Id;

                % Visualize (x,y) 2D image position of marker center with a green dot:
                xy = detectedMarkers(i).Center2D;
                Screen('DrawDots', win, xy .* winscale', 10, [0 1 0], [], 1);

                % Visualize (x,y) 2D image position of marker corners as yellow dots,
                % and boundaries as red lines:
                xyv = detectedMarkers(i).Corners2D;
                Screen('FramePoly', win, [1 0 0], xyv' .* winscale, 2);
                Screen('DrawDots', win, xyv .* winscale', 10, [1 1 0], [], 1);

                % 3D pose and pose reconstruction error:
                poseError = detectedMarkers(i).PoseError;

                % Pose as OpenGL GL_MODELVIEW_MATRIX compatible 4x4 matrix:
                OpenGLPoseMatrix = detectedMarkers(i).ModelViewMatrix;

                % 3D position of the markers center in apriltag natural reference frame.
                % Extract mpos as the 3 component translation vector from the pose matrix:
                mpos = detectedMarkers(i).TransformMatrix(1:3, 4);
                %fprintf('Id: %i  Hamming %i  Score %f  PosError [mm]: %f\n', id, hamming, score, poseError * 1e3);

                % Draw some text next to each markers center, with marker id and estimated 'mpos'
                % 3D center coordinates in meters, relative to the optical center of the camera,
                % with x-axis to the right, y-axis down, and z-axis facing away from the camera,
                % ie. along its optical axis, ie. in the viewing direction:
                statusTxt = sprintf('%i: %04f : %04f : %04f', id, mpos(1), mpos(2), mpos(3));
                Screen('DrawText', win, statusTxt, xy(1) * winscale(1) + 10, xy(2) * winscale(2), [1 0 1], [], 0);

                % Visualize detected marker in 3D as well, via OpenGL 3D rendering:
                Screen('BeginOpenGL', win);

                % Setup rigid position and orientation of marker via the OpenGLPoseMatrix:
                glLoadMatrixd(OpenGLPoseMatrix);

                % Visualize location and orientation of tag, by drawing some reference frame on it:
                % Tag in x-y plane, z-axis comes out of tag like a surface normal vector.
                % Draw little reference frame in 3D, ie. x/y/z axis as red, green, blue lines:
                glDisable(GL.LIGHTING);

                glBegin(GL.LINES);
                glColor3d(1, 0, 0);
                glVertex3d(0, 0, 0);
                glVertex3d(sizeM, 0, 0);

                glColor3d(0, 1, 0);
                glVertex3d(0, 0, 0);
                glVertex3d(0, sizeM, 0);

                glColor3d(0, 0, 1);
                glVertex3d(0, 0, 0);
                glVertex3d(0, 0, sizeM);
                glEnd;

                % For the fun of it, also draw some 3D objects "glued" onto the
                % tags denoted by the vector 'marker':
                glEnable(GL.LIGHTING);

                % Switch drawn 3D objects by markerId:
                glTranslatef(0, 0, sizeM/2)
                switch j
                    case 1,
                        glutSolidTeapot(sizeM);
                    case 2,
                        glutSolidCube(sizeM);
                    case 3,
                        glutSolidSphere(sizeM, 50, 50);
                end

                % Done with this markers 3D drawing:
                Screen('EndOpenGL', win);
            end
        end

        % Show updated image at next retrace:
        Screen('Flip', win);

        % Next tracking loop iteration...
        count = count + 1;
    end

    % Stats...
    telapsed = GetSecs - tstart;
    fprintf('Average fps: %f\n', count / telapsed);

    % Stop capture, close engine and onscreen window:
    Screen('StopVideoCapture', grabber);
    Screen('CloseVideoCapture', grabber);
    sca;

    % Shutdown tracker:
    PsychCV('AprilShutdown');
catch
   sca;
   PsychCV('AprilShutdown');
end

% Restore sync settings:
Screen('Preference', 'SkipSyncTests', olsync);