0

I am new to C#. I am trying to do blob detection using opencv asset in unity3d.

I am getting this error:

NullReferenceException: Object reference not set to an instance of an object

after line 89 (where I call detector.detect) The unity console points me to line 230 in FeatureDetect.cs code (part of the opecv library).

I am trying to solve this since a long time, any help is highly appreciated!

Following is my code:

using UnityEngine;
using System.Collections;

using OpenCVForUnity;

namespace OpenCVForUnitySample
{
/// <summary>
/// WebCamTexture to mat sample.
/// </summary>
public class WebCamTextureToMatSample : MonoBehaviour
{
    WebCamTexture webCamTexture;
    Color32[] colors;
    public bool isFrontFacing = false;
    int width = 640;
    int height = 480;
    public Mat rgbaMat;
    Texture2D texture;
    bool initDone = false;
    public Mat GrayMat;
    public Mat KeypointMat;
    public FeatureDetector detector;
    public MatOfKeyPoint keypoint1; 

    // Use this for initialization
    void Start ()
    {
        StartCoroutine (init ());
    }

    private IEnumerator init ()
    {
        if (webCamTexture != null) {
            webCamTexture.Stop ();
            initDone = false;
            rgbaMat.Dispose ();
        }

        // Checks how many and which cameras are available on the device
        for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
            if (WebCamTexture.devices [cameraIndex].isFrontFacing == isFrontFacing) {
                Debug.Log (cameraIndex + " name " + WebCamTexture.devices [cameraIndex].name + " isFrontFacing " + WebCamTexture.devices [cameraIndex].isFrontFacing);

                webCamTexture = new WebCamTexture (WebCamTexture.devices [cameraIndex].name, width, height);
                break;
            }
        }

        if (webCamTexture == null) {
            webCamTexture = new WebCamTexture (width, height);
        }

        Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);

        // Starts the camera
        webCamTexture.Play ();

        while (true) {
            //If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
            if (webCamTexture.width > 16 && webCamTexture.height > 16) {
                Debug.Log ("width " + webCamTexture.width + " height " + webCamTexture.height + " fps " + webCamTexture.requestedFPS);
                Debug.Log ("videoRotationAngle " + webCamTexture.videoRotationAngle + " videoVerticallyMirrored " + webCamTexture.videoVerticallyMirrored);

                colors = new Color32[webCamTexture.width * webCamTexture.height];

                rgbaMat = new Mat (webCamTexture.height, webCamTexture.width, CvType.CV_8UC4);
                GrayMat = new Mat (webCamTexture.height, webCamTexture.width, CvType.CV_8UC1);

                detector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);

                Debug.Log ("detector created");

                detector.detect(GrayMat,keypoint1); // <<< ERROR HERE
                Debug.Log ("keypoints created");

                //Scalar red = Scalar(0,255,0);

                Features2d.drawKeypoints(GrayMat,keypoint1,KeypointMat);

                texture = new Texture2D (webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);

                gameObject.transform.eulerAngles = new Vector3 (0, 0, 0);
                #if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
                gameObject.transform.eulerAngles = new Vector3 (0, 0, -90);
                #endif

                //                                      gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.AngleAxis (webCamTexture.videoRotationAngle, Vector3.back);

                gameObject.transform.localScale = new Vector3 (webCamTexture.width, webCamTexture.height, 1);

                //                                      bool videoVerticallyMirrored = webCamTexture.videoVerticallyMirrored;
                //                                      float scaleX = 1;
                //                                      float scaleY = videoVerticallyMirrored ? -1.0f : 1.0f;
                //                                      if (webCamTexture.videoRotationAngle == 270)
                //                                              scaleY = -1.0f;
                //                                      gameObject.transform.localScale = new Vector3 (scaleX * gameObject.transform.localScale.x, scaleY * gameObject.transform.localScale.y, 1);

                gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
                Camera.main.orthographicSize = webCamTexture.width / 2;

                initDone = true;
                break;
            } else {
                yield return 0;
            }
        }
    }

    // Update is called once per frame
    void Update ()
    {
        if (!initDone)
            return;

        if (webCamTexture.width > 16 && webCamTexture.height > 16) {

            Utils.webCamTextureToMat (webCamTexture, rgbaMat, colors);
            Utils.webCamTextureToMat (webCamTexture, GrayMat, colors);

            #if UNITY_IPHONE && !UNITY_EDITOR

            if (webCamTexture.videoVerticallyMirrored){
                if(isFrontFacing){
                    Core.flip (rgbaMat, rgbaMat, 1);
                }else{
                    Core.flip (rgbaMat, rgbaMat, 0);
                }
            }else{
                if(isFrontFacing){
                    Core.flip (rgbaMat, rgbaMat, -1);
                }
            }
            #endif

            //Utils.matToTexture2D (rgbaMat, texture, colors);
            Utils.matToTexture2D (GrayMat, texture, colors);

            gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
        }
    }

    void OnDisable ()
    {
        webCamTexture.Stop ();
    }

    void OnGUI ()
    {
        float screenScale = Screen.width / 240.0f;
        Matrix4x4 scaledMatrix = Matrix4x4.Scale (new Vector3 (screenScale, screenScale, screenScale));
        GUI.matrix = scaledMatrix;

        GUILayout.BeginVertical ();
        if (GUILayout.Button ("back")) {
            Application.LoadLevel ("OpenCVForUnitySample");
        }
        if (GUILayout.Button ("change camera")) {
            isFrontFacing = !isFrontFacing;
            StartCoroutine (init ());
        }

        GUILayout.EndVertical ();
    }
}
}
Alex
  • 12,468
  • 28
  • 56
ypag
  • 1
  • 2
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jdphenix Apr 26 '15 at 21:23
  • I don't know what a FeatureDetector is, but if it is a Dyanamic Class. You should Construct the object first, before using it. Below your GrayMat. You should add detector = new FeatureDetector(); – Aizen Apr 26 '15 at 21:28
  • "FeatureDetect.cs code (part of the opecv library)." - you're wrong here. all of it is an unofficial, 3rd party wrapper. – berak Apr 29 '15 at 14:16

1 Answers1

0

Try this

 detector = new FeatureDetector(); //assuming it has a default constructor. 
 detector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB);

            Debug.Log ("detector created");
            //detector.detect(

            **detector.detect(GrayMat,keypoint1);**
            Debug.Log ("keypoints created");

Updated

If you check my comment on the detector = new FeatureDetector(); I added assuming it has a default constructor. Now the new error that you're having is simply saying that the FeatureDetector class doesn't have one.

In C# it is by default that it will make a default constructor if you don't have one, but somehow it will not create it if you make a parameter constructor first, before a default constructor.

Now you have to add it manually. Simply open the FeatureDetector Class and add this line

public FeatureDetector(){}

That is it. Try to run your Code again.

Aizen
  • 1,705
  • 2
  • 12
  • 25
  • Thanks Aizen, I tried it, this is what I get: "The type `OpenCVForUnity.FeatureDetector' does not contain a constructor that takes `0' arguments" What do you think might be going wrong here? Thanks for your time! – ypag Apr 29 '15 at 03:36