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

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

新規登録して質問してみよう
ただいま回答率
85.48%
ARCore

ARCoreは、グーグル社が提供しているAndroid向けのAR(拡張現実)フレームワークです。スマホ内蔵のカメラとIMUセンサー(慣性計測装置)を使って、モーショントラッキングや水平面の検出、光源の推測を行い、ARを実現します。

C#

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

OpenCV

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

Unity

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

Q&A

0回答

1041閲覧

ARcoreとOpencv for Unity を組み合わせたい

Suchmos23

総合スコア6

ARCore

ARCoreは、グーグル社が提供しているAndroid向けのAR(拡張現実)フレームワークです。スマホ内蔵のカメラとIMUセンサー(慣性計測装置)を使って、モーショントラッキングや水平面の検出、光源の推測を行い、ARを実現します。

C#

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

OpenCV

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

Unity

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

0グッド

0クリップ

投稿2020/08/07 06:11

実現したいこと

OpenCV for Unityを用いて青色の部分の面積が半分になったことを認識したら、ARcoreを利用してCubeオブジェエクトを出現させたいです。

イメージ説明

今現在できていること

  • ARcoreを利用して3Dオブジェクトを重畳表示
  • OpenCV for Unityを用いて青色部分の認識と青色部分の面積が半分になったことの認識

できていないこと(詰まっているとこ)

今現在できていることで述べた二つの処理を組み合わせることができていません。

青色部分の認識とその面積が半分になったことを認識するスクリプト(OpenCV for Unity)

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using OpenCVForUnity; 5using OpenCVForUnity.UnityUtils; 6using OpenCVForUnity.ImgprocModule; 7using OpenCVForUnity.CoreModule; 8using UnityEngine.UI; 9 10 11public class findcontuor : MonoBehaviour 12{ 13 WebCamTexture webCamTexture; 14 15 16 17 [SerializeField] 18 public GameObject splash; 19 20 [SerializeField] 21 public GameObject cube; 22 23 public int Width = 640; 24 public int Height = 480; 25 public int FPS = 30; 26 27 28 29 private readonly static Scalar blue_lower = new Scalar(110, 50, 50);//(H,S,V)?? 30 private readonly static Scalar blue_upper = new Scalar(130, 255, 255);//(H,S,V)?? 31 32 public GameObject text_object = null; // Textオブジェクト 33 34 35 36 // もしWebCamでやりたかったらStart()でWebCamを起動します。 37 // 画像を読み込む場合は同じくStart()でテクスチャに画像を読み込ませれば以下同じように動きます。 38 void Start() 39 { 40 41 WebCamDevice[] devices = WebCamTexture.devices; 42 webCamTexture = new WebCamTexture(devices[0].name, Width, Height, FPS); 43 GetComponent<Renderer>().material.mainTexture = webCamTexture; 44 webCamTexture.Play(); 45 46 } 47 48 49 void Update() 50 { 51 52 //Matを生成する 53 Mat imgMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC3); 54 55 //Texture→Mat 56 Utils.webCamTextureToMat(webCamTexture, imgMat); 57 58 59 //ガウシアンフィルターによる平滑化 60 Mat imgMat2 = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC3); 61 Imgproc.GaussianBlur(imgMat, imgMat2, new Size(5, 5), 0); 62 63 // BGRからHSVに変換 64 Mat hsvMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC3);//CV_8UC3?? 65 Imgproc.cvtColor(imgMat2, hsvMat, Imgproc.COLOR_RGB2HSV); 66 67 //medianBlur化 68 Mat blurMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC3);//CV_8CU3?? 69 Imgproc.medianBlur(hsvMat, blurMat, 3); 70 71 // 画像の平坦化 72 Imgproc.equalizeHist(blurMat, blurMat); 73 74 75 //2値化 76 Mat binMat = new Mat(webCamTexture.height, webCamTexture.width, CvType.CV_8UC3);//CV_8CU3?? 77 Core.inRange(blurMat, blue_lower, blue_upper, binMat); 78 79 80 81 //輪郭を抽出して抽出した輪郭を描く 82 83 List<MatOfPoint> Contours = new List<MatOfPoint>(); 84 Mat Hierarchy = new Mat(); 85 86 Imgproc.findContours(binMat, Contours, Hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);//2値化したMatに対して輪郭を見つける 87 88 89 // オブジェクトからTextコンポーネントを取得 90 Text score_text = text_object.GetComponent<Text>(); 91 92 //最大領域を見つける 93 int maxIdx = 0; 94 double maxArea = 0.0; 95 96 for (int i = 0; i < Contours.Count; i++) 97 { 98 double tmpArea = Imgproc.contourArea(Contours[i]); 99 if (maxArea < tmpArea) 100 { 101 maxArea = tmpArea; 102 maxIdx = i; 103 } 104 } 105 106 107 score_text.text = "maxArea:" + maxArea; 108 109 110 void cut() 111 { 112 if (maxArea > 3000 && maxArea < 6000) 113 { 114 Imgproc.putText(imgMat, "Cut!", new Point(5, imgMat.rows() - 70), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 0, 0, 255), 2, Imgproc.LINE_AA, false); 115 //splash = Instantiate(splash, new Vector3(-20, 0, 10), Quaternion.identity); 116 //cube = Instantiate(cube, new Vector3(-20,0,50), Quaternion.identity); 117 } 118 } 119 120 121 122 for (int i = 0; i < Contours.Count; i++) 123 { 124 Imgproc.drawContours(imgMat, Contours, i, new Scalar(255, 0, 0), 2, 8, Hierarchy, 0, new Point());//輪郭を描くのは元のMatに対して描く 125 } 126 127 128 Texture2D texture_out = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);//元のMatに輪郭が描かれたものが表示される 129 Utils.matToTexture2D(imgMat, texture_out); 130 gameObject.GetComponent<Renderer>().material.mainTexture = texture_out; 131 132 133 134 } 135} 136

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問