【问题标题】:blob detection in unity3d: NullReferenceExceptionunity3d 中的 blob 检测:NullReferenceException
【发布时间】:2015-07-05 04:41:03
【问题描述】:

我是 C# 新手。我正在尝试在 unity3d 中使用opencv asset 进行斑点检测。

我收到此错误:

NullReferenceException:对象引用未设置为对象的实例

在第 89 行之后(我调用detector.detect),统一控制台将我指向 FeatureDetect.cs 代码(opecv 库的一部分)中的第 230 行。

我一直在努力解决这个问题,非常感谢任何帮助!

以下是我的代码:

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

【问题讨论】:

  • 我不知道 FeatureDetector 是什么,但如果它是一个动态类。您应该先构造对象,然后再使用它。在您的 GrayMat 下方。您应该添加检测器 = new FeatureDetector();
  • "FeatureDetect.cs 代码(opecv 库的一部分)。" - 你错了。所有这些都是非官方的第 3 方包装器。

标签: c# opencv unity3d feature-detection opencvsharp


【解决方案1】:

试试这个

 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");

更新

如果您查看我对检测器的评论 = new FeatureDetector();我添加了假设它有一个默认的构造函数。现在,您遇到的新错误只是说 FeatureDetector 类没有。

在 C# 中,默认情况下,如果你没有默认构造函数,它会创建一个默认构造函数,但不知何故,如果你先创建一个参数构造函数,然后再创建一个默认构造函数,它就不会创建它。

现在您必须手动添加它。只需打开 FeatureDetector 类并添加这一行

public FeatureDetector(){}

就是这样。尝试再次运行您的代码。

【讨论】:

  • 谢谢 Aizen,我试过了,这是我得到的:“OpenCVForUnity.FeatureDetector' does not contain a constructor that takes 0' 类型参数”你认为这里可能出了什么问题?感谢您的宝贵时间!
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
相关资源
最近更新 更多