質問編集履歴
4
編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -589,3 +589,13 @@
|
|
589
589
|
|
590
590
|
|
591
591
|
```
|
592
|
+
|
593
|
+
|
594
|
+
|
595
|
+
<編集3>
|
596
|
+
|
597
|
+
編集2で載せましたコードがなぜ正常に実行できないのかがわかりません。
|
598
|
+
|
599
|
+
depends.exeを利用して必要なDLLは揃えました。
|
600
|
+
|
601
|
+
基のソースコードがラズパイ用であるためでしょうか?
|
3
編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -327,3 +327,265 @@
|
|
327
327
|
どうやら#include <bcm2835.h>が関係しているようです。
|
328
328
|
|
329
329
|
ただ、このbcm2835がARMやライブラリにどのように働くかは中身を見てみないとわからないので調べてみます。
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
<編集2>
|
334
|
+
|
335
|
+
PCの方でもビルドしてみました。
|
336
|
+
|
337
|
+
PCの環境
|
338
|
+
|
339
|
+
- Windows10 64bit
|
340
|
+
|
341
|
+
- OPENCV3.2.0
|
342
|
+
|
343
|
+
- VS2107
|
344
|
+
|
345
|
+
実行ファイルが得られたのですが、実行すると停止してしまいます。
|
346
|
+
|
347
|
+
必要なDLLはそろえたのですが実行は出来ませんでした。
|
348
|
+
|
349
|
+
ラズパイでないので少しプログラムを編集しました。
|
350
|
+
|
351
|
+
こちらがプログラムです。
|
352
|
+
|
353
|
+
プログラム自体はラズパイでビルドする予定のものなのでうまくいかなかったのかもしれません。
|
354
|
+
|
355
|
+
(ほかに原因があるように思えますが。)
|
356
|
+
|
357
|
+
```
|
358
|
+
|
359
|
+
#include <opencv/cv.h>
|
360
|
+
|
361
|
+
#include <opencv/highgui.h>
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
// 未来カメラのメインプログラム
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
int calcBrightness(CvRect* rect, IplImage* frame);
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
int main() {
|
374
|
+
|
375
|
+
int key;
|
376
|
+
|
377
|
+
char str[32];
|
378
|
+
|
379
|
+
int brightness;
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
CvCapture *capture = 0;
|
388
|
+
|
389
|
+
IplImage *frame = 0;
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
CvHaarClassifierCascade* cvHCC = (CvHaarClassifierCascade*)cvLoad("/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml");
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
CvMemStorage* cvMStr = cvCreateMemStorage(0);
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
CvSeq* face;
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
CvFont font;
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
// ウィンドウの作成とフルスクリーン表示設定
|
410
|
+
|
411
|
+
cvNamedWindow("capture_window", 0);
|
412
|
+
|
413
|
+
cvSetWindowProperty("capture_window", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
// フォント構造体の初期化
|
418
|
+
|
419
|
+
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1, 1);
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
// カメラからのビデオキャプチャを初期化
|
424
|
+
|
425
|
+
capture = cvCreateCameraCapture(0);
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
// 取得するカメラ画像の幅と高さを設定(カメラにより指定できる値が異なります)
|
430
|
+
|
431
|
+
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, (double)160);
|
432
|
+
|
433
|
+
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, (double)120);
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
while (1) {
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
// 1フレーム取り出す
|
444
|
+
|
445
|
+
frame = cvQueryFrame(capture);
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
// 画像中から検出対象の情報を取得
|
450
|
+
|
451
|
+
face = cvHaarDetectObjects(frame, cvHCC, cvMStr);
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
// 検出した個数分のループを回す
|
458
|
+
|
459
|
+
for (int i = 0; i < face->total; i++) {
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
CvRect* faceRect = (CvRect*)cvGetSeqElem(face, i);
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
// 顔検出領域の輝度を取得
|
468
|
+
|
469
|
+
brightness = calcBrightness(faceRect, frame);
|
470
|
+
|
471
|
+
sprintf(str, "B:%d", brightness);
|
472
|
+
|
473
|
+
// 顔の位置に矩形、その下に輝度情報をテキスト表示する
|
474
|
+
|
475
|
+
cvRectangle(frame,
|
476
|
+
|
477
|
+
cvPoint(faceRect->x, faceRect->y),
|
478
|
+
|
479
|
+
cvPoint(faceRect->x + faceRect->width, faceRect->y + faceRect->height),
|
480
|
+
|
481
|
+
CV_RGB(255, 255, 0),
|
482
|
+
|
483
|
+
2, CV_AA);
|
484
|
+
|
485
|
+
cvPutText(frame, str, cvPoint(faceRect->x, faceRect->y + faceRect->height + 16),
|
486
|
+
|
487
|
+
&font, CV_RGB(255, 255, 0));
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
cvShowImage("capture_window", frame);
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
key = cvWaitKey(1);
|
502
|
+
|
503
|
+
if (key == ' ') {
|
504
|
+
|
505
|
+
break;
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
// ダミーでフレームを取得(遅延量を少なく出来るように調整)
|
512
|
+
|
513
|
+
cvQueryFrame(capture);
|
514
|
+
|
515
|
+
cvQueryFrame(capture);
|
516
|
+
|
517
|
+
cvQueryFrame(capture);
|
518
|
+
|
519
|
+
cvQueryFrame(capture);
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
cvReleaseMemStorage(&cvMStr);
|
526
|
+
|
527
|
+
cvReleaseCapture(&capture);
|
528
|
+
|
529
|
+
cvDestroyWindow("capture_window");
|
530
|
+
|
531
|
+
cvReleaseHaarClassifierCascade(&cvHCC);
|
532
|
+
|
533
|
+
|
534
|
+
|
535
|
+
return 0;
|
536
|
+
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
// 指定した領域の輝度平均値を得る
|
542
|
+
|
543
|
+
int calcBrightness(CvRect* rect, IplImage* frame)
|
544
|
+
|
545
|
+
{
|
546
|
+
|
547
|
+
int x;
|
548
|
+
|
549
|
+
int y;
|
550
|
+
|
551
|
+
unsigned char r, g, b, mono;
|
552
|
+
|
553
|
+
unsigned long sum;
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
sum = 0;
|
558
|
+
|
559
|
+
// rectで指定された領域サイズでループを回す
|
560
|
+
|
561
|
+
for (y = rect->y; y<(rect->y + rect->height); y++) {
|
562
|
+
|
563
|
+
for (x = rect->x; x<(rect->x + rect->width); x++) {
|
564
|
+
|
565
|
+
b = frame->imageData[frame->widthStep*y + x * 3];
|
566
|
+
|
567
|
+
g = frame->imageData[frame->widthStep*y + x * 3 + 1];
|
568
|
+
|
569
|
+
r = frame->imageData[frame->widthStep*y + x * 3 + 2];
|
570
|
+
|
571
|
+
// モノクロのデータを算出
|
572
|
+
|
573
|
+
mono = (unsigned char)((float)r*0.30f + (float)g*0.59f + (float)b*0.11f);
|
574
|
+
|
575
|
+
sum += mono;
|
576
|
+
|
577
|
+
}
|
578
|
+
|
579
|
+
}
|
580
|
+
|
581
|
+
// 平均値を戻す
|
582
|
+
|
583
|
+
return(sum / (rect->width*rect->height));
|
584
|
+
|
585
|
+
}
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
|
590
|
+
|
591
|
+
```
|
2
情報不足でした。
test
CHANGED
File without changes
|
test
CHANGED
@@ -313,3 +313,17 @@
|
|
313
313
|
ビルドされる前のライブラリのソースコードがどのような出力をするかわからないと思います。その出力されたバイナリデータ(ライブラリ)がラズパイでコンパイルしただけで正常に動く実行ファイルが得られる理由がわかりません。
|
314
314
|
|
315
315
|
ラズパイの設計者がgccでopencvのライブラリを使う事を考慮して設計したため実行ファイルが正常に動くのでしょうか?
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
<編集>
|
320
|
+
|
321
|
+
今更ですが、この本を参考にしてラズパイでOpencvを使おうと考えていました。
|
322
|
+
|
323
|
+
[http://www.kumikomi.net/interface/contents/201401.php](http://www.kumikomi.net/interface/contents/201401.php)
|
324
|
+
|
325
|
+
載せたソースコードはこの本のサポートページに載っていたものです。
|
326
|
+
|
327
|
+
どうやら#include <bcm2835.h>が関係しているようです。
|
328
|
+
|
329
|
+
ただ、このbcm2835がARMやライブラリにどのように働くかは中身を見てみないとわからないので調べてみます。
|
1
編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,112 +22,282 @@
|
|
22
22
|
|
23
23
|
```
|
24
24
|
|
25
|
-
#include
|
26
|
-
|
27
|
-
#include
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
#in
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
equalizeHist(gray, gray);
|
72
|
-
|
73
|
-
vector<cv::Rect> founds, founds2;
|
74
|
-
|
75
|
-
cascade.detectMultiScale(gray, founds, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
|
76
|
-
|
77
|
-
for (auto faceRect: founds) {
|
78
|
-
|
79
|
-
cv::rectangle(image, faceRect, cv::Scalar(0, 0, 255), 2);
|
80
|
-
|
81
|
-
cv::Mat roi = gray(faceRect);
|
82
|
-
|
83
|
-
cascade2.detectMultiScale(roi, founds2, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
|
84
|
-
|
85
|
-
for (auto eyeRect: founds2) {
|
86
|
-
|
87
|
-
cv::Rect rect(faceRect.x + eyeRect.x, faceRect.y + eyeRect.y, eyeRect.width, eyeRect.height);
|
88
|
-
|
89
|
-
cv::rectangle(image, rect, cv::Scalar(0, 255, 0), 2);
|
90
|
-
|
91
|
-
}
|
25
|
+
#include "cv.h"
|
26
|
+
|
27
|
+
#include "highgui.h"
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
#define CONTROL_LED
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
#ifdef CONTROL_LED
|
36
|
+
|
37
|
+
#include <bcm2835.h>
|
38
|
+
|
39
|
+
#endif
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
// 未来カメラのメインプログラム
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
int calcBrightness(CvRect* rect,IplImage* frame);
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
int main() {
|
52
|
+
|
53
|
+
int key;
|
54
|
+
|
55
|
+
char str[32];
|
56
|
+
|
57
|
+
int brightness;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#ifdef CONTROL_LED
|
62
|
+
|
63
|
+
// BCM2835ライブラリの初期化
|
64
|
+
|
65
|
+
if(!bcm2835_init()){
|
66
|
+
|
67
|
+
printf("bcm2835_init failed\n");
|
68
|
+
|
69
|
+
return(-1);
|
92
70
|
|
93
71
|
}
|
94
72
|
|
73
|
+
// P1端子11番ピンを出力、LOW設定にする
|
74
|
+
|
75
|
+
bcm2835_gpio_fsel(RPI_V2_GPIO_P1_11,BCM2835_GPIO_FSEL_OUTP);
|
76
|
+
|
77
|
+
bcm2835_gpio_write(RPI_V2_GPIO_P1_11,LOW);
|
78
|
+
|
79
|
+
#endif
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
CvCapture *capture = 0;
|
84
|
+
|
85
|
+
IplImage *frame = 0;
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
CvHaarClassifierCascade* cvHCC = (CvHaarClassifierCascade*)cvLoad("/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml");
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
CvMemStorage* cvMStr = cvCreateMemStorage(0);
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
CvSeq* face;
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
CvFont font;
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// ウィンドウの作成とフルスクリーン表示設定
|
106
|
+
|
95
|
-
cv
|
107
|
+
cvNamedWindow ("capture_window", 0);
|
108
|
+
|
96
|
-
|
109
|
+
cvSetWindowProperty("capture_window",CV_WND_PROP_FULLSCREEN,CV_WINDOW_FULLSCREEN);
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// フォント構造体の初期化
|
114
|
+
|
115
|
+
cvInitFont(&font,CV_FONT_HERSHEY_PLAIN,1,1);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// カメラからのビデオキャプチャを初期化
|
120
|
+
|
97
|
-
au
|
121
|
+
capture = cvCreateCameraCapture (0);
|
122
|
+
|
123
|
+
|
124
|
+
|
98
|
-
|
125
|
+
// 取得するカメラ画像の幅と高さを設定(カメラにより指定できる値が異なります)
|
126
|
+
|
127
|
+
cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_WIDTH, (double)160);
|
128
|
+
|
129
|
+
cvSetCaptureProperty (capture, CV_CAP_PROP_FRAME_HEIGHT, (double)120);
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
while (1) {
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
// 1フレーム取り出す
|
140
|
+
|
99
|
-
|
141
|
+
frame = cvQueryFrame (capture);
|
142
|
+
|
143
|
+
|
144
|
+
|
100
|
-
|
145
|
+
// 画像中から検出対象の情報を取得
|
146
|
+
|
147
|
+
face = cvHaarDetectObjects(frame, cvHCC, cvMStr);
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
#ifdef CONTROL_LED
|
152
|
+
|
153
|
+
// 顔検出数がゼロならLED消灯、それ以外なら点灯の為にGPIOを制御する
|
154
|
+
|
155
|
+
if(face->total == 0){
|
156
|
+
|
157
|
+
bcm2835_gpio_write(RPI_V2_GPIO_P1_11,LOW);
|
158
|
+
|
159
|
+
} else {
|
160
|
+
|
161
|
+
bcm2835_gpio_write(RPI_V2_GPIO_P1_11,HIGH);
|
162
|
+
|
101
|
-
|
163
|
+
}
|
164
|
+
|
102
|
-
|
165
|
+
#endif
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
// 検出した個数分のループを回す
|
170
|
+
|
171
|
+
for (int i = 0; i < face->total; i++) {
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
CvRect* faceRect = (CvRect*)cvGetSeqElem(face, i);
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
// 顔検出領域の輝度を取得
|
180
|
+
|
181
|
+
brightness = calcBrightness(faceRect,frame);
|
182
|
+
|
183
|
+
sprintf(str,"B:%d",brightness);
|
184
|
+
|
185
|
+
// 顔の位置に矩形、その下に輝度情報をテキスト表示する
|
186
|
+
|
187
|
+
cvRectangle(frame,
|
188
|
+
|
189
|
+
cvPoint(faceRect->x, faceRect->y),
|
190
|
+
|
191
|
+
cvPoint(faceRect->x + faceRect->width, faceRect->y + faceRect->height),
|
192
|
+
|
193
|
+
CV_RGB(255, 255 ,0),
|
194
|
+
|
195
|
+
2, CV_AA);
|
196
|
+
|
197
|
+
cvPutText(frame,str,cvPoint(faceRect->x, faceRect->y + faceRect->height + 16),
|
198
|
+
|
199
|
+
&font,CV_RGB(255,255,0));
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
cvShowImage ("capture_window", frame);
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
key = cvWaitKey (1);
|
214
|
+
|
215
|
+
if (key == ' ') {
|
216
|
+
|
217
|
+
break;
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
// ダミーでフレームを取得(遅延量を少なく出来るように調整)
|
224
|
+
|
103
|
-
|
225
|
+
cvQueryFrame (capture);
|
226
|
+
|
227
|
+
cvQueryFrame (capture);
|
228
|
+
|
229
|
+
cvQueryFrame (capture);
|
230
|
+
|
231
|
+
cvQueryFrame (capture);
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
cvReleaseMemStorage(&cvMStr);
|
238
|
+
|
239
|
+
cvReleaseCapture (&capture);
|
240
|
+
|
241
|
+
cvDestroyWindow("capture_window");
|
242
|
+
|
243
|
+
cvReleaseHaarClassifierCascade(&cvHCC);
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
return 0;
|
104
248
|
|
105
249
|
}
|
106
250
|
|
107
251
|
|
108
252
|
|
253
|
+
// 指定した領域の輝度平均値を得る
|
254
|
+
|
109
|
-
int
|
255
|
+
int calcBrightness(CvRect* rect,IplImage* frame)
|
256
|
+
|
110
|
-
|
257
|
+
{
|
258
|
+
|
111
|
-
t
|
259
|
+
int x;
|
260
|
+
|
112
|
-
|
261
|
+
int y;
|
262
|
+
|
263
|
+
unsigned char r,g,b,mono;
|
264
|
+
|
265
|
+
unsigned long sum;
|
266
|
+
|
267
|
+
|
268
|
+
|
113
|
-
|
269
|
+
sum = 0;
|
270
|
+
|
114
|
-
|
271
|
+
// rectで指定された領域サイズでループを回す
|
272
|
+
|
273
|
+
for(y=rect->y;y<(rect->y+rect->height);y++){
|
274
|
+
|
275
|
+
for(x=rect->x;x<(rect->x+rect->width);x++){
|
276
|
+
|
277
|
+
b = frame->imageData[frame->widthStep*y+x*3 ];
|
278
|
+
|
279
|
+
g = frame->imageData[frame->widthStep*y+x*3+1];
|
280
|
+
|
281
|
+
r = frame->imageData[frame->widthStep*y+x*3+2];
|
282
|
+
|
283
|
+
// モノクロのデータを算出
|
284
|
+
|
285
|
+
mono = (unsigned char)((float)r*0.30f + (float)g*0.59f + (float)b*0.11f);
|
286
|
+
|
287
|
+
sum += mono;
|
288
|
+
|
115
|
-
|
289
|
+
}
|
116
|
-
|
117
|
-
|
290
|
+
|
118
|
-
|
119
|
-
cout << ex.what() << endl;
|
120
|
-
|
121
|
-
string s;
|
122
|
-
|
123
|
-
cin >> s;
|
124
|
-
|
125
|
-
}
|
291
|
+
}
|
126
|
-
|
292
|
+
|
127
|
-
|
293
|
+
// 平均値を戻す
|
294
|
+
|
295
|
+
return(sum / (rect->width*rect->height));
|
128
296
|
|
129
297
|
}
|
130
298
|
|
299
|
+
|
300
|
+
|
131
301
|
```
|
132
302
|
|
133
303
|
|