forked from AlexeyAB/darknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_opencv.cpp
1549 lines (1356 loc) · 52.5 KB
/
image_opencv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "image_opencv.h"
#include <iostream>
#ifdef OPENCV
#include "utils.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <atomic>
#include <opencv2/core/version.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/opencv_modules.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
// includes for OpenCV >= 3.x
#ifndef CV_VERSION_EPOCH
#include <opencv2/core/types.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#endif
// OpenCV includes for OpenCV 2.x
#ifdef CV_VERSION_EPOCH
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/core/types_c.h>
#include <opencv2/core/version.hpp>
#endif
//using namespace cv;
using std::cerr;
using std::endl;
#ifdef DEBUG
#define OCV_D "d"
#else
#define OCV_D
#endif//DEBUG
// OpenCV libraries
#ifndef CV_VERSION_EPOCH
#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)"" CVAUX_STR(CV_VERSION_REVISION) OCV_D
#ifndef USE_CMAKE_LIBS
#pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
#endif // USE_CMAKE_LIBS
#else // CV_VERSION_EPOCH
#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)"" CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR) OCV_D
#ifndef USE_CMAKE_LIBS
#pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
#pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
#endif // USE_CMAKE_LIBS
#endif // CV_VERSION_EPOCH
#include "http_stream.h"
#ifndef CV_RGB
#define CV_RGB(r, g, b) cvScalar( (b), (g), (r), 0 )
#endif
#ifndef CV_FILLED
#define CV_FILLED cv::FILLED
#endif
#ifndef CV_AA
#define CV_AA cv::LINE_AA
#endif
extern "C" {
//struct mat_cv : cv::Mat { };
//struct cap_cv : cv::VideoCapture { };
//struct write_cv : cv::VideoWriter { };
//struct mat_cv : cv::Mat { int a[0]; };
//struct cap_cv : cv::VideoCapture { int a[0]; };
//struct write_cv : cv::VideoWriter { int a[0]; };
// ====================================================================
// cv::Mat
// ====================================================================
image mat_to_image(cv::Mat mat);
cv::Mat image_to_mat(image img);
// image ipl_to_image(mat_cv* src);
// mat_cv *image_to_ipl(image img);
// cv::Mat ipl_to_mat(IplImage *ipl);
// IplImage *mat_to_ipl(cv::Mat mat);
extern "C" mat_cv *load_image_mat_cv(const char *filename, int flag)
{
cv::Mat *mat_ptr = NULL;
try {
cv::Mat mat = cv::imread(filename, flag);
if (mat.empty())
{
std::string shrinked_filename = filename;
if (shrinked_filename.length() > 1024) {
shrinked_filename.resize(1024);
shrinked_filename = std::string("name is too long: ") + shrinked_filename;
}
cerr << "Cannot load image " << shrinked_filename << std::endl;
std::ofstream bad_list("bad.list", std::ios::out | std::ios::app);
bad_list << shrinked_filename << std::endl;
//if (check_mistakes) getchar();
return NULL;
}
cv::Mat dst;
if (mat.channels() == 3) cv::cvtColor(mat, dst, cv::COLOR_RGB2BGR);
else if (mat.channels() == 4) cv::cvtColor(mat, dst, cv::COLOR_RGBA2BGRA);
else dst = mat;
mat_ptr = new cv::Mat(dst);
return (mat_cv *)mat_ptr;
}
catch (...) {
cerr << "OpenCV exception: load_image_mat_cv \n";
}
if (mat_ptr) delete mat_ptr;
return NULL;
}
// ----------------------------------------
cv::Mat load_image_mat(char *filename, int channels)
{
int flag = cv::IMREAD_UNCHANGED;
if (channels == 0) flag = cv::IMREAD_COLOR;
else if (channels == 1) flag = cv::IMREAD_GRAYSCALE;
else if (channels == 3) flag = cv::IMREAD_COLOR;
else {
fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
}
//flag |= IMREAD_IGNORE_ORIENTATION; // un-comment it if you want
cv::Mat *mat_ptr = (cv::Mat *)load_image_mat_cv(filename, flag);
if (mat_ptr == NULL) {
return cv::Mat();
}
cv::Mat mat = *mat_ptr;
delete mat_ptr;
return mat;
}
// ----------------------------------------
extern "C" image load_image_cv(char *filename, int channels)
{
cv::Mat mat = load_image_mat(filename, channels);
if (mat.empty()) {
return make_image(10, 10, channels);
}
return mat_to_image(mat);
}
// ----------------------------------------
extern "C" image load_image_resize(char *filename, int w, int h, int c, image *im)
{
image out;
try {
cv::Mat loaded_image = load_image_mat(filename, c);
*im = mat_to_image(loaded_image);
cv::Mat resized(h, w, CV_8UC3);
cv::resize(loaded_image, resized, cv::Size(w, h), 0, 0, cv::INTER_LINEAR);
out = mat_to_image(resized);
}
catch (...) {
cerr << " OpenCV exception: load_image_resize() can't load image %s " << filename << " \n";
out = make_image(w, h, c);
*im = make_image(w, h, c);
}
return out;
}
// ----------------------------------------
extern "C" int get_width_mat(mat_cv *mat)
{
if (mat == NULL) {
cerr << " Pointer is NULL in get_width_mat() \n";
return 0;
}
return ((cv::Mat *)mat)->cols;
}
// ----------------------------------------
extern "C" int get_height_mat(mat_cv *mat)
{
if (mat == NULL) {
cerr << " Pointer is NULL in get_height_mat() \n";
return 0;
}
return ((cv::Mat *)mat)->rows;
}
// ----------------------------------------
extern "C" void release_mat(mat_cv **mat)
{
try {
cv::Mat **mat_ptr = (cv::Mat **)mat;
if (*mat_ptr) delete *mat_ptr;
*mat_ptr = NULL;
}
catch (...) {
cerr << "OpenCV exception: release_mat \n";
}
}
// ====================================================================
// IplImage
// ====================================================================
/*
extern "C" int get_width_cv(mat_cv *ipl_src)
{
IplImage *ipl = (IplImage *)ipl_src;
return ipl->width;
}
// ----------------------------------------
extern "C" int get_height_cv(mat_cv *ipl_src)
{
IplImage *ipl = (IplImage *)ipl_src;
return ipl->height;
}
// ----------------------------------------
extern "C" void release_ipl(mat_cv **ipl)
{
IplImage **ipl_img = (IplImage **)ipl;
if (*ipl_img) cvReleaseImage(ipl_img);
*ipl_img = NULL;
}
// ----------------------------------------
// ====================================================================
// image-to-ipl, ipl-to-image, image_to_mat, mat_to_image
// ====================================================================
extern "C" mat_cv *image_to_ipl(image im)
{
int x, y, c;
IplImage *disp = cvCreateImage(cvSize(im.w, im.h), IPL_DEPTH_8U, im.c);
int step = disp->widthStep;
for (y = 0; y < im.h; ++y) {
for (x = 0; x < im.w; ++x) {
for (c = 0; c < im.c; ++c) {
float val = im.data[c*im.h*im.w + y*im.w + x];
disp->imageData[y*step + x*im.c + c] = (unsigned char)(val * 255);
}
}
}
return (mat_cv *)disp;
}
// ----------------------------------------
extern "C" image ipl_to_image(mat_cv* src_ptr)
{
IplImage* src = (IplImage*)src_ptr;
int h = src->height;
int w = src->width;
int c = src->nChannels;
image im = make_image(w, h, c);
unsigned char *data = (unsigned char *)src->imageData;
int step = src->widthStep;
int i, j, k;
for (i = 0; i < h; ++i) {
for (k = 0; k < c; ++k) {
for (j = 0; j < w; ++j) {
im.data[k*w*h + i*w + j] = data[i*step + j*c + k] / 255.;
}
}
}
return im;
}
// ----------------------------------------
cv::Mat ipl_to_mat(IplImage *ipl)
{
Mat m = cvarrToMat(ipl, true);
return m;
}
// ----------------------------------------
IplImage *mat_to_ipl(cv::Mat mat)
{
IplImage *ipl = new IplImage;
*ipl = mat;
return ipl;
}
// ----------------------------------------
*/
extern "C" cv::Mat image_to_mat(image img)
{
int channels = img.c;
int width = img.w;
int height = img.h;
cv::Mat mat = cv::Mat(height, width, CV_8UC(channels));
int step = mat.step;
for (int y = 0; y < img.h; ++y) {
for (int x = 0; x < img.w; ++x) {
for (int c = 0; c < img.c; ++c) {
float val = img.data[c*img.h*img.w + y*img.w + x];
mat.data[y*step + x*img.c + c] = (unsigned char)(val * 255);
}
}
}
return mat;
}
// ----------------------------------------
extern "C" image mat_to_image(cv::Mat mat)
{
int w = mat.cols;
int h = mat.rows;
int c = mat.channels();
image im = make_image(w, h, c);
unsigned char *data = (unsigned char *)mat.data;
int step = mat.step;
for (int y = 0; y < h; ++y) {
for (int k = 0; k < c; ++k) {
for (int x = 0; x < w; ++x) {
//uint8_t val = mat.ptr<uint8_t>(y)[c * x + k];
//uint8_t val = mat.at<Vec3b>(y, x).val[k];
//im.data[k*w*h + y*w + x] = val / 255.0f;
im.data[k*w*h + y*w + x] = data[y*step + x*c + k] / 255.0f;
}
}
}
return im;
}
image mat_to_image_cv(mat_cv *mat)
{
return mat_to_image(*(cv::Mat*)mat);
}
// ====================================================================
// Window
// ====================================================================
extern "C" void create_window_cv(char const* window_name, int full_screen, int width, int height)
{
try {
int window_type = cv::WINDOW_NORMAL;
#ifdef CV_VERSION_EPOCH // OpenCV 2.x
if (full_screen) window_type = CV_WINDOW_FULLSCREEN;
#else
if (full_screen) window_type = cv::WINDOW_FULLSCREEN;
#endif
cv::namedWindow(window_name, window_type);
cv::moveWindow(window_name, 0, 0);
cv::resizeWindow(window_name, width, height);
}
catch (...) {
cerr << "OpenCV exception: create_window_cv \n";
}
}
// ----------------------------------------
extern "C" void resize_window_cv(char const* window_name, int width, int height)
{
try {
cv::resizeWindow(window_name, width, height);
}
catch (...) {
cerr << "OpenCV exception: create_window_cv \n";
}
}
// ----------------------------------------
extern "C" void destroy_all_windows_cv()
{
try {
cv::destroyAllWindows();
}
catch (...) {
cerr << "OpenCV exception: destroy_all_windows_cv \n";
}
}
// ----------------------------------------
extern "C" int wait_key_cv(int delay)
{
try {
return cv::waitKey(delay);
}
catch (...) {
cerr << "OpenCV exception: wait_key_cv \n";
}
return -1;
}
// ----------------------------------------
extern "C" int wait_until_press_key_cv()
{
return wait_key_cv(0);
}
// ----------------------------------------
extern "C" void make_window(char *name, int w, int h, int fullscreen)
{
try {
cv::namedWindow(name, cv::WINDOW_NORMAL);
if (fullscreen) {
#ifdef CV_VERSION_EPOCH // OpenCV 2.x
cv::setWindowProperty(name, cv::WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
#else
cv::setWindowProperty(name, cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);
#endif
}
else {
cv::resizeWindow(name, w, h);
if (strcmp(name, "Demo") == 0) cv::moveWindow(name, 0, 0);
}
}
catch (...) {
cerr << "OpenCV exception: make_window \n";
}
}
// ----------------------------------------
static float get_pixel(image m, int x, int y, int c)
{
assert(x < m.w && y < m.h && c < m.c);
return m.data[c*m.h*m.w + y*m.w + x];
}
// ----------------------------------------
extern "C" void show_image_cv(image p, const char *name)
{
try {
image copy = copy_image(p);
constrain_image(copy);
cv::Mat mat = image_to_mat(copy);
if (mat.channels() == 3) cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
else if (mat.channels() == 4) cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGR);
cv::namedWindow(name, cv::WINDOW_NORMAL);
cv::imshow(name, mat);
free_image(copy);
}
catch (...) {
cerr << "OpenCV exception: show_image_cv \n";
}
}
// ----------------------------------------
/*
extern "C" void show_image_cv_ipl(mat_cv *disp, const char *name)
{
if (disp == NULL) return;
char buff[256];
sprintf(buff, "%s", name);
cv::namedWindow(buff, WINDOW_NORMAL);
cvShowImage(buff, disp);
}
// ----------------------------------------
*/
extern "C" void show_image_mat(mat_cv *mat_ptr, const char *name)
{
try {
if (mat_ptr == NULL) return;
cv::Mat &mat = *(cv::Mat *)mat_ptr;
cv::namedWindow(name, cv::WINDOW_NORMAL);
cv::imshow(name, mat);
}
catch (...) {
cerr << "OpenCV exception: show_image_mat \n";
}
}
// ====================================================================
// Video Writer
// ====================================================================
extern "C" write_cv *create_video_writer(char *out_filename, char c1, char c2, char c3, char c4, int fps, int width, int height, int is_color)
{
try {
cv::VideoWriter * output_video_writer =
#ifdef CV_VERSION_EPOCH
new cv::VideoWriter(out_filename, CV_FOURCC(c1, c2, c3, c4), fps, cv::Size(width, height), is_color);
#else
new cv::VideoWriter(out_filename, cv::VideoWriter::fourcc(c1, c2, c3, c4), fps, cv::Size(width, height), is_color);
#endif
return (write_cv *)output_video_writer;
}
catch (...) {
cerr << "OpenCV exception: create_video_writer \n";
}
return NULL;
}
extern "C" void write_frame_cv(write_cv *output_video_writer, mat_cv *mat)
{
try {
cv::VideoWriter *out = (cv::VideoWriter *)output_video_writer;
out->write(*(cv::Mat*)mat);
}
catch (...) {
cerr << "OpenCV exception: write_frame_cv \n";
}
}
extern "C" void release_video_writer(write_cv **output_video_writer)
{
try {
if (output_video_writer) {
std::cout << " closing...";
cv::VideoWriter *out = *(cv::VideoWriter **)output_video_writer;
out->release();
delete out;
output_video_writer = NULL;
std::cout << " closed!";
}
else {
cerr << "OpenCV exception: output_video_writer isn't created \n";
}
}
catch (...) {
cerr << "OpenCV exception: release_video_writer \n";
}
}
/*
extern "C" void *open_video_stream(const char *f, int c, int w, int h, int fps)
{
VideoCapture *cap;
if(f) cap = new VideoCapture(f);
else cap = new VideoCapture(c);
if(!cap->isOpened()) return 0;
if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
if(fps) cap->set(CV_CAP_PROP_FPS, w);
return (void *) cap;
}
extern "C" image get_image_from_stream(void *p)
{
VideoCapture *cap = (VideoCapture *)p;
Mat m;
*cap >> m;
if(m.empty()) return make_empty_image(0,0,0);
return mat_to_image(m);
}
extern "C" int show_image_cv(image im, const char* name, int ms)
{
Mat m = image_to_mat(im);
imshow(name, m);
int c = waitKey(ms);
if (c != -1) c = c%256;
return c;
}
*/
// ====================================================================
// Video Capture
// ====================================================================
extern "C" cap_cv* get_capture_video_stream(const char *path) {
cv::VideoCapture* cap = NULL;
try {
cap = new cv::VideoCapture(path);
}
catch (...) {
cerr << " OpenCV exception: video-stream " << path << " can't be opened! \n";
}
return (cap_cv*)cap;
}
// ----------------------------------------
extern "C" cap_cv* get_capture_webcam(int index)
{
cv::VideoCapture* cap = NULL;
try {
cap = new cv::VideoCapture(index);
//cap->set(CV_CAP_PROP_FRAME_WIDTH, 1280);
//cap->set(CV_CAP_PROP_FRAME_HEIGHT, 960);
}
catch (...) {
cerr << " OpenCV exception: Web-camera " << index << " can't be opened! \n";
}
return (cap_cv*)cap;
}
// ----------------------------------------
extern "C" void release_capture(cap_cv* cap)
{
try {
cv::VideoCapture *cpp_cap = (cv::VideoCapture *)cap;
delete cpp_cap;
}
catch (...) {
cerr << " OpenCV exception: cv::VideoCapture " << cap << " can't be released! \n";
}
}
// ----------------------------------------
extern "C" mat_cv* get_capture_frame_cv(cap_cv *cap) {
cv::Mat *mat = NULL;
try {
mat = new cv::Mat();
if (cap) {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
if (cpp_cap.isOpened())
{
cpp_cap >> *mat;
}
else std::cout << " Video-stream stopped! \n";
}
else cerr << " cv::VideoCapture isn't created \n";
}
catch (...) {
std::cout << " OpenCV exception: Video-stream stoped! \n";
}
return (mat_cv *)mat;
}
// ----------------------------------------
extern "C" int get_stream_fps_cpp_cv(cap_cv *cap)
{
int fps = 25;
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
#ifndef CV_VERSION_EPOCH // OpenCV 3.x
fps = cpp_cap.get(cv::CAP_PROP_FPS);
#else // OpenCV 2.x
fps = cpp_cap.get(CV_CAP_PROP_FPS);
#endif
}
catch (...) {
cerr << " Can't get FPS of source videofile. For output video FPS = 25 by default. \n";
}
return fps;
}
// ----------------------------------------
extern "C" double get_capture_property_cv(cap_cv *cap, int property_id)
{
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
return cpp_cap.get(property_id);
}
catch (...) {
cerr << " OpenCV exception: Can't get property of source video-stream. \n";
}
return 0;
}
// ----------------------------------------
extern "C" double get_capture_frame_count_cv(cap_cv *cap)
{
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
#ifndef CV_VERSION_EPOCH // OpenCV 3.x
return cpp_cap.get(cv::CAP_PROP_FRAME_COUNT);
#else // OpenCV 2.x
return cpp_cap.get(CV_CAP_PROP_FRAME_COUNT);
#endif
}
catch (...) {
cerr << " OpenCV exception: Can't get CAP_PROP_FRAME_COUNT of source videofile. \n";
}
return 0;
}
// ----------------------------------------
extern "C" int set_capture_property_cv(cap_cv *cap, int property_id, double value)
{
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
return cpp_cap.set(property_id, value);
}
catch (...) {
cerr << " Can't set property of source video-stream. \n";
}
return false;
}
// ----------------------------------------
extern "C" int set_capture_position_frame_cv(cap_cv *cap, int index)
{
try {
cv::VideoCapture &cpp_cap = *(cv::VideoCapture *)cap;
#ifndef CV_VERSION_EPOCH // OpenCV 3.x
return cpp_cap.set(cv::CAP_PROP_POS_FRAMES, index);
#else // OpenCV 2.x
return cpp_cap.set(CV_CAP_PROP_POS_FRAMES, index);
#endif
}
catch (...) {
cerr << " Can't set CAP_PROP_POS_FRAMES of source videofile. \n";
}
return false;
}
// ----------------------------------------
// ====================================================================
// ... Video Capture
// ====================================================================
extern "C" image get_image_from_stream_cpp(cap_cv *cap)
{
cv::Mat *src = NULL;
static int once = 1;
if (once) {
once = 0;
do {
if (src) delete src;
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!src) return make_empty_image(0, 0, 0);
} while (src->cols < 1 || src->rows < 1 || src->channels() < 1);
printf("Video stream: %d x %d \n", src->cols, src->rows);
}
else
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!src) return make_empty_image(0, 0, 0);
image im = mat_to_image(*src);
rgbgr_image(im);
if (src) delete src;
return im;
}
// ----------------------------------------
extern "C" int wait_for_stream(cap_cv *cap, cv::Mat* src, int dont_close)
{
if (!src) {
if (dont_close) src = new cv::Mat(416, 416, CV_8UC(3)); // cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
else return 0;
}
if (src->cols < 1 || src->rows < 1 || src->channels() < 1) {
if (dont_close) {
delete src;// cvReleaseImage(&src);
int z = 0;
for (z = 0; z < 20; ++z) {
src = (cv::Mat*)get_capture_frame_cv(cap);
delete src;// cvReleaseImage(&src);
}
src = new cv::Mat(416, 416, CV_8UC(3)); // cvCreateImage(cvSize(416, 416), IPL_DEPTH_8U, 3);
}
else return 0;
}
return 1;
}
// ----------------------------------------
extern "C" image get_image_from_stream_resize(cap_cv *cap, int w, int h, int c, mat_cv** in_img, int dont_close)
{
c = c ? c : 3;
cv::Mat *src = NULL;
static int once = 1;
if (once) {
once = 0;
do {
if (src) delete src;
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!src) return make_empty_image(0, 0, 0);
} while (src->cols < 1 || src->rows < 1 || src->channels() < 1);
printf("Video stream: %d x %d \n", src->cols, src->rows);
}
else
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0);
*(cv::Mat **)in_img = src;
cv::Mat new_img = cv::Mat(h, w, CV_8UC(c));
cv::resize(*src, new_img, new_img.size(), 0, 0, cv::INTER_LINEAR);
if (c>1) cv::cvtColor(new_img, new_img, cv::COLOR_RGB2BGR);
image im = mat_to_image(new_img);
//show_image_cv(im, "im");
//show_image_mat(*in_img, "in_img");
return im;
}
// ----------------------------------------
extern "C" image get_image_from_stream_letterbox(cap_cv *cap, int w, int h, int c, mat_cv** in_img, int dont_close)
{
c = c ? c : 3;
cv::Mat *src = NULL;
static int once = 1;
if (once) {
once = 0;
do {
if (src) delete src;
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!src) return make_empty_image(0, 0, 0);
} while (src->cols < 1 || src->rows < 1 || src->channels() < 1);
printf("Video stream: %d x %d \n", src->cols, src->rows);
}
else
src = (cv::Mat*)get_capture_frame_cv(cap);
if (!wait_for_stream(cap, src, dont_close)) return make_empty_image(0, 0, 0); // passes (cv::Mat *)src while should be (cv::Mat **)src
*in_img = (mat_cv *)new cv::Mat(src->rows, src->cols, CV_8UC(c));
cv::resize(*src, **(cv::Mat**)in_img, (*(cv::Mat**)in_img)->size(), 0, 0, cv::INTER_LINEAR);
if (c>1) cv::cvtColor(*src, *src, cv::COLOR_RGB2BGR);
image tmp = mat_to_image(*src);
image im = letterbox_image(tmp, w, h);
free_image(tmp);
release_mat((mat_cv **)&src);
//show_image_cv(im, "im");
//show_image_mat(*in_img, "in_img");
return im;
}
// ----------------------------------------
// ====================================================================
// Image Saving
// ====================================================================
extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
extern int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
extern "C" void save_mat_png(cv::Mat img_src, const char *name)
{
cv::Mat img_rgb;
if (img_src.channels() >= 3) cv::cvtColor(img_src, img_rgb, cv::COLOR_RGB2BGR);
stbi_write_png(name, img_rgb.cols, img_rgb.rows, 3, (char *)img_rgb.data, 0);
}
// ----------------------------------------
extern "C" void save_mat_jpg(cv::Mat img_src, const char *name)
{
cv::Mat img_rgb;
if (img_src.channels() >= 3) cv::cvtColor(img_src, img_rgb, cv::COLOR_RGB2BGR);
stbi_write_jpg(name, img_rgb.cols, img_rgb.rows, 3, (char *)img_rgb.data, 80);
}
// ----------------------------------------
extern "C" void save_cv_png(mat_cv *img_src, const char *name)
{
cv::Mat* img = (cv::Mat* )img_src;
save_mat_png(*img, name);
}
// ----------------------------------------
extern "C" void save_cv_jpg(mat_cv *img_src, const char *name)
{
cv::Mat* img = (cv::Mat*)img_src;
save_mat_jpg(*img, name);
}
// ----------------------------------------
// ====================================================================
// Draw Detection
// ====================================================================
extern "C" void draw_detections_cv_v3(mat_cv* mat, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output)
{
try {
cv::Mat *show_img = (cv::Mat*)mat;
int i, j;
if (!show_img) return;
static int frame_id = 0;
frame_id++;
for (i = 0; i < num; ++i) {
char labelstr[4096] = { 0 };
int class_id = -1;
for (j = 0; j < classes; ++j) {
int show = strncmp(names[j], "dont_show", 9);
if (dets[i].prob[j] > thresh && show) {
if (class_id < 0) {
strcat(labelstr, names[j]);
class_id = j;
char buff[20];
if (dets[i].track_id) {
sprintf(buff, " (id: %d)", dets[i].track_id);
strcat(labelstr, buff);
}
sprintf(buff, " (%2.0f%%)", dets[i].prob[j] * 100);
strcat(labelstr, buff);
printf("%s: %.0f%% ", names[j], dets[i].prob[j] * 100);
if (dets[i].track_id) printf("(track = %d, sim = %f) ", dets[i].track_id, dets[i].sim);
}
else {
strcat(labelstr, ", ");
strcat(labelstr, names[j]);
printf(", %s: %.0f%% ", names[j], dets[i].prob[j] * 100);
}
}
}
if (class_id >= 0) {
int width = std::max(1.0f, show_img->rows * .002f);
//if(0){
//width = pow(prob, 1./2.)*10+1;
//alphabet = 0;
//}
//printf("%d %s: %.0f%%\n", i, names[class_id], prob*100);
int offset = class_id * 123457 % classes;
float red = get_color(2, offset, classes);
float green = get_color(1, offset, classes);
float blue = get_color(0, offset, classes);
float rgb[3];
//width = prob*20+2;
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
box b = dets[i].bbox;
if (std::isnan(b.w) || std::isinf(b.w)) b.w = 0.5;
if (std::isnan(b.h) || std::isinf(b.h)) b.h = 0.5;
if (std::isnan(b.x) || std::isinf(b.x)) b.x = 0.5;
if (std::isnan(b.y) || std::isinf(b.y)) b.y = 0.5;
b.w = (b.w < 1) ? b.w : 1;
b.h = (b.h < 1) ? b.h : 1;
b.x = (b.x < 1) ? b.x : 1;
b.y = (b.y < 1) ? b.y : 1;
//printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
int left = (b.x - b.w / 2.)*show_img->cols;
int right = (b.x + b.w / 2.)*show_img->cols;
int top = (b.y - b.h / 2.)*show_img->rows;
int bot = (b.y + b.h / 2.)*show_img->rows;
if (left < 0) left = 0;
if (right > show_img->cols - 1) right = show_img->cols - 1;
if (top < 0) top = 0;
if (bot > show_img->rows - 1) bot = show_img->rows - 1;
//int b_x_center = (left + right) / 2;
//int b_y_center = (top + bot) / 2;
//int b_width = right - left;
//int b_height = bot - top;
//sprintf(labelstr, "%d x %d - w: %d, h: %d", b_x_center, b_y_center, b_width, b_height);
float const font_size = show_img->rows / 1000.F;
cv::Size const text_size = cv::getTextSize(labelstr, cv::FONT_HERSHEY_COMPLEX_SMALL, font_size, 1, 0);
cv::Point pt1, pt2, pt_text, pt_text_bg1, pt_text_bg2;
pt1.x = left;
pt1.y = top;
pt2.x = right;
pt2.y = bot;
pt_text.x = left;
pt_text.y = top - 4;// 12;
pt_text_bg1.x = left;
pt_text_bg1.y = top - (3 + 18 * font_size);
pt_text_bg2.x = right;
if ((right - left) < text_size.width) pt_text_bg2.x = left + text_size.width;
pt_text_bg2.y = top;
cv::Scalar color;
color.val[0] = red * 256;
color.val[1] = green * 256;
color.val[2] = blue * 256;
// you should create directory: result_img
//static int copied_frame_id = -1;
//static IplImage* copy_img = NULL;
//if (copied_frame_id != frame_id) {
// copied_frame_id = frame_id;
// if(copy_img == NULL) copy_img = cvCreateImage(cvSize(show_img->width, show_img->height), show_img->depth, show_img->nChannels);
// cvCopy(show_img, copy_img, 0);
//}
//static int img_id = 0;
//img_id++;
//char image_name[1024];
//sprintf(image_name, "result_img/img_%d_%d_%d_%s.jpg", frame_id, img_id, class_id, names[class_id]);
//CvRect rect = cvRect(pt1.x, pt1.y, pt2.x - pt1.x, pt2.y - pt1.y);
//cvSetImageROI(copy_img, rect);
//cvSaveImage(image_name, copy_img, 0);
//cvResetImageROI(copy_img);
cv::rectangle(*show_img, pt1, pt2, color, width, 8, 0);
if (ext_output)
printf("\t(left_x: %4.0f top_y: %4.0f width: %4.0f height: %4.0f)\n",
(float)left, (float)top, b.w*show_img->cols, b.h*show_img->rows);
else