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
Related articles
- c # - about nullreferenceexception: object reference not set to an instance of an object
- Object reference and copy instance analysis in php
- c # - error "nullreferenceexception: object reference not set to an instance of an object"
- c # - [unity] object reference not set to an instance of an object error resolution
- c # - object references not set in the unity instance
- How to implement video object tracking with OpenCV -python3
- Java object serialization operation instance analysis
- Reference and copy instance resolution in Python
- Java understand value passing and reference passing by instance
- Spring forced injection and reference instance resolution
- Python class attributes and instance attributes, class object and instance object usage analysis
- iOS debugging block reference object cannot be released tips sharing
- Python matches a position (opencv) instance in the original image through a screenshot
- vbnet (net framework) object reference specification
- Java reference queue and virtual reference instance analysis
- javascript - how to reference vue instance in vuejs inline method handler
- ES6 top-level object, global object instance analysis
- Opencv implements object extraction and measurement
- java get null field instance code in object
- Method instance for getting injected object through ApplicationContext getBean in spring
Nothing is assigned to WStartp.
That's null
One
You don't set anything in mt (it is null) and you are accessing it suddenly