質問編集履歴

1

みずらくて申し訳ありませんが関係しているコードを載せさせていただきました。

2019/05/21 12:42

投稿

hsgru3
hsgru3

スコア22

test CHANGED
@@ -1 +1 @@
1
- 動的配列内の構造体の変数アクセス
1
+ 範囲for文がtimer1_Tickで使えるがbutton1_Clickで使えない。
test CHANGED
@@ -14,417 +14,605 @@
14
14
 
15
15
  c++/CLRでWindowsフォームアプリケーションの作成をしています。
16
16
 
17
- その際以下のように範囲for文を使って動的配列の構造体の変数にアクセスしてその変数表示させています
17
+ 以下のように範囲for文を使って動的配列の構造体の変数にアクセスし処理行っています
18
+
18
-
19
+ ```ここに言語を入力
20
+
19
-
21
+ for (auto &i : result_vec) {
22
+
23
+ 処理
24
+
25
+ }
20
26
 
21
27
  ```
22
28
 
23
- private: System::Void show_result(std::vector<bbox_t> result_vec) {
24
-
25
- for (auto &i : result_vec) {
26
-
27
- std::cout << "obj_id = " << i.obj_id << " - x = " << i.x << ", y = " << i.y
28
-
29
- << ", w = " << i.w << ", h = " << i.h
30
-
31
- << ", prob = " << i.prob << std::endl;
32
-
33
-     }
29
+ この関数が以下のソースコード内で```timer1_Tick```の中では正常に動作しますが```button1_Click```の中では動作せず困っています。
30
+
31
+
32
+
33
+ ### 該当のソースコード
34
+
35
+
36
+
37
+ USP.h
38
+
39
+ ```
40
+
41
+ #include <iostream>
42
+
43
+ #include <time.h>
44
+
45
+ #include <fstream>
46
+
47
+ #include <vector>
48
+
49
+ #include "yolo_v2_class.hpp"
50
+
51
+ #include "opencv2\opencv.hpp"
52
+
53
+ #include "pxcsensemanager.h"
54
+
55
+ #include "PXCProjection.h"
56
+
57
+ #include "aufd_v.1.3.h"
58
+
59
+
60
+
61
+ using namespace cv;
62
+
63
+ using namespace std;
64
+
65
+
66
+
67
+ //カラー画像(処理用、保存用)
68
+
69
+ Mat src, dst;
70
+
71
+
72
+
73
+ //座標変換インスタンス
74
+
75
+ PXCProjection *projection = nullptr;
76
+
77
+ const PXCCapture::Sample *sample;
78
+
79
+ PXCSenseManager *senseManager = 0;
80
+
81
+
82
+
83
+ //デプス画像の距離情報格納バッファ
84
+
85
+ vector<unsigned short> depthBuffer;
86
+
87
+
88
+
89
+ 変数の設定
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+ namespace UI {
98
+
99
+
100
+
101
+ using namespace System;
102
+
103
+ using namespace System::ComponentModel;
104
+
105
+ using namespace System::Collections;
106
+
107
+ using namespace System::Windows::Forms;
108
+
109
+ using namespace System::Data;
110
+
111
+ using namespace System::Drawing;
112
+
113
+
114
+
115
+ /// <summary>
116
+
117
+ /// USP の概要
118
+
119
+ /// </summary>
120
+
121
+ public ref class USP : public System::Windows::Forms::Form
122
+
123
+ {
124
+
125
+ public:
126
+
127
+ USP(void)
128
+
129
+ {
130
+
131
+ InitializeComponent();
132
+
133
+ //
134
+
135
+ //TODO: ここにコンストラクター コードを追加します
136
+
137
+ //
138
+
139
+ }
140
+
141
+
142
+
143
+ protected:
144
+
145
+ /// <summary>
146
+
147
+ /// 使用中のリソースをすべてクリーンアップします。
148
+
149
+ /// </summary>
150
+
151
+ ~USP()
152
+
153
+ {
154
+
155
+ if (components)
156
+
157
+ {
158
+
159
+ delete components;
160
+
161
+ }
162
+
163
+ }
164
+
165
+ private: System::Windows::Forms::PictureBox^ pictureBox1;
166
+
167
+ protected:
168
+
169
+ private: System::Windows::Forms::Timer^ timer1;
170
+
171
+ private: System::Windows::Forms::Button^ button1;
172
+
173
+ private: System::Windows::Forms::Button^ button2;
174
+
175
+ private: System::Windows::Forms::Button^ button3;
176
+
177
+ private: System::Windows::Forms::Button^ button4;
178
+
179
+
180
+
181
+ private: System::Windows::Forms::ComboBox^ comboBox1;
182
+
183
+ private: System::Windows::Forms::ComboBox^ comboBox2;
184
+
185
+ private: System::Windows::Forms::Label^ label1;
186
+
187
+ private: System::Windows::Forms::Label^ label2;
188
+
189
+ private: System::ComponentModel::IContainer^ components;
190
+
191
+
192
+
193
+ private:
194
+
195
+ /// <summary>
196
+
197
+ /// 必要なデザイナー変数です。
198
+
199
+ /// </summary>
200
+
201
+
202
+
203
+
204
+
205
+ #pragma region Windows Form Designer generated code
206
+
207
+ /// <summary>
208
+
209
+ /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
210
+
211
+ /// コード エディターで変更しないでください。
212
+
213
+ /// </summary>
214
+
215
+ void InitializeComponent(void)
216
+
217
+ {
218
+
219
+ ~~~~~省略~~~~~
220
+
221
+ #pragma endregion
222
+
223
+ //初期化
224
+
225
+ private: System::Void USP_Load(System::Object^ sender, System::EventArgs^ e) {
226
+
227
+
228
+
229
+ // SenseManagerを生成する
230
+
231
+ senseManager = PXCSenseManager::CreateInstance();
232
+
233
+ if (senseManager == 0) {
234
+
235
+ throw std::runtime_error("SenseManagerの生成に失敗しました");
236
+
237
+ }
238
+
239
+
240
+
241
+ // カラーストリームを有効にする
242
+
243
+ auto sts = senseManager->EnableStream(PXCCapture::StreamType::STREAM_TYPE_COLOR,
244
+
245
+ COLOR_WIDTH, COLOR_HEIGHT, COLOR_FPS);
246
+
247
+ if (sts < PXC_STATUS_NO_ERROR) {
248
+
249
+ throw std::runtime_error("カラーストリームの有効化に失敗しました");
250
+
251
+ }
252
+
253
+
254
+
255
+ // Depthストリームを有効にする
256
+
257
+ sts = senseManager->EnableStream(PXCCapture::StreamType::STREAM_TYPE_DEPTH,
258
+
259
+ DEPTH_WIDTH, DEPTH_HEIGHT, DEPTH_FPS);
260
+
261
+ if (sts < PXC_STATUS_NO_ERROR) {
262
+
263
+ throw std::runtime_error("Depthストリームの有効化に失敗しました");
264
+
265
+ }
266
+
267
+
268
+
269
+ // パイプラインを初期化する
270
+
271
+ sts = senseManager->Init();
272
+
273
+ if (sts < PXC_STATUS_NO_ERROR) {
274
+
275
+ throw std::runtime_error("パイプラインの初期化に失敗しました");
276
+
277
+ }
278
+
279
+
280
+
281
+ // デバイスを取得する
282
+
283
+ auto device = senseManager->QueryCaptureManager()->QueryDevice();
284
+
285
+
286
+
287
+ // 座標変換オブジェクトを作成
288
+
289
+ projection = device->CreateProjection();
290
+
291
+
292
+
293
+ // depthBufferの作成
294
+
295
+ depthBuffer.resize(DEPTH_WIDTH * DEPTH_HEIGHT);
296
+
297
+
298
+
299
+ // 超音波集束装置の初期化
300
+
301
+ hDevice = OpenDevice();
302
+
303
+ DummyOutput(hDevice);
304
+
305
+ StopOutput(hDevice);
306
+
307
+
308
+
309
+ //タイマーの初期化
310
+
311
+ timer1->Interval = 33;
312
+
313
+ timer1->Enabled = true;
314
+
315
+
316
+
317
+ //設定値の呼び出し
318
+
319
+ FILE *fp;
320
+
321
+ SaveData Data;
322
+
323
+ fp = fopen("savedata.dat", "rb");
324
+
325
+ fread(&Data, sizeof(Data), 1, fp);
326
+
327
+ fclose(fp);
328
+
329
+ Frequency = Data.F;
330
+
331
+ Time = Data.T;
332
+
333
+ comboBox1->SelectedIndex = Frequency/10;
334
+
335
+ comboBox2->SelectedIndex = Time - 1;
336
+
337
+ }
338
+
339
+
340
+
341
+ //実行
342
+
343
+ private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
344
+
345
+
346
+
347
+ //描画
348
+
349
+ captureImage();
350
+
351
+
352
+
353
+ //設定値の反映
354
+
355
+ Int32::TryParse(comboBox1->SelectedItem->ToString(), Frequency);
356
+
357
+ Int32::TryParse(comboBox2->SelectedItem->ToString(), Time);
358
+
359
+ }
360
+
361
+
362
+
363
+ //フレーム取得
364
+
365
+ private: System::Void captureImage() {
366
+
367
+
368
+
369
+ // フレームを取得する
370
+
371
+ pxcStatus sts = senseManager->AcquireFrame(false);
372
+
373
+ if (sts < PXC_STATUS_NO_ERROR) {
374
+
375
+ return;
376
+
377
+ }
378
+
379
+
380
+
381
+ // Colorフレームデータを取得する
382
+
383
+ sample = senseManager->QuerySample();
384
+
385
+ if (sample) {
386
+
387
+ // 各データを表示する
388
+
389
+ updateColorImage(sample->color);
390
+
391
+ }
392
+
393
+
394
+
395
+ // Depthフレームデータを取得する
396
+
397
+ if (sample) {
398
+
399
+ // 各データを表示する
400
+
401
+ updateDepthData(sample->depth);
402
+
403
+ }
404
+
405
+
406
+
407
+ // フレームを解放する
408
+
409
+ senseManager->ReleaseFrame();
410
+
411
+
412
+
413
+ //保存用の画像
414
+
415
+ dst = src.clone();
416
+
417
+
418
+
419
+ //イチゴ花検出
420
+
421
+ std::vector<bbox_t> result_vec = detector.detect(dst);
422
+
423
+
424
+
425
+ //花の領域表示
426
+
427
+ draw_boxes(dst, result_vec);
428
+
429
+
430
+
431
+ //花の座標表示
432
+
433
+ //show_result(result_vec);
434
+
435
+
436
+
437
+
438
+
439
+ //描画
440
+
441
+ drawImage(pictureBox1, dst);
442
+
443
+ }
444
+
445
+
446
+
447
+ //描画
448
+
449
+ private: System::Void drawImage(PictureBox^ pic, Mat mat) {
450
+
451
+ ~~~~~~~省略~~~~~~
452
+
453
+ }
454
+
455
+
456
+
457
+ //花の領域表示
458
+
459
+ private: System::Void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec) {
460
+
461
+
462
+
463
+ for (auto &i : result_vec) {
464
+
465
+ rectangle(mat_img, Rect(i.x, i.y, i.w, i.h), Scalar(50, 200, 50), 3);
466
+
467
+ circle(mat_img, cv::Point(i.x+i.w/2, i.y+i.h/2), 5, Scalar(50, 200, 50), -1);
468
+
469
+ }
470
+
471
+ }
472
+
473
+
474
+
475
+ //花の座標表示
476
+
477
+ private: System::Void show_result(std::vector<bbox_t> result_vec) {
478
+
479
+
480
+
481
+ for (auto &i : result_vec) {
482
+
483
+ int X, Y, Z;
484
+
485
+ cv::Point point = { (int)(i.x + i.w / 2), (int)(i.y + i.h / 2) };
486
+
487
+ ColorToDevice(point, &X, &Y, &Z);
488
+
489
+ cout << "(" << X << "," << Y << "," << Z << ") prob = " << (int)(i.prob * 100) << "%" << "(" << point.x << "," << point.y <<")" << endl;
490
+
491
+ }
492
+
493
+ }
494
+
495
+
496
+
497
+ //フレーム更新(カラー)
498
+
499
+ private: System::Void updateColorImage(PXCImage* colorFrame) {
500
+
501
+ ~~~~~~~~省略~~~
502
+
503
+ }
504
+
505
+
506
+
507
+ //フレーム更新(距離)
508
+
509
+ private: System::Void updateDepthData(PXCImage* depthFrame) {
510
+
511
+ ~~~~~~省略~~~~
512
+
513
+ }
514
+
515
+
516
+
517
+ //カラー座標を超音波装置座標に変換する
518
+
519
+ private: System::Void ColorToDevice(cv::Point point, int *dpointx, int *dpointy, int *dpointz)
520
+
521
+ {
522
+
523
+ ~~~~~~~~省略~~~~~
524
+
525
+ }
526
+
527
+
528
+
529
+ //ボタン1 加振開始
530
+
531
+ private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
532
+
533
+
534
+
535
+ //イチゴ花検出
536
+
537
+ std::vector<bbox_t> result_vec = detector.detect(dst);
538
+
539
+ cv::Point point;
540
+
541
+
542
+
543
+ //順番に加振
544
+
545
+ for (auto &i : result_vec) {
546
+
547
+ int X, Y, Z;
548
+
549
+ point = {(int)(i.x + i.w/ 2), (int)(i.y + i.h/ 2)};
550
+
551
+ ColorToDevice(point, &X, &Y, &Z);
552
+
553
+ StartOutput(hDevice, X, Y, Z, P, Frequency); //出力
554
+
555
+ cout << "出力 (" << X << "," << Y << "," << Z << ")" << endl;
556
+
557
+ Sleep(Time * 1000);
558
+
559
+ StopOutput(hDevice);
560
+
561
+ cout << "停止" << endl;
562
+
563
+ }
564
+
565
+ }
566
+
567
+
568
+
569
+ //ボタン2 ストップ
570
+
571
+ private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
572
+
573
+ ~~~~~省略~~~
574
+
575
+ }
576
+
577
+
578
+
579
+ //ボタン3 画像の保存
580
+
581
+ private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
582
+
583
+
584
+
585
+ ~~~~~~~省略~~~~~~~~~~
586
+
587
+ }
588
+
589
+
590
+
591
+ //ボタン4 設定値の保存
592
+
593
+ private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
594
+
595
+ ~~~~~~~~~~省略~~~~~~
34
596
 
35
597
  }
36
598
 
599
+
600
+
601
+ //マウスイベント
602
+
603
+ private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
604
+
605
+ ~~~~~~~~~~~省略~~~~
606
+
607
+ }
608
+
609
+ };
610
+
611
+ }
612
+
37
613
  ```
38
614
 
39
- この関数が以下のソースコード内で```timer1_Tick```の中では正常に動作しますが```button1_Click```の中では動作せず困っています。
615
+
40
-
41
-
42
-
43
- ### 該当のソースコード
44
-
45
-
46
-
47
- USP.h
48
-
49
- ```
50
-
51
- #pragma once
52
-
53
- #pragma warning(disable: 4819)
54
-
55
- #pragma warning(disable: 4793)
56
-
57
- #pragma warning(disable: 4996)
58
-
59
- #pragma warning(disable: 4099)
60
-
61
-
62
-
63
- #include <iostream>
64
-
65
- #include <time.h>
66
-
67
- #include <string>
68
-
69
- #include <fstream>
70
-
71
- #include <vector>
72
-
73
-
74
-
75
- ~~~省略~~~~
76
-
77
-
78
-
79
- #pragma endregion
80
-
81
- private: System::Void USP_Load(System::Object^ sender, System::EventArgs^ e) {
82
-
83
-
84
-
85
- // SenseManagerを生成する
86
-
87
- senseManager = PXCSenseManager::CreateInstance();
88
-
89
- if (senseManager == 0) {
90
-
91
- throw std::runtime_error("SenseManagerの生成に失敗しました");
92
-
93
- }
94
-
95
-
96
-
97
- // カラーストリームを有効にする
98
-
99
- auto sts = senseManager->EnableStream(PXCCapture::StreamType::STREAM_TYPE_COLOR,
100
-
101
- COLOR_WIDTH, COLOR_HEIGHT, COLOR_FPS);
102
-
103
- if (sts < PXC_STATUS_NO_ERROR) {
104
-
105
- throw std::runtime_error("カラーストリームの有効化に失敗しました");
106
-
107
- }
108
-
109
-
110
-
111
- // Depthストリームを有効にする
112
-
113
- sts = senseManager->EnableStream(PXCCapture::StreamType::STREAM_TYPE_DEPTH,
114
-
115
- DEPTH_WIDTH, DEPTH_HEIGHT, DEPTH_FPS);
116
-
117
- if (sts < PXC_STATUS_NO_ERROR) {
118
-
119
- throw std::runtime_error("Depthストリームの有効化に失敗しました");
120
-
121
- }
122
-
123
-
124
-
125
- // パイプラインを初期化する
126
-
127
- sts = senseManager->Init();
128
-
129
- if (sts < PXC_STATUS_NO_ERROR) {
130
-
131
- throw std::runtime_error("パイプラインの初期化に失敗しました");
132
-
133
- }
134
-
135
-
136
-
137
- // デバイスを取得する
138
-
139
- auto device = senseManager->QueryCaptureManager()->QueryDevice();
140
-
141
-
142
-
143
- // 座標変換オブジェクトを作成
144
-
145
- projection = device->CreateProjection();
146
-
147
-
148
-
149
- // depthBufferの作成
150
-
151
- depthBuffer.resize(DEPTH_WIDTH * DEPTH_HEIGHT);
152
-
153
-
154
-
155
- // 超音波集束装置の初期化
156
-
157
- hDevice = OpenDevice();
158
-
159
- DummyOutput(hDevice);
160
-
161
- StopOutput(hDevice);
162
-
163
- printf("デバイスオープン\n");
164
-
165
-
166
-
167
- //タイマーの初期化
168
-
169
- timer1->Interval = 33;
170
-
171
- timer1->Enabled = true;
172
-
173
-
174
-
175
- }
176
-
177
-
178
-
179
- //実行
180
-
181
- private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
182
-
183
-
184
-
185
- //描画
186
-
187
- captureImage();
188
-
189
- }
190
-
191
-
192
-
193
- //フレーム取得
194
-
195
- private: System::Void captureImage() {
196
-
197
-
198
-
199
- // フレームを取得する
200
-
201
- pxcStatus sts = senseManager->AcquireFrame(false);
202
-
203
- if (sts < PXC_STATUS_NO_ERROR) {
204
-
205
- return;
206
-
207
- }
208
-
209
-
210
-
211
- // Colorフレームデータを取得する
212
-
213
- sample = senseManager->QuerySample();
214
-
215
- if (sample) {
216
-
217
- // 各データを表示する
218
-
219
- updateColorImage(sample->color);
220
-
221
- }
222
-
223
-
224
-
225
- // Depthフレームデータを取得する
226
-
227
- if (sample) {
228
-
229
- // 各データを表示する
230
-
231
- updateDepthData(sample->depth);
232
-
233
- }
234
-
235
-
236
-
237
- // フレームを解放する
238
-
239
- senseManager->ReleaseFrame();
240
-
241
-
242
-
243
- //イチゴ花検出
244
-
245
- std::vector<bbox_t> result_vec = detector.detect(colorImage);
246
-
247
-
248
-
249
- //花の領域表示
250
-
251
- draw_boxes(colorImage, result_vec);
252
-
253
-
254
-
255
- //花の座標表示
256
-
257
- //show_result(result_vec);
258
-
259
-
260
-
261
-
262
-
263
- //描画
264
-
265
- drawImage(pictureBox1, colorImage);
266
-
267
- }
268
-
269
-
270
-
271
- //描画
272
-
273
- private: System::Void drawImage(PictureBox^ pic, Mat mat) {
274
-
275
-
276
-
277
- }
278
-
279
-
280
-
281
- //花の領域表示
282
-
283
- private: System::Void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec) {
284
-
285
- for (auto &i : result_vec) {
286
-
287
- cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), cv::Scalar(50, 200, 50), 3);
288
-
289
- }
290
-
291
- }
292
-
293
-
294
-
295
- //花の座標表示
296
-
297
- private: System::Void show_result(std::vector<bbox_t> result_vec) {
298
-
299
- for (auto &i : result_vec) {
300
-
301
- std::cout << "obj_id = " << i.obj_id << " - x = " << i.x << ", y = " << i.y
302
-
303
- << ", w = " << i.w << ", h = " << i.h
304
-
305
- << ", prob = " << i.prob << std::endl;
306
-
307
- }
308
-
309
- }
310
-
311
- //フレーム更新(カラー)
312
-
313
- private: System::Void updateColorImage(PXCImage* colorFrame) {
314
-
315
-
316
-
317
- }
318
-
319
-
320
-
321
- //フレーム更新(距離)
322
-
323
- private: System::Void updateDepthData(PXCImage* depthFrame) {
324
-
325
-
326
-
327
- }
328
-
329
-
330
-
331
- // デプス座標を超音波装置座標に変換する
332
-
333
- private: System::Void DepthToDevice(cv::Point point, int *dpointx, int *dpointy, int *dpointz)
334
-
335
- {
336
-
337
-
338
-
339
- }
340
-
341
-
342
-
343
-
344
-
345
- //ボタン1 加振開始
346
-
347
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
348
-
349
- Start();
350
-
351
- }
352
-
353
- private: System::Void Start() {
354
-
355
- printf("出力\n");
356
-
357
-
358
-
359
- //イチゴ花検出
360
-
361
- std::vector<bbox_t> result_vec = detector.detect(colorImage);
362
-
363
- cv::Point cpoint;
364
-
365
- for (auto &i : result_vec) {
366
-
367
- int X, Y, Z;
368
-
369
- cpoint.x = (i.x + i.w) / 2;
370
-
371
- cpoint.y = (i.y + i.h) / 2;
372
-
373
- DepthToDevice(cpoint, &X, &Y, &Z);
374
-
375
- StartOutput(hDevice, X, Y, Z, P, F); //出力
376
-
377
- Sleep(200);
378
-
379
- StopOutput(hDevice); // 停止
380
-
381
- }
382
-
383
- }
384
-
385
- //ボタン2 ストップ
386
-
387
- private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
388
-
389
- StopOutput(hDevice); // 停止
390
-
391
- printf("停止\n");
392
-
393
- }
394
-
395
- };
396
-
397
- }
398
-
399
- ```
400
-
401
-
402
-
403
- main.cpp
404
-
405
- ```
406
-
407
- #include "USP.h"
408
-
409
-
410
-
411
- using namespace UI;
412
-
413
-
414
-
415
- [STAThreadAttribute]
416
-
417
- int main() {
418
-
419
- USP ^ui = gcnew USP();
420
-
421
- ui->ShowDialog();
422
-
423
- return 0;
424
-
425
- }
426
-
427
- ```
428
616
 
429
617
 
430
618
 
@@ -432,7 +620,7 @@
432
620
 
433
621
 
434
622
 
435
- ```timer1_Tick```で動作して```button1_Click```で動作しない理由がよくわかりません。
623
+ 範囲for文が```timer1_Tick```で動作して```button1_Click```で動作しない理由がよくわかりません。
436
624
 
437
625
  別の書き方でもいいので教えていただきたいです。
438
626