Image Tracker

The Image Tracker recognizes and tracks planar images. It can demonstrate not only 3D objects but also videos, even the transparent one.

Create Instants
Starting / Stopping the Tracker
Using the Tracking Information
Setting a Target Image
Adding / Replacing a Target Image
Changing Tracking Mode

Create Instants

>ImageTrackerViewController.swift

var cameraDevice:MasCameraDevice = MasCameraDevice()
var backgroundRenderer:MasBackgroundRenderer = MasBackgroundRenderer()
var trackingManager:MasTrackerManager = MasTrackerManager()

Starting / Stopping the Tracker

To start / stop Tracker after loading the map, refer to the following code.

>ImageTrackerViewController.swift

@objc func resumeAR()
{
    ...
    trackingManager.start(.TRACKER_TYPE_IMAGE)
}

@objc func pauseAR()
{
    trackingManager.stopTracker()
    ...
}

Using the Tracking Information

In the folder where SDK is installed, go to ‘data > SDKSample > Original > ImageTarget' folder, and there is sample target image. Print the image.

If you use the sample code, the following content will be augmented for each image.

  • Blocks.jpg: The alpha video is augmented.
  • Lego.jpg: The normal video is augmented.
  • Glacier.jpg: The cube with the texture is augmented.

To apply tracking results to augmented objects, refer to the following code.

>ImageTrackerViewController.swift

    override func glkView(_ view: GLKView, drawIn rect: CGRect) {
        ...
        let trackingState:MasTrackingState = trackingManager.updateTrackingState()
        let result:MasTrackingResult = trackingState.getTrackingResult()

        guard let backgroundTexture = backgroundRenderer.getBackgroundTexture() else {
            return
        }
        
        backgroundRenderer.begin(backgroundTexture)
        backgroundRenderer.renderBackgroundToTexture()
        backgroundRenderer.end()
        
        backgroundQuad.draw(texture: backgroundTexture, projectionMatrix: cameraDevice.getBackgroundPlaneProjectionMatrix())
        
        glEnable(GLenum(GL_DEPTH_TEST))
        
        let projectionMatrix:matrix_float4x4 = cameraDevice.getProjectionMatrix()
        
        let trackingCount:Int32 = result.getCount()
        
        if trackingCount > 0 {
            for i in stride(from: 0, to: trackingCount, by: 1) {
                let trackable:MasTrackable = result.getTrackable(i)
                
                if trackable.getName() == "Lego" {
                    if videoCaptureController.getState() == MEDIA_STATE.PLAYING {
                        videoCaptureController.play()
                        videoCaptureController.update()
                        
                        videoPanelRenderer.setVideoTextureId(videoTextureId: videoCaptureController.getTextureId())
                        videoPanelRenderer.setProjectionMatrix(projectionMatrix: projectionMatrix)
                        videoPanelRenderer.setPoseMatrix(poseMatrix: trackable.getPose())
                        videoPanelRenderer.setTranslation(x: 0.0, y: 0.0, z: 0.0)
                        videoPanelRenderer.setScale(x: 0.26, y: -0.15, z: 1.0)
                        videoPanelRenderer.draw()
                    }
                } else if trackable.getName() == "Blocks" {
                    if chromakeyVideoCaptureController.getState() == MEDIA_STATE.PLAYING {
                        chromakeyVideoCaptureController.play()
                        chromakeyVideoCaptureController.update()
                        
                        chromakeyVideoPanelRenderer.setVideoTextureId(videoTextureId: chromakeyVideoCaptureController.getTextureId())
                        chromakeyVideoPanelRenderer.setProjectionMatrix(projectionMatrix: projectionMatrix)
                        chromakeyVideoPanelRenderer.setPoseMatrix(poseMatrix: trackable.getPose())
                        chromakeyVideoPanelRenderer.setTranslation(x: 0.0, y: 0.0, z: 0.0)
                        chromakeyVideoPanelRenderer.setScale(x: 0.26, y: -0.18, z: 1.0)
                        chromakeyVideoPanelRenderer.draw()
                    }
                } else if trackable.getName() == "Glacier" {
                    textureCube.setProjectionMatrix(projectionMatrix: projectionMatrix)
                    textureCube.setPoseMatrix(poseMatrix: trackable.getPose())
                    textureCube.setTranslation(x: 0.0, y: 0.0, z: -0.025)
                    textureCube.setScale(x: 0.15, y: 0.15, z: 0.05)
                    textureCube.draw()
                } else {
                    coloredCube.setProjectionMatrix(projectionMatrix: projectionMatrix)
                    coloredCube.setPoseMatrix(poseMatrix: trackable.getPose())
                    coloredCube.setTranslation(x: 0.0, y: 0.0, z: -0.025)
                    coloredCube.setScale(x: 0.15, y: 0.15, z: 0.005)
                    coloredCube.draw()
                }
            }
        }
        else {
            videoCaptureController.pause()
            chromakeyVideoCaptureController.pause()
        }
        
        glDisable(GLenum(GL_DEPTH_TEST))
        
    }

Setting a Target Image

By calling function addTrackerData to register the map file and calling function loadTrackerData, the target image can be tracked. To set a target image, refer to the following code.

>ImageTrackerViewController.swift

func startEngin()
{
    ...
    let blocksTrackerMapPath:String = Bundle.main.path(forResource: "Blocks", ofType: "2dmap", inDirectory: "data/SDKSample")!
    let glacierTrackerMapPath:String = Bundle.main.path(forResource: "Glacier", ofType: "2dmap", inDirectory: "data/SDKSample")!
    let legoTrackerMapPath:String = Bundle.main.path(forResource: "Lego", ofType: "2dmap", inDirectory: "data/SDKSample")!
        
    trackingManager.start(.TRACKER_TYPE_IMAGE)
    trackingManager.setTrackingOption(.NORMAL_TRACKING)
    trackingManager.addTrackerData(blocksTrackerMapPath)
    trackingManager.addTrackerData(glacierTrackerMapPath)
    trackingManager.addTrackerData(legoTrackerMapPath)
        
    trackingManager.loadTrackerData()
}

Adding / Replacing a Target Image

  1. Create a map file refer to Documentation > Target Manager.

  2. Download the file you created.

  3. Unzip the downloaded file and copy it to the desired path.

  4. Set a target image.

Changing Tracking Mode

Image Tracker has three Tracking modes : normal, extended, and multi.

The default setting is normal mode.

  • normal mode: This is the normal image tracker.
trackingManager.setTrackingOption(.NORMAL_TRACKING)
  • extended mode: This function is traceable even when the distance from the target image is far away.
trackingManager.setTrackingOption(.EXTENDED_TRACKING)
  • multi mode: This function allows you to recognize and track up to three target images at the same time.
trackingManager.setTrackingOption(.MULTI_TRACKING)