Home>

I want to get the AR marker coordinates using a Web camera and OpenCVSharp.
The marker recognition itself was made by combining LiveSketch and Marker_Detector in demo, but I would like to use the coordinates in another script.
Specifically, I want to put coordinates from LiveSketchScript into MarkerTracking's WStartp.
I think corner is a marker coordinate so I am trying to put it in.
After that, I want to calculate the distance between the centers of the two markers.

While it starts now, WStartp will show NULL when checked in Debug.Log, and the following errors will be displayed in large numbers.

Thank you.

namespace OpenCvSharp.Demo
{
    using UnityEngine;
    using OpenCvSharp;
    using Aruco;
    public class LiveSketchScript: WebCamera
    {
        MarkerTracking mt;
        protected override void Awake ()
        {
            base.Awake ();
            this.forceFrontalCamera = true;
        }
        // Our sketch generation function
        protected override bool ProcessTexture (WebCamTexture input, ref Texture2D output)
        {
            // Create default parameres for detection
            DetectorParameters detectorParameters = DetectorParameters.Create ();
            // Dictionary holds set of all available markers
            Dictionary dictionary = CvAruco.GetPredefinedDictionary (PredefinedDictionaryName.Dict6X6_250);
            // Variables to hold results
            Point2f [] [] corners;
            int [] ids;
            Point2f [] [] rejectedImgPoints;
            // Create Opencv image from unity texture
            Mat mat = Unity.TextureToMat (input);
            // Convert image to grasyscale
            Mat grayMat = new Mat ();
            Cv2.CvtColor (mat, grayMat, ColorConversionCodes.BGR2GRAY);
            // Detect and draw markers
            CvAruco.DetectMarkers (grayMat, dictionary, out corners, out ids, detectorParameters, out rejectedImgPoints);
            CvAruco.DrawDetectedMarkers (mat, corners, ids);
            mt.WStartp = corners;
            output = Unity.MatToTexture (mat);

            return true;
        }
    }
}
using OpenCvSharp;
using OpenCvSharp.Demo;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MarkerTracking: MonoBehaviour
{
    LiveSketchScript LSS;
    public Point2f [] [] WStartp, WEndo, CStartp, CEndp;
    // DefaultTrackableEventHandler DTE;
    bool Count = false;
    Vector3 WposST, WposED, CposST, CposED, Ccal;
    float Dist;
    // Start is called before the first frame update
    void Start ()
    {
        // DTE = this.gameObject.GetComponent<DefaultTrackableEventHandler>();
    }
    // Update is called once per frame
    void Update ()
    {
        Debug.Log (WStartp);
        if (Input.GetKeyDown ("return"))
            {
                Debug.Log (WStartp);
                if (Count == false)
                {
                    // WposST = GameObject.Find ("WorldMarker"). Transform.position;
                    // CposST = this.gameObject.transform.position;
                    Count = true;
                }
                else if (Count)
                {
                    Ccal = CposED-(WposED-WposST);
                    Dist = Vector3.Distance (CposST, Ccal);
                    Debug.Log (Dist);
                    Count = false;
                }
            }
            if (Count)
            {
                WposED = GameObject.Find ("WorldMarker"). Transform.position;
                CposED = this.gameObject.transform.position;
            }
    }
}
NullReferenceException: Object reference not set to an instance of an object
OpenCvSharp.Demo.LiveSketchScript.ProcessTexture (UnityEngine.WebCamTexture input, UnityEngine.Texture2D&output) (at Assets/OpenCV + Unity/Demo/LiveSketch_WebCam/LiveSketchScript.cs: 67)
OpenCvSharp.Demo.WebCamera.Update () (at Assets/OpenCV + Unity/Demo/Scripts/WebCamera.cs: 145)
  • Answer # 1

      

    Debug.Log (WStartp);

    Nothing is assigned to WStartp.
    That's null


    One

      

    mt.WStartp = corners;

    You don't set anything in mt (it is null) and you are accessing it suddenly