質問編集履歴

2

誤字を治した

2021/12/14 05:48

投稿

himel
himel

スコア0

test CHANGED
File without changes
test CHANGED
@@ -188,10 +188,6 @@
188
188
 
189
189
 
190
190
 
191
-
192
-
193
-
194
-
195
191
  cnt++;
196
192
 
197
193
 
@@ -214,9 +210,7 @@
214
210
 
215
211
  var average1_Y = midllePoints[0].Y + (midllePoints[1].Y - midllePoints[0].Y) / 2;
216
212
 
217
- // var average1_Z = 0;
213
+
218
-
219
-
220
214
 
221
215
  center.Add(new Point2f(average1_X, average1_Y));
222
216
 

1

ソースコードを追加した

2021/12/14 05:48

投稿

himel
himel

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,279 @@
1
1
  UnityとOpencvを用いてARUCOマーカー上にcudeを出現させたいのですが、どうやってできるでしょうか?
2
2
 
3
3
  RawImageをつかってそこにカメラ画像を写してやっています。
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+ ```ここに言語を入力
12
+
13
+ using UnityEngine;
14
+
15
+ using OpenCvSharp;
16
+
17
+ using OpenCvSharp.Aruco;
18
+
19
+ using UnityEngine.UI;
20
+
21
+ using System.Collections.Generic;
22
+
23
+ using System.Linq;
24
+
25
+
26
+
27
+ public class AR1 : MonoBehaviour
28
+
29
+ {
30
+
31
+
32
+
33
+ [SerializeField] private RawImage _renderer;
34
+
35
+
36
+
37
+ private int _width = 1920;
38
+
39
+ private int _height = 1080;
40
+
41
+ private int _fps = 30;
42
+
43
+ private WebCamTexture _webcamTexture;
44
+
45
+
46
+
47
+ private const PredefinedDictionaryName dictName = PredefinedDictionaryName.Dict6X6_250;
48
+
49
+ private Dictionary ar_dict;
50
+
51
+ private DetectorParameters detect_param;
52
+
53
+ private Vector3 center1;
54
+
55
+ private Vector3 t;
56
+
57
+ public GameObject cude;
58
+
59
+
60
+
61
+
62
+
63
+ private void Start()
64
+
65
+ {
66
+
67
+ WebCamDevice[] devices = WebCamTexture.devices;
68
+
69
+ _webcamTexture = new WebCamTexture(devices[0].name, this._width, this._height, this._fps);
70
+
71
+ _webcamTexture.Play();
72
+
73
+
74
+
75
+
76
+
77
+ }
78
+
79
+
80
+
81
+
82
+
83
+ void OnDestroy()
84
+
85
+ {
86
+
87
+ if (_webcamTexture != null)
88
+
89
+ {
90
+
91
+ if (_webcamTexture.isPlaying) _webcamTexture.Stop();
92
+
93
+ _webcamTexture = null;
94
+
95
+ }
96
+
97
+ }
98
+
99
+
100
+
101
+ private void Update()
102
+
103
+ => Armaker(_webcamTexture);
104
+
105
+
106
+
107
+ private void Armaker(WebCamTexture tex)
108
+
109
+ {
110
+
111
+
112
+
113
+ ar_dict = CvAruco.GetPredefinedDictionary(dictName);
114
+
115
+ detect_param = DetectorParameters.Create();
116
+
117
+ Mat cam_frame = OpenCvSharp.Unity.TextureToMat(tex);
118
+
119
+
120
+
121
+
122
+
123
+ Point2f[][] corners; //ARマーカのカドの座標
124
+
125
+ int[] ids; //検出されたARマーカのID
126
+
127
+ Point2f[][] reject_Points;
128
+
129
+
130
+
131
+ Mat grayMat = new Mat();
132
+
133
+
134
+
135
+ Cv2.CvtColor(cam_frame, grayMat, ColorConversionCodes.BGR2GRAY);
136
+
137
+
138
+
139
+ CvAruco.DetectMarkers(cam_frame, grayMat, ar_dict, out corners, out ids, detect_param, out reject_Points);
140
+
141
+
142
+
143
+ if (ids.Length != 0)
144
+
145
+ {
146
+
147
+
148
+
149
+ List<Point2f> midllePoints = new List<Point2f>();
150
+
151
+ List<Point2f> center = new List<Point2f>();
152
+
153
+
154
+
155
+
156
+
157
+ CvAruco.DrawDetectedMarkers(cam_frame, corners, ids, new Scalar(0, 255, 0));
158
+
159
+
160
+
161
+ var markers = Enumerable.Zip(ids, corners, (i, c) => new { i, c })
162
+
163
+ .ToDictionary(x => x.i, x => x.c)
164
+
165
+ .OrderBy(i => i.Key);
166
+
167
+
168
+
169
+
170
+
171
+ int cnt = 0;
172
+
173
+ foreach (var marker in markers)
174
+
175
+ {
176
+
177
+ var average_X = marker.Value.Average(p => p.X);
178
+
179
+ var average_Y = marker.Value.Average(p => p.Y);
180
+
181
+
182
+
183
+
184
+
185
+ // マーカーの中心座標を取得
186
+
187
+ midllePoints.Add(new Point2f(average_X, average_Y));
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+ cnt++;
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+
204
+
205
+ int cnt1 = 0;
206
+
207
+ foreach (var t in midllePoints)
208
+
209
+ {
210
+
211
+
212
+
213
+ var average1_X = midllePoints[0].X + (midllePoints[1].X - midllePoints[0].X) / 2;
214
+
215
+ var average1_Y = midllePoints[0].Y + (midllePoints[1].Y - midllePoints[0].Y) / 2;
216
+
217
+ // var average1_Z = 0;
218
+
219
+
220
+
221
+ center.Add(new Point2f(average1_X, average1_Y));
222
+
223
+
224
+
225
+ cnt1++;
226
+
227
+
228
+
229
+ }
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ // マーカーの中心座標を描画
238
+
239
+ midllePoints.ForEach(mp => cam_frame.Circle(
240
+
241
+ (int)mp.X, (int)mp.Y, 1, new Scalar(0, 0, 255), 3, LineTypes.AntiAlias));
242
+
243
+
244
+
245
+ // マーカー間の中心点を描画
246
+
247
+ center.ForEach(tc => cam_frame.Circle(
248
+
249
+ (int)tc.X, (int)tc.Y, 1, new Scalar(0, 255, 255), 5, LineTypes.AntiAlias));
250
+
251
+
252
+
253
+
254
+
255
+ foreach (var c in center)
256
+
257
+ {
258
+
259
+ t = new Vector3(center[0].X, center[0].Y, 0);
260
+
261
+
262
+
263
+ cude.transform.position = t;
264
+
265
+ }
266
+
267
+
268
+
269
+ }
270
+
271
+ _renderer.texture = OpenCvSharp.Unity.MatToTexture(cam_frame);
272
+
273
+
274
+
275
+ }
276
+
277
+ }
278
+
279
+ ```