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

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

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

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

OpenCV

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

Unity

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

Q&A

1回答

2593閲覧

ARマーカーの2点間の中心座標を求めたい。

tart

総合スコア0

C#

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

OpenCV

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

Unity

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

0グッド

0クリップ

投稿2021/12/02 05:57

Unityを用いてARUCOマーカーで2点間の中心点を求めたいです。
図の矢印部分を求めようとしています。二つ目のプログラムみたいなことをしたいのですが学が浅くどのように書けばよいかお聞きしたく質問させていただきました。

イメージ説明

C#

1for(int i=0;i</*認識されたマーカーの数*/i++) 2{ 3 Vector3 Apos = maker[i].transform.position; 4 Vector3 center = (maker[i].transform.position +maker[i+1].transform.position) * 0.5f; 5 6 } 7

C#

1using UnityEngine; 2using OpenCvSharp; 3using OpenCvSharp.Aruco; 4using UnityEngine.UI; 5using System.Collections.Generic; 6using System.Linq; 7using System; 8 9public class AR : 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 23 private void Start() 24 { 25 WebCamDevice[] devices = WebCamTexture.devices; 26 _webcamTexture = new WebCamTexture(devices[0].name, this._width, this._height, this._fps); 27 _webcamTexture.Play(); 28 29 30 } 31 32 33 void OnDestroy() 34 { 35 if (_webcamTexture != null) 36 { 37 if (_webcamTexture.isPlaying) _webcamTexture.Stop(); 38 _webcamTexture = null; 39 } 40 } 41 42 private void Update() 43 => Armaker(_webcamTexture); 44 45 private void Armaker(WebCamTexture tex) 46 { 47 48 ar_dict = CvAruco.GetPredefinedDictionary(dictName); 49 detect_param = DetectorParameters.Create(); 50 Mat cam_frame = OpenCvSharp.Unity.TextureToMat(tex); 51 52 Point2f[][] corners; 53 int[] ids; 54 Point2f[][] reject_Points; 55 56 Mat grayMat = new Mat(); 57 58 Cv2.CvtColor(cam_frame, grayMat, ColorConversionCodes.BGR2GRAY); 59 60 CvAruco.DetectMarkers(cam_frame,grayMat, ar_dict, out corners, out ids, detect_param, out reject_Points); 61 62 if (ids.Length != 0) 63 { 64 CvAruco.DrawDetectedMarkers(cam_frame, corners, ids, new Scalar(0, 255, 0)); 65 var markers = Enumerable.Zip(ids, corners, (i, c) => new { i, c }) 66 .ToDictionary(x => x.i, x => x.c) 67 .OrderBy(i => i.Key); 68 69 List<Point2f> midllePoints = new List<Point2f>(); 70 71 int cnt = 0; 72 foreach (var marker in markers) 73 { 74 var average_X = marker.Value.Average(p => p.X); 75 var average_Y = marker.Value.Average(p => p.Y); 76 77 midllePoints.Add(new Point2f(average_X, average_Y)); 78 Console.WriteLine($"marker{cnt} X:{average_X}, Y:{average_Y}"); 79 cnt++; 80 } 81 82 midllePoints.ForEach(mp => cam_frame.Circle( 83 (int)mp.X, (int)mp.Y, 1, new Scalar(0, 0, 255), 3, LineTypes.AntiAlias)); 84 Cv2.ImShow("marker", cam_frame); 85 86 } 87 _renderer.texture = OpenCvSharp.Unity.MatToTexture(cam_frame); 88 } 89}

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

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

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

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

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

guest

回答1

0

2点の座標のX,Yの差分をとって、差分の1/2の座標を出せばよろしい

X=X1+(X2-X1)/2
Y=Y1+(Y2-Y1)/2

投稿2021/12/02 06:07

編集2021/12/02 06:12
y_waiwai

総合スコア87774

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問