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

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

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

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

OpenCV

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

Unity

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

Q&A

0回答

1385閲覧

UnityでOpenCVを使うとInvaild textureというエラーが表示される

Suchmos23

総合スコア6

C#

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

OpenCV

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

Unity

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

0グッド

0クリップ

投稿2020/11/19 10:49

やりたいこと

Vive pro eyeのHMDに付随しているcameraから画像をTexture2D形式で取得して,その画像データをOpenCVを使って画像処理を行いたいです.

###直面している問題
Invaild texture UnityEngine.Texture2D:GetPixels32()というエラーが毎秒表示されます.
取得した画像も真っ黒になっています.
何が原因でInvaild textureになっているかが分からないです.

###ソースコード

C#

1//======= Copyright (c) Valve Corporation, All rights reserved. =============== 2using UnityEngine; 3using OpenCVForUnity; 4using OpenCVForUnity.UnityUtils; 5using OpenCVForUnity.ImgprocModule; 6using OpenCVForUnity.CoreModule; 7using UnityEngine.UI; 8 9namespace Valve.VR.Extras 10{ 11 public class SteamVR_TestTrackedCamera_cp : MonoBehaviour 12 { 13 public Material material; 14 public Transform target; 15 public bool undistorted = true; 16 public bool cropped = true; 17 18 19 private readonly static Scalar blue_lower = new Scalar(110, 50, 50);// 20 private readonly static Scalar blue_upper = new Scalar(130, 255, 255);// 21 22 23 Mat imgMat; 24 Mat imgMat2; 25 Mat hsvMat; 26 Mat blurMat; 27 Mat binMat; 28 Mat Hierarchy; 29 30 Texture2D texture; 31 Texture2D texture_out; 32 33 //List<MatOfPoint> Contours; 34 35 36 private void OnEnable() 37 { 38 Debug.Log("OnEnable");//走ってる 39 40 // The video stream must be symmetrically acquired and released in 41 // order to properly disable the stream once there are no consumers. 42 SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted); 43 source.Acquire(); 44 45 // Auto-disable if no camera is present. 46 if (!source.hasCamera) 47 enabled = false; 48 } 49 50 private void OnDisable() 51 { 52 Debug.Log("OnDisable");//走ってる 53 // Clear the texture when no longer active. 54 material.mainTexture = null; 55 56 // The video stream must be symmetrically acquired and released in 57 // order to properly disable the stream once there are no consumers. 58 SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted); 59 source.Release(); 60 } 61 62 private void Update() 63 { 64 SteamVR_TrackedCamera.VideoStreamTexture source = SteamVR_TrackedCamera.Source(undistorted); 65 texture = source.texture; 66 67 68 //画像処理 69 //Matを生成する 70 imgMat = new Mat(texture.height, texture.width, CvType.CV_8UC3); 71 72 //Texture→Mat 73 OpenCVForUnity.UnityUtils.Utils.texture2DToMat(texture, imgMat,true,0);//おそらくこいつが原因かも 74 75 //ガウシアンフィルターによる平滑化 76 imgMat2 = new Mat(texture.height, texture.width, CvType.CV_8UC3); 77 Imgproc.GaussianBlur(imgMat, imgMat2, new Size(5, 5), 0); 78 79 // BGRからHSVに変換 80 hsvMat = new Mat(texture.height, texture.width, CvType.CV_8UC3);//CV_8UC3?? 81 Imgproc.cvtColor(imgMat2, hsvMat, Imgproc.COLOR_RGB2HSV); 82 83 //medianBlur化 84 blurMat = new Mat(texture.height, texture.width, CvType.CV_8UC3);//CV_8CU3?? 85 Imgproc.medianBlur(hsvMat, blurMat, 3); 86 87 //2値化 88 binMat = new Mat(texture.height, texture.width, CvType.CV_8UC3);//CV_8CU3?? 89 Core.inRange(blurMat, blue_lower, blue_upper, binMat); 90 91 92 texture_out = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);//元のMatに輪郭が描かれたものが表示される 93 OpenCVForUnity.UnityUtils.Utils.matToTexture2D(imgMat2, texture_out); 94 95 96 if (texture == null) 97 { 98 99 return; 100 } 101 102 // Apply the latest texture to the material. This must be performed 103 // every frame since the underlying texture is actually part of a ring 104 // buffer which is updated in lock-step with its associated pose. 105 // (You actually really only need to call any of the accessors which 106 // internally call Update on the SteamVR_TrackedCamera.VideoStreamTexture). 107 108 gameObject.GetComponent<Renderer>().material.mainTexture = texture_out;//追記した 109 110 // Adjust the height of the quad based on the aspect to keep the texels square. 111 float aspect = (float)texture.width / texture.height; 112 113 // The undistorted video feed has 'bad' areas near the edges where the original 114 // square texture feed is stretched to undo the fisheye from the lens. 115 // Therefore, you'll want to crop it to the specified frameBounds to remove this. 116 if (cropped) 117 { 118 VRTextureBounds_t bounds = source.frameBounds; 119 material.mainTextureOffset = new Vector2(bounds.uMin, bounds.vMin); 120 121 float du = bounds.uMax - bounds.uMin; 122 float dv = bounds.vMax - bounds.vMin; 123 material.mainTextureScale = new Vector2(du, dv); 124 125 aspect *= Mathf.Abs(du / dv); 126 } 127 else 128 { 129 material.mainTextureOffset = Vector2.zero; 130 material.mainTextureScale = new Vector2(1, -1); 131 } 132 133 //target.localScale = new Vector3(1, 1.0f / aspect, 1);//ここは変えなくておk? 134 135 // Apply the pose that this frame was recorded at. 136 if (source.hasTracking) 137 { 138 139 const float ProjectionZ = 1.0f; 140 Vector2 ProjectionScale = GetProjectionScale(source); 141 Vector2 LocalScale = new Vector2(10.0f * ProjectionZ / ProjectionScale.x, 10.0f * ProjectionZ / ProjectionScale.y); 142 143 target.localScale = new Vector3(LocalScale.x, LocalScale.y, 1.0f); 144 145 146 147 SteamVR_Utils.RigidTransform rigidTransform = source.transform; 148 target.localPosition = rigidTransform.TransformPoint(new Vector3(0.0f, 0.0f, ProjectionZ)); 149 target.localRotation = rigidTransform.rot; 150 } 151 } 152 153 // プロジェクションのスケールを取得する 154 static Vector2 GetProjectionScale(SteamVR_TrackedCamera.VideoStreamTexture source) 155 { 156 Valve.VR.CVRTrackedCamera trackedCamera = Valve.VR.OpenVR.TrackedCamera; 157 if (trackedCamera == null) return Vector2.one; 158 159 // スケール値を取得するだけなので、Near/Farの値は何でも構わない 160 const float Near = 1.0f; 161 const float Far = 100.0f; 162 163 //Valve.VR.HmdMatrix44_t ProjectionMatrix = new Valve.VR.HmdMatrix44_t(); 164 165 /* 166 if (trackedCamera.GetCameraProjection(source.deviceIndex,0,source.frameType,Near, Far, ref ProjectionMatrix) != 167 Valve.VR.EVRTrackedCameraError.None)//コピペしたままだとエラーを吐かれる。引数が足りていないから。nCameraIndexの部分が足りていない。とりあえず、1にしてみた。 168 { 169 return Vector2.one; 170 } 171 */ 172 173 174 return new Vector2(10, 10); 175 176 177 } 178 179 } 180}

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

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

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

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

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

ayousanz

2020/11/21 19:50

何行目でエラーが出ているとか,他のエラーなどはなかったのでしょうか.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問