These scripts and samples demonstrate how to use the ArUco marker detection functions of OpenCV with the ZED camera and SDK in Unity.
The ZEDToOpenCVRetriever script can also be used to supply a proper OpenCV mat of the camera's images for other OpenCV uses.
*Note: The free trial version of OpenCV for Unity will work in the Unity Editor, but you will not be able to build into a standalone app.
You'll need to use marker images from a pre-defined dictionary within the ArUco module of OpenCV. For convienence, we've included the first five images from the 4x4 dictionary in this plugin. Other images are available on the Internet. At time of writing, the first 50 markers of the 4x4 dictionary can be downloaded at http://jevois.org/data/ArUco.zip.
If you print the markers, make sure they lay flat. It can be good to secure them onto a more rigid surface like a cardboard square. It is also good to include a border around the black edges of the marker; otherwise, OpenCV will not be able to detect the marker if it's edges are touching a black object.
You can also display the markers on a screen, like a phone, which can be especially convenient during development. Just be sure that the brightness is turned up, and that you keep the size of the marker static.
A basic marker detection scene needs four things:
Note: The MarkerObject can be substituted for another script if it subscribes to ZEDArUcoDetectionManager.OnMarkersDetected, which lists all markers detected each frame.
The most basic scene, which moves a virtual object to a marker when it's detected, would use the MarkerObject_MoveToMarker script. With those four objects in the scene, do the following:
Run the scene, point the ZED at the marker, and the 3D object should move to it.
Note: MarkerObject_MoveToMarker isn't built to handle detecting multiple instances of the same marker at the same time. If you want multiple copies of a virtual object in this case, see MarkerObject_CreateObjectsAtMarkers.
The MarkerObject script is an abstract class that will automatically register itself with the scene's ZEDArUcoDetectionManager on start. It has functions that will be called with information about the marker's detection:
For a barebones reference, see MarkerObject_MoveToMarkerSimple. This is a very barebones implementation of MarkerObject, which moves the object to its marker when detected. It lacks smoothing and other features of MarkerObject_MoveToMarker, showing only what's required to make a class properly inherit from MarkerObject.