質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

3056閲覧

Unity)Texture2DがNull?

navesanta

総合スコア198

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/08/18 01:04

OpenCV for Unityを使って顔認証→認識した顔の前にイメージを表示するアプリを作っています。

手順)
1、OpenCv for unityのインストール
2、OpenCvのセット(Set Plugin Import SettingとStreamingAssetsをAssets直下へ)
3、CanvasとQuadを作成して下記コードをQuadにセット
4、これでいいかと思っていましたがシュミレータで試すと下記エラーが出ます。
エラー内容はtexture2DがNullと言う意味で判断しています。
しかしOnWebCamTextureToMatHelperInitialized()でtexture2Dをnewしており、
Start時に_webCamTextureToMatHelper.Initialize()で実行しているので
Nullではないような気がしています。どこがおかしいのでしょうか?

環境)
PC: Windows10
Unity2019.4.0f1
言語:C#

error内容)

ArgumentNullException: Value cannot be null. Parameter name: texture2D OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D (OpenCVForUnity.CoreModule.Mat mat, UnityEngine.Texture2D texture2D, System.Boolean flip, System.Int32 flipCode, System.Boolean flipAfter, System.Boolean updateMipmaps, System.Boolean makeNoLongerReadable) (at Assets/OpenCVForUnity/org/opencv/unity/Utils.cs:321) WebCamera.Update () (at Assets/Scripts/WebCamera.cs:45)

WebCamera.cs

