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}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。