Detect and estimate pose for AprilTag in image
returns the recognized AprilTag families,
detectedFamily] = readAprilTag(___)
Detect AprilTags in Image
Read an image into the workspace.
I = imread("aprilTagsMulti.jpg");
Specify the AprilTag formats to search in the image.
tagFamily = ["tag36h11","tagCircle21h7","tagCircle49h12","tagCustom48h12","tagStandard41h12"];
Detect tags and tag locations in the image, and read the tag IDs.
[id,loc,detectedFamily] = readAprilTag(I,tagFamily); for idx = 1:length(id) % Display the ID and tag family disp("Detected Tag ID, Family: " + id(idx) + ", " ... + detectedFamily(idx)); % Insert markers to indicate the locations markerRadius = 8; numCorners = size(loc,1); markerPosition = [loc(:,:,idx),repmat(markerRadius,numCorners,1)]; I = insertShape(I,"FilledCircle",markerPosition,Color="red",Opacity=1); end
Detected Tag ID, Family: 30, tag36h11 Detected Tag ID, Family: 32, tagCircle21h7 Detected Tag ID, Family: 98, tagStandard41h12 Detected Tag ID, Family: 195, tagCustom48h12 Detected Tag ID, Family: 884, tagCircle49h12
Display the image with markers to indicate the corner locations of the detected tags.
Estimate AprilTag Poses in Image
Read an image that contains AprilTags into the workspace. Display the image.
I = imread("aprilTag36h11.jpg"); imshow(I)
Load the camera intrinsic parameters.
data = load("camIntrinsicsAprilTag.mat"); intrinsics = data.intrinsics;
Specify the tag size in meters.
tagSize = 0.04;
Undistort the input image using the camera intrinsic parameters.
I = undistortImage(I,intrinsics,OutputView="same");
Detect a specific family of AprilTags and estimate the tag poses.
[id,loc,pose] = readAprilTag(I,"tag36h11",intrinsics,tagSize);
Set the origin for the axes vectors and for the tag frames.
worldPoints = [0 0 0; tagSize/2 0 0; 0 tagSize/2 0; 0 0 tagSize/2];
Add the tag frames and IDs to the image.
for i = 1:length(pose) % Get image coordinates for axes. imagePoints = world2img(worldPoints,pose(i),intrinsics); % Draw colored axes. I = insertShape(I,Line=[imagePoints(1,:) imagePoints(2,:); ... imagePoints(1,:) imagePoints(3,:); imagePoints(1,:) imagePoints(4,:)], ... Color=["red","green","blue"],LineWidth=7); I = insertText(I,loc(1,:,i),id(i),BoxOpacity=1,FontSize=25); end
Display the annotated image.
I — Input image
M-by-N-by-3 truecolor image | M-by-N grayscale image
Input image, specified as an M-by-N-by-3 truecolor image or an M-by-N grayscale image.
tagFamily — AprilTag families
one or more valid AprilTag families
AprilTag families, specified as one or more of the valid AprilTag families listed in this table. Specifying a family can reduce the run time of the function by restricting the search.
intrinsics — Camera intrinsic parameters
Camera intrinsic parameters, specified as a
tagSize — Size of tag
Size of the tags in world units (such as millimeters), specified as a positive scalar. The function defines the size of the tags as the length between two adjacent corner outer black edges.
id — Tag IDs
vector of positive integers
Tag IDs, returned a vector of positive integers.
loc — Locations of tags in image
Locations of tags in image, returned as a 4-by-2-by-N array. The array contains the (x,y) locations for each of the four corners for N tags.
pose — Poses of tags
Poses of tags with respect to the camera, returned as an array of
objects. Each object encapsulates the 3-D rigid transformation of a tag in the same
world units as the
tagSize input argument. The origin of each tag
frame is located at the center of the corresponding tag. The
X-Y plane of the tag frame is defined by the
planar surface on which the AprilTag sits with the Z-axis pointing
into the tag.
detectedFamily — Detected tag families
vector of strings
Detected tag families, returned as a vector of strings.
For applications that require real-time performance, while also minimizing false-positive detections, consider using the
For applications that require a faster detection time as opposed to the number of supported IDs, use the
For pregenerated tags for all supported tag families, see Pregenerated AprilTag Images on GitHub.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Generates code only on host platforms.
You must specify the input argument
tagFamilyas a character vector or an array of character vectors. String inputs are not supported.
Version HistoryIntroduced in R2020b
R2022b: Returns poses as
Starting in R2022b, most Computer Vision Toolbox™ functions create and perform geometric transformations using the premultiply
convention. Accordingly, the
readAprilTag function now returns
pose as an array of
objects, which use the premultiply convention. Before, the function returned
pose as an array of
which use the postmultiply convention. For more information, see Migrate Geometric Transformations to Premultiply Convention.