How to destroy an tracked image when using ar foundation?

Hi! I am a bit stumped since the documentation says the following:

You should never Destroy a trackable component or its GameObject directly. For trackables that support manual removal, their manager provides a method to remove it. For example, to remove an anchor, you need to call RemoveAnchor on the ARAnchorManager.
Trackable managers | AR Foundation | 4.1.13

But I can’t find a method for destroying tracked images in the trackedImage manager, leaving me wondering how I am supposed to actually go about cleaning up AR stuff, if my scene is running for longer periods of time, generating new image markers that should be cleaned up an no longer tracked.

Any help is appreciated :slight_smile:

You don’t have to do any cleaning yourself. If your app is designed to track many reference images, you can split them into multiple reference image libraries and select the one that is currently needed. Setting a new image library (or null) to the ARTrackedImageManager.referenceLibrary will clean resources from the old image library.

1 Like

Bump for this question. I also would like to destroy gameobjects generated from tracked image manager in order to conserve memory, however its unclear how to do this. In my case I am only using one reference image library so it doesnt make sense to change the reference in order to force a cleanup.

Is there an update on this at all? Anyone figured out how to do it?

I have tried setting the referenceLibrary variable, but it is not clearing the old tracked targets.

_aRTrackedImageManager.referenceLibrary = _imageLibrary;```

This appears to do nothing... I have also tried CreateRuntimeLibrary() and SetTrackablesActive(), but neither gives the behavior I want.

What I am looking for is to clear out the tracked images, but if an image is seen again, it should come back.

Everything I have tried either leaves all the tracked images or removes them all permanently.

I am using:

- Unity 2020.3.26f1
- AR Foundation 4.1.10
- ARCore 4.1.10

Any help would be appreciated.
1 Like

Image trackables can only be added or removed by your AR Foundation provider (ie, ARCore or ARKit). If you wish to hide your tracked image content, I recommend deactivating the trackable GameObjects (ARTrackedImageManager.trackables), then re-activating them later based on the application logic you are implementing.

Are you trying to remove the actual tracked images or the content that spawns when a tracked image is recognized?

So If I want to track only 1 image at a time, what is the ideal solution?

  • A predefined reference library (Not dynamic)
  • Lets say 2 images are in the camera frame , I wanna track and act on one of them only. Not both.

I already tried deactivating the ARTrackedImage gameobject of that image. But it is still getting tracked. Or is it the way simulator act? and I can get this working on device?

@andyb-unity

1 Like

Set Max Number of Moving Images to 1? That’s the first thing I would try. AR Tracked Image Manager component | AR Foundation | 6.0.2

Ok I looked a bit closer at this: maximumNumberOfTrackedImages | Apple Developer Documentation

So this could work for you if the image that you want to track is always in view.

Otherwise what you would need to do is set up your image libraries like this: 1 library with all the images you want to track, and then n libraries, each containing only 1 image. Then switch between libraries at runtime to prevent the other images from being tracked after you have found the first one.

This seems to be more for track while moving feature. I will post my findings down.

I thought of this reference lib mechanism. But I don’t wanted to go this way because this is for sure gonna be effecting the performance at runtime. So for tracking one image at a time, Like what vuforia does,

After a lot of thoughts and time investments I came up with the following findings and solution.

  1. Set device as the Tracking origin mode on the XROrigin Component.
  2. Create a customized tracked image status manager script and use the

ARTrackedImageMange’s “trackedImagesChanged” action to get

ARTrackedImagesChangedEventArgs added, updated and removed list of trackables.

Consider TrackedImageInfoManager in the samples as a starting point for this.

  1. If you are familiar with existing third party image recognitions like vuforia, wikitude, easyAR, zappar etc.
    ARFoundation’s image tracker can be mapped as …

Added - First Seen. This list will get filled with a specific trackable when it was found for the first time in an ARSession. It will be removed the moment it gets added to the updated list and will never get add again to this list in the same session.

Updated - Tracked OR Limited (You can consider limited as Track lost) : This will need a logic to manage the states. Apart from added, this will have items coming in and out throughout the experience. When a trackable is in the camera view and being tracked it will be available in the updated list with a trackingState.

Removed - Is probably happened once the session ends. I never personally had this item updated.

So talking about the updated list. Until and unless there is only 1 image in the camera view at a time, this updated list will have that single image trackable in the list. The moment a second image comes to the camera view, this list will have that second trackable as the second element in the list. And both will have the trackingState as tracking. Now if you move the camera to get ride of one of them and focus only on one, Then this list will again go back to the 1 count with the current tracking item but in that change call back you can get a tracking limited state for the ignored item. This limited state update happens only once but tracking callback is continuous.

Now what can be done is this updated list could be used to compare the trackables with their ids and their tracking states. Once we have this handy, to manage the one image at a time scenario, need to add a logic to find the nearest image to the device by calculating the distance of each tracking trackables with the device position. This is why I mentioned to make the device as the origin in the first point. This makes the distance more precise.

NB: I have come up with all these findings using the XRSimulator and the ARFoundation samples only. Please mind rectifying anything mentioned above.

So as a conclusion, this way of doing will perform no harm on the existing ARFoundation and we can leverage the full potential of using cross platform openXR runtime features for AR Apps we create. I am working on the one image at a time logic will post soon after testing them on the actual devices with some actual images.

@andyb-unity Thanks for the quick reply. A humble suggestion would be to include more details about the
ARTrackedImagesChangedEventArgs in the documentation.

2 Likes

Nice writeup! Yes this is all correct. We have a new technical writer this year who has done a lot of work on documentation already. We’ll be rolling out more docs updates throughout the year.

1 Like

Note that the trackingState property applies to all trackables, not just images. So pages like this one that we’ve done for plane tracking will be similar for tracked images. AR Plane component | AR Foundation | 6.0.2

1 Like

Cool. That would be awesome

Yeah, Will be digging into plane detection next. Thanks.

Hello !

I have a problem with ARCore, I try to remove an image who is insn’t tracked anymore. So I use the State to do that but I have a problem

Here my code :


foreach (var trackedImage in eventArgs.updated)
{
var imageName = trackedImage.referenceImage.name;
#if UNITY_ANDROID
Debug.Log("Android");
#endif
Debug.Log(imageName + " is stated " + trackedImage.trackingState);
#if UNITY_ANDROID
if (trackedImage.trackingState == TrackingState.Limited)
{
Debug.Log($"Image '{imageName}' is no longer tracked. Scheduling destruction.");
if (_instantiatedPrefabs.ContainsKey(imageName))
{
_instantiatedPrefabs[imageName].DestroyPrefabs();
_instantiatedPrefabs.Remove(imageName);
}
}
#endif

But, I don’t understand why the program never reach the “Debug.Log($“Image ‘{imageName}’ is no longer tracked. Scheduling destruction.”);”
In the log I have :
Android
Image is stated Limited

So I guess the problem is this “trackedImage.trackingState == TrackingState.Limited”

I don’t know the problem and I don’t know if it will solve anything

Sorry for the mistakes and it’s not my code so I don’t understand all of it

Thanks in advance

@VirgileSi I recommend that you attach a debugger to your code so that you can see what’s happening. Unity - Manual: Debug C# code in Unity

That does not work. In my understanding, a “moving” image is not the same as the static images. And it will still track all the images it detects.

I really don’t understand why you guys didn’t give us the option of removing a tracked image.
It seems like there is just no way to remove one after detection? Very strange

AR Foundation is a transparent window into the platform’s state. If the platform hasn’t removed the tracked image, AR Foundation won’t remove its representation of the tracked image either. You can of course disable or destroy any of your GameObjects regardless.