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

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

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

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

OpenCV

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

Unity

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

Q&A

0回答

1819閲覧

ArUcoマーカーを用いて特定の座標にオブジェクトを表示させたい。

himel

総合スコア0

C#

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

OpenCV

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

Unity

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

0グッド

0クリップ

投稿2021/12/16 07:56

Unityを用いてArUcoマーカー間の中心座標にオブジェクトを表示させたいです。
カメラの映像をRawImageを使って映しています。カメラ映像の中にどうやったらオブジェクトを設置できるのかお聞きしたく質問させていただきました。

1つ目のソースコードで、マーカー間の中心座標を求めてそこにcudeを設置しようと考えているのですが、実行してみるとマーカーを認識した瞬間フリーズしてしまいます。ご教授していただけると幸いです。

C#

1 // マーカーとマーカーの間の中心点 2var average1_X = midllePoints[0].X + (midllePoints[1].X - midllePoints[0].X) / 2; 3var average1_Y = midllePoints[0].Y + (midllePoints[1].Y - midllePoints[0].Y) / 2; 4 5 center.Add(new Point2f(average1_X, average1_Y)); 6 7 Vector3 centerPoint = new Vector3(center[0].X, center[0].Y); 8 cude.transform.position = centerPoint; 9 10 int obj = Cv2.projectPoints(centerPoint, cam_frame); 11 12 13 cnt1++; 14 15 }

C#

1//全体のソースコード 2using UnityEngine; 3using OpenCvSharp; 4using OpenCvSharp.Aruco; 5using UnityEngine.UI; 6using System.Collections.Generic; 7using System.Linq; 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 19 private const PredefinedDictionaryName dictName = PredefinedDictionaryName.Dict6X6_250; 20 private Dictionary ar_dict; 21 private DetectorParameters detect_param; 22 public GameObject cude; 23 24 private int id; 25 private int markerSize; 26 private Vector3 pos; 27 private object tvecs; 28 private object rvecs; 29 private object _objPoints; 30 31 private void Start() 32 { 33 // カメラ設定 34 WebCamDevice[] devices = WebCamTexture.devices; 35 _webcamTexture = new WebCamTexture(devices[0].name, this._width, this._height, this._fps); 36 _webcamTexture.Play(); 37 38 39 } 40 41 42 void OnDestroy() 43 { 44 if (_webcamTexture != null) 45 { 46 if (_webcamTexture.isPlaying) _webcamTexture.Stop(); 47 _webcamTexture = null; 48 } 49 } 50 51 private void Update() 52 => Armaker(_webcamTexture); 53 54 private void Armaker(WebCamTexture tex) 55 { 56 // カメラをテクスチャに載せて表示させる 57 // ARマーカーの設定と描画設定 58 59 ar_dict = CvAruco.GetPredefinedDictionary(dictName); 60 detect_param = DetectorParameters.Create(); 61 Mat cam_frame = OpenCvSharp.Unity.TextureToMat(tex); 62 63 64 Point2f[][] corners; //ARマーカのカドの座標 65 int[] ids; //検出されたARマーカのID 66 Point2f[][] reject_Points; 67 68 Mat grayMat = new Mat(); 69 70 Cv2.CvtColor(cam_frame, grayMat, ColorConversionCodes.BGR2GRAY); 71 72 CvAruco.DetectMarkers(cam_frame, grayMat, ar_dict, out corners, out ids, detect_param, out reject_Points); 73 74 if (ids.Length != 0) 75 { 76       List<Point2f> midllePoints = new List<Point2f>(); 77 List<Point2f> center = new List<Point2f>(); 78 79 CvAruco.DrawDetectedMarkers(cam_frame, corners, ids, new Scalar(0, 255, 0)); 80 81 //ARマーカーの定義 座標とマーカー番号 82 var markers = Enumerable.Zip(ids, corners, (i, c) => new { i, c }) 83 .ToDictionary(x => x.i, x => x.c) 84 .OrderBy(i => i.Key); 85 86 87 int cnt = 0; 88 foreach (var marker in markers) 89 { 90 //マーカー個々の中心座標 91 var average_X = marker.Value.Average(p => p.X); 92 var average_Y = marker.Value.Average(p => p.Y); 93 94 // マーカーの中心座標を取得 95 midllePoints.Add(new Point2f(average_X, average_Y)); 96 97 cnt++; 98 99 } 100 101 102 int cnt1 = 0; 103 foreach (var Points in midllePoints) 104 { 105 106 // マーカーとマーカーの間の中心点 107 var average1_X = midllePoints[0].X + (midllePoints[1].X - midllePoints[0].X) / 2; 108 var average1_Y = midllePoints[0].Y + (midllePoints[1].Y - midllePoints[0].Y) / 2; 109 110 center.Add(new Point2f(average1_X, average1_Y)); 111 112 Vector3 centerPoint = new Vector3(center[0].X, center[0].Y); 113 cude.transform.position = centerPoint; 114 115 int obj = Cv2.projectPoints(centerPoint, cam_frame); 116 117 cnt1++; 118 119 } 120 121 122 // マーカーの中心座標を描画 123 midllePoints.ForEach(mp => cam_frame.Circle( 124 (int)mp.X, (int)mp.Y, 1, new Scalar(0, 0, 255), 3, LineTypes.AntiAlias)); 125 126 // マーカー間の中心点を描画 127 center.ForEach(tc => cam_frame.Circle( 128 (int)tc.X, (int)tc.Y, 1, new Scalar(0, 255, 255), 5, LineTypes.AntiAlias)); 129 130 } 131 132 // RawImageにカメラ画像を描画 133 _renderer.texture = OpenCvSharp.Unity.MatToTexture(cam_frame); 134 } 135} 136 137

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問