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

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

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

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

OpenCV

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

Unity

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

Q&A

0回答

1725閲覧

ArUcoマーカーの上にオブジェクトを置きたい

himel

総合スコア0

C#

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

OpenCV

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

Unity

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

0グッド

0クリップ

投稿2021/12/20 10:50

OpenCV plus Unityを用いて、ArUcoマーカー上にオブジェクトを配置したいです。
UIのRawImageにカメラ映像を映してパソコンの内カメラで読み込ませています。
cam_frameの中でどうやってオブジェクトを置いたら良いか、Vector3は使えるのかお聞きしたいです。ご教示していただけると幸いです。

C#

1using UnityEngine; 2using OpenCvSharp; 3using OpenCvSharp.Aruco; 4using UnityEngine.UI; 5using System.Collections.Generic; 6using System.Linq; 7 8 9public class AR1 : MonoBehaviour 10{ 11 12 [SerializeField] private RawImage _renderer; 13 14 private int _width = 1920; 15 private int _height = 1080; 16 private int _fps = 30; 17 private WebCamTexture _webcamTexture; 18 private const PredefinedDictionaryName dictName = PredefinedDictionaryName.Dict6X6_250; 19 private Dictionary ar_dict; 20 private DetectorParameters detect_param; 21 private void Start() 22 { 23 // カメラ設定 24 WebCamDevice[] devices = WebCamTexture.devices; 25 _webcamTexture = new WebCamTexture(devices[0].name, this._width, this._height, this._fps); 26 _webcamTexture.Play(); 27 28 29 } 30 31 32 void OnDestroy() 33 { 34 if (_webcamTexture != null) 35 { 36 if (_webcamTexture.isPlaying) _webcamTexture.Stop(); 37 _webcamTexture = null; 38 } 39 } 40 41 private void Update() 42 => Armaker(_webcamTexture); 43 44 private void Armaker(WebCamTexture tex) 45 { 46 // カメラをテクスチャに載せて表示させる 47 // ARマーカーの設定と描画設定 48 49 ar_dict = CvAruco.GetPredefinedDictionary(dictName); 50 detect_param = DetectorParameters.Create(); 51 Mat cam_frame = OpenCvSharp.Unity.TextureToMat(tex); 52 53 54 Point2f[][] corners; //ARマーカのカドの座標 55 int[] ids; //検出されたARマーカのID 56 Point2f[][] reject_Points; 57 58 59 Mat grayMat = new Mat(); 60 61 Cv2.CvtColor(cam_frame, grayMat, ColorConversionCodes.BGR2GRAY); 62 63 CvAruco.DetectMarkers(cam_frame, grayMat, ar_dict, out corners, out ids, detect_param, out reject_Points); 64 65 if (ids.Length != 0) 66 { 67 List<Point2f> midllePoints = new List<Point2f>(); 68 69 CvAruco.DrawDetectedMarkers(cam_frame, corners, ids, new Scalar(0, 255, 0)); 70 71 //ARマーカーの定義 座標とマーカー番号 72 var markers = Enumerable.Zip(ids, corners, (i, c) => new { i, c }) 73 .ToDictionary(x => x.i, x => x.c) 74 .OrderBy(i => i.Key); 75 76 int cnt = 0; 77 foreach (var marker in markers) 78 { 79 //マーカー個々の中心座標 80 var average_X = marker.Value.Average(p => p.X); 81 var average_Y = marker.Value.Average(p => p.Y); 82 83 // マーカーの中心座標を取得 84 midllePoints.Add(new Point2f(average_X, average_Y)); 85 86 87 cnt++; 88 89 } 90 91 92 // マーカーの中心座標を描画 93 midllePoints.ForEach(mp => cam_frame.Circle( 94 (int)mp.X, (int)mp.Y, 1, new Scalar(0, 0, 255), 3, LineTypes.AntiAlias)); 95 } 96 97 // RawImageにカメラ画像を描画 98 _renderer.texture = OpenCvSharp.Unity.MatToTexture(cam_frame,); 99 } 100 101}

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問