简体   繁体   English

unity3d中的Blob检测:NullReferenceException

[英]blob detection in unity3d: NullReferenceException

I am new to C#. 我是C#的新手。 I am trying to do blob detection using opencv asset in unity3d. 我正在尝试在unity3d中使用opencv资产进行斑点检测。

I am getting this error: 我收到此错误:

NullReferenceException: Object reference not set to an instance of an object NullReferenceException:对象引用未设置为对象的实例

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). 在第89行之后(我将其称为detector.detect),统一控制台将我指向FeatureDetect.cs代码中的230行(opecv库的一部分)。

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 ();
    }
}
}

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(); 如果检查我对检测器的评论= 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. 现在,您遇到的新错误只是说FeatureDetector类没有一个。

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. 在C#中,默认情况下,如果您没有默认的构造函数,它将创建一个默认的构造函数,但是如果您在默认构造函数之前先创建一个参数构造函数,它将以某种方式无法创建它。

Now you have to add it manually. 现在,您必须手动添加它。 Simply open the FeatureDetector Class and add this line 只需打开FeatureDetector类并添加此行

public FeatureDetector(){}

That is it. 这就对了。 Try to run your Code again. 尝试再次运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM