UnityとOpencvを用いてARUCOマーカー上にcudeを出現させたいのですが、どうやってできるでしょうか?
RawImageをつかってそこにカメラ画像を写してやっています。
using UnityEngine; using OpenCvSharp; using OpenCvSharp.Aruco; using UnityEngine.UI; using System.Collections.Generic; using System.Linq; public class AR1 : MonoBehaviour { [SerializeField] private RawImage _renderer; private int _width = 1920; private int _height = 1080; private int _fps = 30; private WebCamTexture _webcamTexture; private const PredefinedDictionaryName dictName = PredefinedDictionaryName.Dict6X6_250; private Dictionary ar_dict; private DetectorParameters detect_param; private Vector3 center1; private Vector3 t; public GameObject cude; private void Start() { WebCamDevice[] devices = WebCamTexture.devices; _webcamTexture = new WebCamTexture(devices[0].name, this._width, this._height, this._fps); _webcamTexture.Play(); } void OnDestroy() { if (_webcamTexture != null) { if (_webcamTexture.isPlaying) _webcamTexture.Stop(); _webcamTexture = null; } } private void Update() => Armaker(_webcamTexture); private void Armaker(WebCamTexture tex) { ar_dict = CvAruco.GetPredefinedDictionary(dictName); detect_param = DetectorParameters.Create(); Mat cam_frame = OpenCvSharp.Unity.TextureToMat(tex); Point2f[][] corners; //ARマーカのカドの座標 int[] ids; //検出されたARマーカのID Point2f[][] reject_Points; Mat grayMat = new Mat(); Cv2.CvtColor(cam_frame, grayMat, ColorConversionCodes.BGR2GRAY); CvAruco.DetectMarkers(cam_frame, grayMat, ar_dict, out corners, out ids, detect_param, out reject_Points); if (ids.Length != 0) { List<Point2f> midllePoints = new List<Point2f>(); List<Point2f> center = new List<Point2f>(); CvAruco.DrawDetectedMarkers(cam_frame, corners, ids, new Scalar(0, 255, 0)); var markers = Enumerable.Zip(ids, corners, (i, c) => new { i, c }) .ToDictionary(x => x.i, x => x.c) .OrderBy(i => i.Key); int cnt = 0; foreach (var marker in markers) { var average_X = marker.Value.Average(p => p.X); var average_Y = marker.Value.Average(p => p.Y); // マーカーの中心座標を取得 midllePoints.Add(new Point2f(average_X, average_Y)); cnt++; } int cnt1 = 0; foreach (var t in midllePoints) { var average1_X = midllePoints[0].X + (midllePoints[1].X - midllePoints[0].X) / 2; var average1_Y = midllePoints[0].Y + (midllePoints[1].Y - midllePoints[0].Y) / 2; center.Add(new Point2f(average1_X, average1_Y)); cnt1++; } // マーカーの中心座標を描画 midllePoints.ForEach(mp => cam_frame.Circle( (int)mp.X, (int)mp.Y, 1, new Scalar(0, 0, 255), 3, LineTypes.AntiAlias)); // マーカー間の中心点を描画 center.ForEach(tc => cam_frame.Circle( (int)tc.X, (int)tc.Y, 1, new Scalar(0, 255, 255), 5, LineTypes.AntiAlias)); foreach (var c in center) { t = new Vector3(center[0].X, center[0].Y, 0); cude.transform.position = t; } } _renderer.texture = OpenCvSharp.Unity.MatToTexture(cam_frame); } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/14 02:15
2021/12/14 05:56
2021/12/14 06:05