using System.Collections.Generic; using OpenCVForUnity; using OpenCVForUnityExample; using UnityEngine; using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; using UnityEngine.UI; [RequireComponent(typeof(WebCamTextureToMatHelper), typeof(FpsMonitor))] public class WebCamera : MonoBehaviour { [SerializeField] Image _firePrefab; Texture2D _texture; WebCamTextureToMatHelper _webCamTextureToMatHelper; FpsMonitor _fpsMonitor; Image _fire; Mat webCamTextureMat; static readonly Scalar SKIN_LOWER = new Scalar(0, 70, 90); static readonly Scalar SKIN_UPPER = new Scalar(35, 255, 255); static readonly float FIRE_SCALE = 100f; static readonly float FIRE_Z_POS = -10f; void Start() { Input.backButtonLeavesApp = true; _fpsMonitor = GetComponent<FpsMonitor>(); _webCamTextureToMatHelper = GetComponent<WebCamTextureToMatHelper>(); _webCamTextureToMatHelper.Initialize(); _fire = Instantiate(_firePrefab, new Vector3(0f, 0f, FIRE_Z_POS), Quaternion.identity); _fire.transform.localScale = new Vector3(FIRE_SCALE, FIRE_SCALE, FIRE_SCALE); } void Update() { if (_webCamTextureToMatHelper.IsPlaying() && _webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = _webCamTextureToMatHelper.GetMat(); SetImage(rgbaMat); Utils.fastMatToTexture2D(rgbaMat, _texture); }else{ return; } } void SetImage(Mat webcamMat) { //RGBAをHSVに変換 using(var hsvMat = new Mat()) using(var handMask = new Mat()) using(var centroids = new Mat()) using(var stats = new Mat()){ //RGBAをHSVに変換 Imgproc.cvtColor(webcamMat, hsvMat, Imgproc.COLOR_RGBA2RGB); Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV); //肌色領域を抽出 Core.inRange(hsvMat, SKIN_LOWER, SKIN_UPPER, handMask); //ラベリング var nLabels = Imgproc.connectedComponentsWithStats(handMask, new Mat(), stats, centroids); //最大の領域の重心を取得 var maxAreaLabel = 0; var maxArea = 0.0; for (int i = 1; i < nLabels; i++) { //0番目のラベルは背景のため飛ばす var area = stats.get(i, Imgproc.CC_STAT_AREA)[0]; if (area > maxArea) { maxArea = area; maxAreaLabel = i; } } //画像上の重心位置をワールド座標に変換 var ctrdOnImg = new Point(centroids.get(maxAreaLabel, 0)[0], centroids.get(maxAreaLabel, 1)[0]); var ctrdOnWorld = new Point((float)ctrdOnImg.x - webcamMat.width() / 2f, webcamMat.height() / 2f - (float)ctrdOnImg.y); _fire.transform.position = new Vector3((float)ctrdOnWorld.x, (float)ctrdOnWorld.y, FIRE_Z_POS); } } public void OnWebCamTextureToMatHelperInitialized() { Debug.Log ("OnWebCamTextureToMatHelperInitialized"); var webCamTextureMat = _webCamTextureToMatHelper.GetMat(); _texture = new Texture2D (webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); GetComponent<Renderer>().material.mainTexture = _texture; Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); if (_fpsMonitor != null){ _fpsMonitor.Add ("width", webCamTextureMat.width().ToString()); _fpsMonitor.Add ("height", webCamTextureMat.height().ToString()); _fpsMonitor.Add ("orientation", Screen.orientation.ToString()); } float width = webCamTextureMat.width(); float height = webCamTextureMat.height(); float widthScale = Screen.width / width; float heightScale = Screen.height / height; if (widthScale < heightScale) { Camera.main.orthographicSize = (width * Screen.height / Screen.width) / 2; } else { Camera.main.orthographicSize = height / 2; } //Quadを画面いっぱいにリサイズ ////https: //blog.narumium.net/2016/12/11/unityでスマホカメラを全面表示する/ var quadHeight = Camera.main.orthographicSize * 2; var quadWidth = quadHeight * Camera.main.aspect; transform.localScale = new Vector3(quadWidth, quadHeight, 1); } public void OnWebCamTextureToMatHelperDisposed () { Debug.Log ("OnWebCamTextureToMatHelperDisposed"); if (_texture != null) { Destroy(_texture); _texture = null; } } public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode){ Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); } void OnDestroy() { _webCamTextureToMatHelper.Dispose (); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MMashiro

2020/08/18 02:35

OnWebCamTextureToMatHelperInitializedはどこから呼ばれていますか? 提示されているコードだけだと呼ばれていないように見えます (_webCamTextureToMatHelper.Initialize();内部から呼ばれている?)
guest

回答1

0

ベストアンサー

OnWebCamTextureToMatHelperInitializedはイベント動作で呼ばれるように見えるので、WebCamTextureToMatHelperInitializedの前にUpdateが走っているとか考えられないですか?
そのエラーが出たときは
Debug.Log ("OnWebCamTextureToMatHelperInitialized");
が実行されているのでしょうか?

WebCamTextureToMatHelper.IsInitializedで初期化が終わっているか確認する、WebCamTextureToMatHelper.IsPlayingで動作中か確認するなどしてはどうでしょうか?

投稿2020/08/18 03:26

YAmaGNZ

総合スコア10222

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

navesanta

2020/08/18 05:03

Debug.Log ("OnWebCamTextureToMatHelperInitialized"); は実行されていません。 イベントが発生していない可能性はあるので 試しに_webCamTextureToMatHelper.Initialize();を OnWebCamTextureToMatHelperInitialized();に変更してみました。 すると NullReferenceException: Object reference not set to an instance of an object が発生しました。 箇所は _texture = new Texture2D (webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); です。 イベントは発生しようとしているが上記エラーで実行できていなのかもしれません。 上記がNullになるのは var webCamTextureMat = _webCamTextureToMatHelper.GetMat(); でwebCamTextureMatがNullなのかもしれません。もう少し色々試してみます。
YAmaGNZ

2020/08/18 05:08

_webCamTextureToMatHelper.Initialize(); を呼んで、Updateで何もしない場合は Debug.Log ("OnWebCamTextureToMatHelperInitialized"); が実行されますか? もし実行されるようであれば実行順番の問題かと思います。
navesanta

2020/08/18 05:14

試してみました。 Debug.Log ("OnWebCamTextureToMatHelperInitialized");は実行されないです。
YAmaGNZ

2020/08/18 05:29

改めてソースをみるとWebCamTextureToMatHelper.IsPlayingでチェックしてましたね。 そうなるとOnWebCamTextureToMatHelperInitializedが呼ばれていないのが原因のようですね。 申し訳ありませんが、私には何故OnWebCamTextureToMatHelperInitializedが呼ばれていないのかは分かりかねます。
navesanta

2020/08/18 07:20

私には何故OnWebCamTextureToMatHelperInitializedが呼ばれていないのかは分かりかねます。 原因分かりました。 初歩的なミスでインスペクタ上でのOnWebCamTextureToMatHelperInitializedの 設定がしてありませんでした。 コード上のみでイベントが発生するのではなくインスペクタでの設定も 必要でした。
navesanta

2020/08/18 07:20

アドバイスありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問