Skip to content

Creating and using 2D Markers (Beta)

John Bell edited this page Mar 14, 2018 · 1 revision

artoolkitX includes a tracking module that looks for known 2D visual landmarks in a video stream and tracks the position and orientation (pose) of these relative to the camera.

These markers are complex images that contain many "image features".

Please see Wikipedia's article on Image Features.

2D Marker Images

Creating a 2D Marker

The 2D Tracker can be loaded with trackable markers using a jpeg. The marker is defined with a *.jpg image and an image scale, which determines the size of any AR content with respect to the marker image.

static const struct marker markers[] = {
    {"pinball.jpg", 1.0}
};
static const int markerCount = (sizeof(markers)/sizeof(markers[0]));

The directory of the resources for the platform of choice can be found using arUtilGetResourcesDirectoryPath and can then be loaded using the addTrackable method on the ARController. The configuration string for the marker is prepended with "2d" followed by the image name and the defined marker height.

// 1 - Add trackables form markers.
int markerIDs[markerCount];
int markerModelIDs[markerCount];
char *resourcesDir = arUtilGetResourcesDirectoryPath(AR_UTIL_RESOURCES_DIRECTORY_BEHAVIOR_BEST);
char *markerConfig;
for (int i = 0; i < markerCount; i++) {
    asprintf(&markerConfig, "2d;%s/%s;%f", resourcesDir, markers[i].name, markers[i].height);
    markerIDs[i] = arController->addTrackable(markerConfig);
    if (markerIDs[i] == -1) {
        ARLOGe("Error adding marker.\n");
        quit(-1);
    }
    free(markerConfig);
}

Creating a 2D Marker Database

Alternatively, it is possible to take an image directory with images and then create an image database that can be loaded using the artoolkitx_image_database_2d tool. This will produce an xml or yml output that can be compressed using suffix ".gz" on the supplied filename.

Usage: artoolkitx_image_database_2d [options]
    Options:
    --imgDir <Image directory to generate image database>
    --fileOut <Output file name to generate image database .xml/.yml with *.gz forcing compression i.e. .xml.gz/.yml.gz>
    --version: Print artoolkitX version and exit.
    -h -help --help: show this message

Loading a 2D Marker Database

When the image database has been created it can then be loaded into the ARController

//2 - Add trackabales from database file
const char *databaseName = "database.xml.gz";
char *databaseFullPath;
asprintf(&databaseFullPath, "%s/%s", resourcesDir, databaseName);
arController->load2DTrackerImageDatabase(databaseFullPath);

Once the database has been loaded by the ARController, it is important to retrieve the markerIDs that have been loaded into the ARController. Note: It is possible to get the count of trackables for only the 2D trackabales.

int markerCount = arController->countTrackables();
int markerIDs[markerCount];
int markerModelIDs[markerCount];
for(int i=0;i<markerCount;i++)
{
    ARTrackable *trackable = arController->getTrackableAtIndex(i);
    markerIDs[i] = trackable->UID;
}