AprilTag: Tag and follow

视频教程11 - AprilTag标记追踪:https://singtown.com/learn/49590/
视频教程21 - 追其他物体的小车:https://singtown.com/learn/50041/

Introduction of AprilTag

Data:https://april.eecs.umich.edu/software/apriltag.html

AprilTag is a visual baseline system which can be used for various missions ranging from AR, robots and camera calibration. This tag can be printed directly by printer and AprilTag can calculate the accurate position, direction and id towards the camera, which is so useful for OpenMV. As follows:

In simple terms, it can identify the 3D position and id of a tag if you stick the tag on your target.

Category of AprilTag

The category of AprilTag is named as family, and it includes following categories:

TAG16H5 → 0 to 29
TAG25H7 → 0 to 241
TAG25H9 → 0 to 34
TAG36H10 → 0 to 2319
TAG36H11 → 0 to 586
ARTOOLKIT → 0 to 511

That is to say, there are 30 families of TAG16H5, which all have their own corresponding id, from 0 to 29.

So what is the difference between different families?

For example, the valid area of TAG16H5 is a cube of 4 x 4. It can see farther than TAG36H11 (for it has 6 x 6 cubes). However, the error rate of TAG16H5 is higher than TAG36H11 for TAG36H11 has more checking message. So we would recommend you TAG36H11.

Generating AprilTag

It is so easy to generate AprilTag. You can download it from the Internet or generate it from IDE in OpenMV. Click tool——Machine Vision——AprilTag, and then choose family. Here we would recommend you TAG36H11.

After that, fill in the amount you want to generate. For example, generate an image of which the id is 0~9 if you need 10.

Then choose the file where you want to store the image.

The image then would be created in the file.

Finally, use the printer to print the image. You can use the screen but it could be too bright.

Code

# AprilTags Example
#
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.

import sensor, image, time, math

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    for tag in img.find_apriltags(): # defaults to TAG36H11 without "families".
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        degress = 180 * tag.rotation() / math.pi
        print(tag.id(),degress)

You can see that it can identify the id, the rotation angle and the position.

3D location

What is most miraculous about AprilTag is its 3D location, which can inform you of the space position of the Tag, which includes 6 degree of freedom, 3 positions and 3 angles.

# AprilTags Example
#
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.

import sensor, image, time, math

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()


# Note! Unlike find_qrcodes the find_apriltags method does not need lens correction on the image to work.

# What's the difference between tag families? Well, for example, the TAG16H5 family is effectively
# a 4x4 square tag. So, this means it can be seen at a longer distance than a TAG36H11 tag which
# is a 6x6 square tag. However, the lower H value (H5 versus H11) means that the false positve
# rate for the 4x4 tag is much, much, much, higher than the 6x6 tag. So, unless you have a
# reason to use the other tags families just use TAG36H11 which is the default family.

# The AprilTags library outputs the pose information for tags. This is the x/y/z translation and
# x/y/z rotation. The x/y/z rotation is in radians and can be converted to degrees. As for
# translation the units are dimensionless and you must apply a conversion function.

# f_x is the x focal length of the camera. It should be equal to the lens focal length in mm
# divided by the x sensor size in mm times the number of pixels in the image.
# The below values are for the OV7725 camera with a 2.8 mm lens.

# f_y is the y focal length of the camera. It should be equal to the lens focal length in mm
# divided by the y sensor size in mm times the number of pixels in the image.
# The below values are for the OV7725 camera with a 2.8 mm lens.

# c_x is the image x center position in pixels.
# c_y is the image y center position in pixels.

f_x = (2.8 / 3.984) * 160 # find_apriltags defaults to this if not set
f_y = (2.8 / 2.952) * 120 # find_apriltags defaults to this if not set
c_x = 160 * 0.5 # find_apriltags defaults to this if not set (the image.w * 0.5)
c_y = 120 * 0.5 # find_apriltags defaults to this if not set (the image.h * 0.5)

def degrees(radians):
    return (180 * radians) / math.pi

while(True):
    clock.tick()
    img = sensor.snapshot()
    for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # 默认为TAG36H11
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
            degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
        # Translation units are unknown. Rotation units are in degrees.
        print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
    print(clock.fps())

It would be output as six variates. Tx, Ty, Tz are 3 locations while Rx,Ry,Rz are 3 rotations.

扩展阅读:

  • 来自星瞳实验室APP: Apriltag识别中,怎么定位/测距?输出的tx ty tz的单位是什么?怎么就得到实际的距离了?https://forum.singtown.com/topic/52

results matching ""

    No results matching ""