-
Notifications
You must be signed in to change notification settings - Fork 866
/
core.cpp
1291 lines (995 loc) · 32.3 KB
/
core.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 "core.h"
#include <string.h>
// Mat_New creates a new empty Mat
Mat Mat_New() {
return new cv::Mat();
}
// Mat_NewWithSize creates a new Mat with a specific size dimension and number of channels.
Mat Mat_NewWithSize(int rows, int cols, int type) {
return new cv::Mat(rows, cols, type, 0.0);
}
// Mat_NewWithSizes creates a new Mat with specific dimension sizes and number of channels.
Mat Mat_NewWithSizes(struct IntVector sizes, int type) {
std::vector<int> sizess;
for (int i = 0; i < sizes.length; ++i) {
sizess.push_back(sizes.val[i]);
}
return new cv::Mat(sizess, type);
}
// Mat_NewFromScalar creates a new Mat from a Scalar. Intended to be used
// for Mat comparison operation such as InRange.
Mat Mat_NewFromScalar(Scalar ar, int type) {
cv::Scalar c = cv::Scalar(ar.val1, ar.val2, ar.val3, ar.val4);
return new cv::Mat(1, 1, type, c);
}
// Mat_NewWithSizeFromScalar creates a new Mat from a Scalar with a specific size dimension and number of channels
Mat Mat_NewWithSizeFromScalar(Scalar ar, int rows, int cols, int type) {
cv::Scalar c = cv::Scalar(ar.val1, ar.val2, ar.val3, ar.val4);
return new cv::Mat(rows, cols, type, c);
}
Mat Mat_NewFromBytes(int rows, int cols, int type, struct ByteArray buf) {
return new cv::Mat(rows, cols, type, buf.data);
}
// Mat_NewWithSizesFromScalar creates multidimensional Mat from a scalar
Mat Mat_NewWithSizesFromScalar(IntVector sizes, int type, Scalar ar) {
std::vector<int> _sizes;
for (int i = 0, *v = sizes.val; i < sizes.length; ++v, ++i) {
_sizes.push_back(*v);
}
cv::Scalar c = cv::Scalar(ar.val1, ar.val2, ar.val3, ar.val4);
return new cv::Mat(_sizes, type, c);
}
// Mat_NewWithSizesFromBytes creates multidimensional Mat from a bytes
Mat Mat_NewWithSizesFromBytes(IntVector sizes, int type, struct ByteArray buf) {
std::vector<int> _sizes;
for (int i = 0, *v = sizes.val; i < sizes.length; ++v, ++i) {
_sizes.push_back(*v);
}
return new cv::Mat(_sizes, type, buf.data);
}
Mat Eye(int rows, int cols, int type) {
cv::Mat* mat = new cv::Mat(rows, cols, type);
*mat = cv::Mat::eye(rows, cols, type);
return mat;
}
Mat Zeros(int rows, int cols, int type) {
cv::Mat* mat = new cv::Mat(rows, cols, type);
*mat = cv::Mat::zeros(rows, cols, type);
return mat;
}
Mat Ones(int rows, int cols, int type) {
cv::Mat* mat = new cv::Mat(rows, cols, type);
*mat = cv::Mat::ones(rows, cols, type);
return mat;
}
Mat Mat_FromPtr(Mat m, int rows, int cols, int type, int prow, int pcol) {
return new cv::Mat(rows, cols, type, m->ptr(prow, pcol));
}
// Mat_Close deletes an existing Mat
void Mat_Close(Mat m) {
delete m;
}
// Mat_Empty tests if a Mat is empty
int Mat_Empty(Mat m) {
return m->empty();
}
// Mat_IsContinuous tests if a Mat is continuous
bool Mat_IsContinuous(Mat m) {
return m->isContinuous();
}
void Mat_Inv(Mat m) {
m->inv();
}
Mat Mat_Col(Mat m, int c) {
return new cv::Mat(m->col(c));
}
Mat Mat_Row(Mat m, int r) {
return new cv::Mat(m->row(r));
}
// Mat_Clone returns a clone of this Mat
Mat Mat_Clone(Mat m) {
return new cv::Mat(m->clone());
}
// Mat_CopyTo copies this Mat to another Mat.
void Mat_CopyTo(Mat m, Mat dst) {
m->copyTo(*dst);
}
// Mat_CopyToWithMask copies this Mat to another Mat while applying the mask
void Mat_CopyToWithMask(Mat m, Mat dst, Mat mask) {
m->copyTo(*dst, *mask);
}
void Mat_ConvertTo(Mat m, Mat dst, int type) {
m->convertTo(*dst, type);
}
void Mat_ConvertToWithParams(Mat m, Mat dst, int type, float alpha, float beta) {
m->convertTo(*dst, type, alpha, beta);
}
// Mat_ToBytes returns the bytes representation of the underlying data.
struct ByteArray Mat_ToBytes(Mat m) {
return toByteArray(reinterpret_cast<const char*>(m->data), m->total() * m->elemSize());
}
struct ByteArray Mat_DataPtr(Mat m) {
return ByteArray {reinterpret_cast<char*>(m->data), static_cast<int>(m->total() * m->elemSize())};
}
// Mat_Region returns a Mat of a region of another Mat
Mat Mat_Region(Mat m, Rect r) {
return new cv::Mat(*m, cv::Rect(r.x, r.y, r.width, r.height));
}
Mat Mat_Reshape(Mat m, int cn, int rows) {
return new cv::Mat(m->reshape(cn, rows));
}
void Mat_PatchNaNs(Mat m) {
cv::patchNaNs(*m);
}
Mat Mat_ConvertFp16(Mat m) {
Mat dst = new cv::Mat();
cv::convertFp16(*m, *dst);
return dst;
}
Mat Mat_Sqrt(Mat m) {
Mat dst = new cv::Mat();
cv::sqrt(*m, *dst);
return dst;
}
// Mat_Mean calculates the mean value M of array elements, independently for each channel, and return it as Scalar vector
Scalar Mat_Mean(Mat m) {
cv::Scalar c = cv::mean(*m);
Scalar scal = Scalar();
scal.val1 = c.val[0];
scal.val2 = c.val[1];
scal.val3 = c.val[2];
scal.val4 = c.val[3];
return scal;
}
// Mat_MeanWithMask calculates the mean value M of array elements,
// independently for each channel, and returns it as Scalar vector
// while applying the mask.
Scalar Mat_MeanWithMask(Mat m, Mat mask){
cv::Scalar c = cv::mean(*m, *mask);
Scalar scal = Scalar();
scal.val1 = c.val[0];
scal.val2 = c.val[1];
scal.val3 = c.val[2];
scal.val4 = c.val[3];
return scal;
}
void LUT(Mat src, Mat lut, Mat dst) {
cv::LUT(*src, *lut, *dst);
}
// Mat_Rows returns how many rows in this Mat.
int Mat_Rows(Mat m) {
return m->rows;
}
// Mat_Cols returns how many columns in this Mat.
int Mat_Cols(Mat m) {
return m->cols;
}
// Mat_Channels returns how many channels in this Mat.
int Mat_Channels(Mat m) {
return m->channels();
}
// Mat_Type returns the type from this Mat.
int Mat_Type(Mat m) {
return m->type();
}
// Mat_Step returns the number of bytes each matrix row occupies.
int Mat_Step(Mat m) {
return m->step;
}
int Mat_Total(Mat m) {
return m->total();
}
int Mat_ElemSize(Mat m){
return m->elemSize();
}
void Mat_Size(Mat m, IntVector* res) {
cv::MatSize ms(m->size);
int* ids = new int[ms.dims()];
for (size_t i = 0; i < ms.dims(); ++i) {
ids[i] = ms[i];
}
res->length = ms.dims();
res->val = ids;
return;
}
// Mat_GetUChar returns a specific row/col value from this Mat expecting
// each element to contain a schar aka CV_8U.
uint8_t Mat_GetUChar(Mat m, int row, int col) {
return m->at<uchar>(row, col);
}
uint8_t Mat_GetUChar3(Mat m, int x, int y, int z) {
return m->at<uchar>(x, y, z);
}
// Mat_GetSChar returns a specific row/col value from this Mat expecting
// each element to contain a schar aka CV_8S.
int8_t Mat_GetSChar(Mat m, int row, int col) {
return m->at<schar>(row, col);
}
int8_t Mat_GetSChar3(Mat m, int x, int y, int z) {
return m->at<schar>(x, y, z);
}
// Mat_GetShort returns a specific row/col value from this Mat expecting
// each element to contain a short aka CV_16S.
int16_t Mat_GetShort(Mat m, int row, int col) {
return m->at<short>(row, col);
}
int16_t Mat_GetShort3(Mat m, int x, int y, int z) {
return m->at<short>(x, y, z);
}
// Mat_GetInt returns a specific row/col value from this Mat expecting
// each element to contain an int aka CV_32S.
int32_t Mat_GetInt(Mat m, int row, int col) {
return m->at<int>(row, col);
}
int32_t Mat_GetInt3(Mat m, int x, int y, int z) {
return m->at<int>(x, y, z);
}
// Mat_GetFloat returns a specific row/col value from this Mat expecting
// each element to contain a float aka CV_32F.
float Mat_GetFloat(Mat m, int row, int col) {
return m->at<float>(row, col);
}
float Mat_GetFloat3(Mat m, int x, int y, int z) {
return m->at<float>(x, y, z);
}
// Mat_GetDouble returns a specific row/col value from this Mat expecting
// each element to contain a double aka CV_64F.
double Mat_GetDouble(Mat m, int row, int col) {
return m->at<double>(row, col);
}
double Mat_GetDouble3(Mat m, int x, int y, int z) {
return m->at<double>(x, y, z);
}
void Mat_SetTo(Mat m, Scalar value) {
cv::Scalar c_value(value.val1, value.val2, value.val3, value.val4);
m->setTo(c_value);
}
// Mat_SetUChar set a specific row/col value from this Mat expecting
// each element to contain a schar aka CV_8U.
void Mat_SetUChar(Mat m, int row, int col, uint8_t val) {
m->at<uchar>(row, col) = val;
}
void Mat_SetUChar3(Mat m, int x, int y, int z, uint8_t val) {
m->at<uchar>(x, y, z) = val;
}
// Mat_SetSChar set a specific row/col value from this Mat expecting
// each element to contain a schar aka CV_8S.
void Mat_SetSChar(Mat m, int row, int col, int8_t val) {
m->at<schar>(row, col) = val;
}
void Mat_SetSChar3(Mat m, int x, int y, int z, int8_t val) {
m->at<schar>(x, y, z) = val;
}
// Mat_SetShort set a specific row/col value from this Mat expecting
// each element to contain a short aka CV_16S.
void Mat_SetShort(Mat m, int row, int col, int16_t val) {
m->at<short>(row, col) = val;
}
void Mat_SetShort3(Mat m, int x, int y, int z, int16_t val) {
m->at<short>(x, y, z) = val;
}
// Mat_SetInt set a specific row/col value from this Mat expecting
// each element to contain an int aka CV_32S.
void Mat_SetInt(Mat m, int row, int col, int32_t val) {
m->at<int>(row, col) = val;
}
void Mat_SetInt3(Mat m, int x, int y, int z, int32_t val) {
m->at<int>(x, y, z) = val;
}
// Mat_SetFloat set a specific row/col value from this Mat expecting
// each element to contain a float aka CV_32F.
void Mat_SetFloat(Mat m, int row, int col, float val) {
m->at<float>(row, col) = val;
}
void Mat_SetFloat3(Mat m, int x, int y, int z, float val) {
m->at<float>(x, y, z) = val;
}
// Mat_SetDouble set a specific row/col value from this Mat expecting
// each element to contain a double aka CV_64F.
void Mat_SetDouble(Mat m, int row, int col, double val) {
m->at<double>(row, col) = val;
}
void Mat_SetDouble3(Mat m, int x, int y, int z, double val) {
m->at<double>(x, y, z) = val;
}
void Mat_AddUChar(Mat m, uint8_t val) {
*m += val;
}
void Mat_SubtractUChar(Mat m, uint8_t val) {
*m -= val;
}
void Mat_MultiplyUChar(Mat m, uint8_t val) {
*m *= val;
}
void Mat_DivideUChar(Mat m, uint8_t val) {
*m /= val;
}
void Mat_AddFloat(Mat m, float val) {
*m += val;
}
void Mat_SubtractFloat(Mat m, float val) {
*m -= val;
}
void Mat_MultiplyFloat(Mat m, float val) {
*m *= val;
}
void Mat_DivideFloat(Mat m, float val) {
*m /= val;
}
Mat Mat_MultiplyMatrix(Mat x, Mat y) {
return new cv::Mat((*x) * (*y));
}
Mat Mat_T(Mat x) {
return new cv::Mat(x->t());
}
void Mat_AbsDiff(Mat src1, Mat src2, Mat dst) {
cv::absdiff(*src1, *src2, *dst);
}
void Mat_Add(Mat src1, Mat src2, Mat dst) {
cv::add(*src1, *src2, *dst);
}
void Mat_AddWeighted(Mat src1, double alpha, Mat src2, double beta, double gamma, Mat dst) {
cv::addWeighted(*src1, alpha, *src2, beta, gamma, *dst);
}
void Mat_BitwiseAnd(Mat src1, Mat src2, Mat dst) {
cv::bitwise_and(*src1, *src2, *dst);
}
void Mat_BitwiseAndWithMask(Mat src1, Mat src2, Mat dst, Mat mask){
cv::bitwise_and(*src1, *src2, *dst, *mask);
}
void Mat_BitwiseNot(Mat src1, Mat dst) {
cv::bitwise_not(*src1, *dst);
}
void Mat_BitwiseNotWithMask(Mat src1, Mat dst, Mat mask) {
cv::bitwise_not(*src1, *dst, *mask);
}
void Mat_BitwiseOr(Mat src1, Mat src2, Mat dst) {
cv::bitwise_or(*src1, *src2, *dst);
}
void Mat_BitwiseOrWithMask(Mat src1, Mat src2, Mat dst, Mat mask) {
cv::bitwise_or(*src1, *src2, *dst, *mask);
}
void Mat_BitwiseXor(Mat src1, Mat src2, Mat dst) {
cv::bitwise_xor(*src1, *src2, *dst);
}
void Mat_BitwiseXorWithMask(Mat src1, Mat src2, Mat dst, Mat mask) {
cv::bitwise_xor(*src1, *src2, *dst, *mask);
}
void Mat_BatchDistance(Mat src1, Mat src2, Mat dist, int dtype, Mat nidx, int normType, int K,
Mat mask, int update, bool crosscheck) {
cv::batchDistance(*src1, *src2, *dist, dtype, *nidx, normType, K, *mask, update, crosscheck);
}
int Mat_BorderInterpolate(int p, int len, int borderType) {
return cv::borderInterpolate(p, len, borderType);
}
void Mat_CalcCovarMatrix(Mat samples, Mat covar, Mat mean, int flags, int ctype) {
cv::calcCovarMatrix(*samples, *covar, *mean, flags, ctype);
}
void Mat_CartToPolar(Mat x, Mat y, Mat magnitude, Mat angle, bool angleInDegrees) {
cv::cartToPolar(*x, *y, *magnitude, *angle, angleInDegrees);
}
bool Mat_CheckRange(Mat m) {
return cv::checkRange(*m);
}
void Mat_Compare(Mat src1, Mat src2, Mat dst, int ct) {
cv::compare(*src1, *src2, *dst, ct);
}
int Mat_CountNonZero(Mat src) {
return cv::countNonZero(*src);
}
void Mat_CompleteSymm(Mat m, bool lowerToUpper) {
cv::completeSymm(*m, lowerToUpper);
}
void Mat_ConvertScaleAbs(Mat src, Mat dst, double alpha, double beta) {
cv::convertScaleAbs(*src, *dst, alpha, beta);
}
void Mat_CopyMakeBorder(Mat src, Mat dst, int top, int bottom, int left, int right, int borderType,
Scalar value) {
cv::Scalar c_value(value.val1, value.val2, value.val3, value.val4);
cv::copyMakeBorder(*src, *dst, top, bottom, left, right, borderType, c_value);
}
void Mat_DCT(Mat src, Mat dst, int flags) {
cv::dct(*src, *dst, flags);
}
double Mat_Determinant(Mat m) {
return cv::determinant(*m);
}
void Mat_DFT(Mat m, Mat dst, int flags) {
cv::dft(*m, *dst, flags);
}
void Mat_Divide(Mat src1, Mat src2, Mat dst) {
cv::divide(*src1, *src2, *dst);
}
bool Mat_Eigen(Mat src, Mat eigenvalues, Mat eigenvectors) {
return cv::eigen(*src, *eigenvalues, *eigenvectors);
}
void Mat_EigenNonSymmetric(Mat src, Mat eigenvalues, Mat eigenvectors) {
cv::eigenNonSymmetric(*src, *eigenvalues, *eigenvectors);
}
void Mat_PCABackProject(Mat data, Mat mean, Mat eigenvectors, Mat result) {
cv::PCABackProject(*data, *mean, *eigenvectors, *result);
}
void Mat_PCACompute(Mat src, Mat mean, Mat eigenvectors, Mat eigenvalues, int maxComponents) {
cv::PCACompute(*src, *mean, *eigenvectors, *eigenvalues, maxComponents);
}
void Mat_PCAProject(Mat data, Mat mean, Mat eigenvectors, Mat result) {
cv::PCAProject(*data, *mean, *eigenvectors, *result);
}
double PSNR(Mat src1, Mat src2) {
return cv::PSNR(*src1, *src2);
}
void SVBackSubst(Mat w, Mat u, Mat vt, Mat rhs, Mat dst) {
cv::SVBackSubst(*w, *u, *vt, *rhs, *dst);
}
void SVDecomp(Mat src, Mat w, Mat u, Mat vt) {
cv::SVDecomp(*src, *w, *u, *vt);
}
void Mat_Exp(Mat src, Mat dst) {
cv::exp(*src, *dst);
}
void Mat_ExtractChannel(Mat src, Mat dst, int coi) {
cv::extractChannel(*src, *dst, coi);
}
void Mat_FindNonZero(Mat src, Mat idx) {
cv::findNonZero(*src, *idx);
}
void Mat_Flip(Mat src, Mat dst, int flipCode) {
cv::flip(*src, *dst, flipCode);
}
void Mat_Gemm(Mat src1, Mat src2, double alpha, Mat src3, double beta, Mat dst, int flags) {
cv::gemm(*src1, *src2, alpha, *src3, beta, *dst, flags);
}
int Mat_GetOptimalDFTSize(int vecsize) {
return cv::getOptimalDFTSize(vecsize);
}
void Mat_Hconcat(Mat src1, Mat src2, Mat dst) {
cv::hconcat(*src1, *src2, *dst);
}
void Mat_Vconcat(Mat src1, Mat src2, Mat dst) {
cv::vconcat(*src1, *src2, *dst);
}
void Rotate(Mat src, Mat dst, int rotateCode) {
cv::rotate(*src, *dst, rotateCode);
}
void Mat_Idct(Mat src, Mat dst, int flags) {
cv::idct(*src, *dst, flags);
}
void Mat_Idft(Mat src, Mat dst, int flags, int nonzeroRows) {
cv::idft(*src, *dst, flags, nonzeroRows);
}
void Mat_InRange(Mat src, Mat lowerb, Mat upperb, Mat dst) {
cv::inRange(*src, *lowerb, *upperb, *dst);
}
void Mat_InRangeWithScalar(Mat src, Scalar lowerb, Scalar upperb, Mat dst) {
cv::Scalar lb = cv::Scalar(lowerb.val1, lowerb.val2, lowerb.val3, lowerb.val4);
cv::Scalar ub = cv::Scalar(upperb.val1, upperb.val2, upperb.val3, upperb.val4);
cv::inRange(*src, lb, ub, *dst);
}
void Mat_InsertChannel(Mat src, Mat dst, int coi) {
cv::insertChannel(*src, *dst, coi);
}
double Mat_Invert(Mat src, Mat dst, int flags) {
double ret = cv::invert(*src, *dst, flags);
return ret;
}
double KMeans(Mat data, int k, Mat bestLabels, TermCriteria criteria, int attempts, int flags, Mat centers) {
double ret = cv::kmeans(*data, k, *bestLabels, *criteria, attempts, flags, *centers);
return ret;
}
double KMeansPoints(PointVector points, int k, Mat bestLabels, TermCriteria criteria, int attempts, int flags, Mat centers) {
std::vector<cv::Point2f> pts;
copyPointVectorToPoint2fVector(points, &pts);
double ret = cv::kmeans(pts, k, *bestLabels, *criteria, attempts, flags, *centers);
return ret;
}
void Mat_Log(Mat src, Mat dst) {
cv::log(*src, *dst);
}
void Mat_Magnitude(Mat x, Mat y, Mat magnitude) {
cv::magnitude(*x, *y, *magnitude);
}
double Mat_Mahalanobis(Mat v1, Mat v2, Mat icovar) {
return cv::Mahalanobis(*v1, *v2, *icovar);
}
void MulTransposed(Mat src, Mat dest, bool ata) {
cv::mulTransposed(*src, *dest, ata);
}
void Mat_Max(Mat src1, Mat src2, Mat dst) {
cv::max(*src1, *src2, *dst);
}
void Mat_MeanStdDev(Mat src, Mat dstMean, Mat dstStdDev) {
cv::meanStdDev(*src, *dstMean, *dstStdDev);
}
void Mat_Merge(struct Mats mats, Mat dst) {
std::vector<cv::Mat> images;
for (int i = 0; i < mats.length; ++i) {
images.push_back(*mats.mats[i]);
}
cv::merge(images, *dst);
}
void Mat_Min(Mat src1, Mat src2, Mat dst) {
cv::min(*src1, *src2, *dst);
}
void Mat_MinMaxIdx(Mat m, double* minVal, double* maxVal, int* minIdx, int* maxIdx) {
cv::minMaxIdx(*m, minVal, maxVal, minIdx, maxIdx);
}
void Mat_MinMaxLoc(Mat m, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc) {
cv::Point cMinLoc;
cv::Point cMaxLoc;
cv::minMaxLoc(*m, minVal, maxVal, &cMinLoc, &cMaxLoc);
minLoc->x = cMinLoc.x;
minLoc->y = cMinLoc.y;
maxLoc->x = cMaxLoc.x;
maxLoc->y = cMaxLoc.y;
}
void Mat_MinMaxLocWithMask(Mat m, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc, Mat mask) {
cv::Point cMinLoc;
cv::Point cMaxLoc;
cv::minMaxLoc(*m, minVal, maxVal, &cMinLoc, &cMaxLoc, *mask);
minLoc->x = cMinLoc.x;
minLoc->y = cMinLoc.y;
maxLoc->x = cMaxLoc.x;
maxLoc->y = cMaxLoc.y;
}
void Mat_MixChannels(struct Mats src, struct Mats dst, struct IntVector fromTo) {
std::vector<cv::Mat> srcMats;
for (int i = 0; i < src.length; ++i) {
srcMats.push_back(*src.mats[i]);
}
std::vector<cv::Mat> dstMats;
for (int i = 0; i < dst.length; ++i) {
dstMats.push_back(*dst.mats[i]);
}
std::vector<int> fromTos;
for (int i = 0; i < fromTo.length; ++i) {
fromTos.push_back(fromTo.val[i]);
}
cv::mixChannels(srcMats, dstMats, fromTos);
}
void Mat_MulSpectrums(Mat a, Mat b, Mat c, int flags) {
cv::mulSpectrums(*a, *b, *c, flags);
}
void Mat_Multiply(Mat src1, Mat src2, Mat dst) {
cv::multiply(*src1, *src2, *dst);
}
void Mat_MultiplyWithParams(Mat src1, Mat src2, Mat dst, double scale, int dtype) {
cv::multiply(*src1, *src2, *dst, scale, dtype);
}
void Mat_Normalize(Mat src, Mat dst, double alpha, double beta, int typ) {
cv::normalize(*src, *dst, alpha, beta, typ);
}
double Norm(Mat src1, int normType) {
return cv::norm(*src1, normType);
}
double NormWithMats(Mat src1, Mat src2, int normType) {
return cv::norm(*src1, *src2, normType);
}
void Mat_PerspectiveTransform(Mat src, Mat dst, Mat tm) {
cv::perspectiveTransform(*src, *dst, *tm);
}
bool Mat_Solve(Mat src1, Mat src2, Mat dst, int flags) {
return cv::solve(*src1, *src2, *dst, flags);
}
int Mat_SolveCubic(Mat coeffs, Mat roots) {
return cv::solveCubic(*coeffs, *roots);
}
double Mat_SolvePoly(Mat coeffs, Mat roots, int maxIters) {
return cv::solvePoly(*coeffs, *roots, maxIters);
}
void Mat_Reduce(Mat src, Mat dst, int dim, int rType, int dType) {
cv::reduce(*src, *dst, dim, rType, dType);
}
void Mat_ReduceArgMax(Mat src, Mat dst, int axis, bool lastIndex) {
cv::reduceArgMax(*src, *dst, axis, lastIndex);
}
void Mat_ReduceArgMin(Mat src, Mat dst, int axis, bool lastIndex) {
cv::reduceArgMin(*src, *dst, axis, lastIndex);
}
void Mat_Repeat(Mat src, int nY, int nX, Mat dst) {
cv::repeat(*src, nY, nX, *dst);
}
void Mat_ScaleAdd(Mat src1, double alpha, Mat src2, Mat dst) {
cv::scaleAdd(*src1, alpha, *src2, *dst);
}
void Mat_SetIdentity(Mat src, double scalar) {
cv::setIdentity(*src, scalar);
}
void Mat_Sort(Mat src, Mat dst, int flags) {
cv::sort(*src, *dst, flags);
}
void Mat_SortIdx(Mat src, Mat dst, int flags) {
cv::sortIdx(*src, *dst, flags);
}
void Mat_Split(Mat src, struct Mats* mats) {
std::vector<cv::Mat> channels;
cv::split(*src, channels);
mats->mats = new Mat[channels.size()];
for (size_t i = 0; i < channels.size(); ++i) {
mats->mats[i] = new cv::Mat(channels[i]);
}
mats->length = (int)channels.size();
}
void Mat_Subtract(Mat src1, Mat src2, Mat dst) {
cv::subtract(*src1, *src2, *dst);
}
Scalar Mat_Trace(Mat src) {
cv::Scalar c = cv::trace(*src);
Scalar scal = Scalar();
scal.val1 = c.val[0];
scal.val2 = c.val[1];
scal.val3 = c.val[2];
scal.val4 = c.val[3];
return scal;
}
void Mat_Transform(Mat src, Mat dst, Mat tm) {
cv::transform(*src, *dst, *tm);
}
void Mat_Transpose(Mat src, Mat dst) {
cv::transpose(*src, *dst);
}
void Mat_TransposeND(Mat src, struct IntVector order, Mat dst) {
std::vector<int> _order;
for (int i = 0, *v = order.val; i < order.length; ++v, ++i) {
_order.push_back(*v);
}
cv::transposeND(*src, _order, *dst);
}
void Mat_PolarToCart(Mat magnitude, Mat degree, Mat x, Mat y, bool angleInDegrees) {
cv::polarToCart(*magnitude, *degree, *x, *y, angleInDegrees);
}
void Mat_Pow(Mat src, double power, Mat dst) {
cv::pow(*src, power, *dst);
}
void Mat_Phase(Mat x, Mat y, Mat angle, bool angleInDegrees) {
cv::phase(*x, *y, *angle, angleInDegrees);
}
Scalar Mat_Sum(Mat src) {
cv::Scalar c = cv::sum(*src);
Scalar scal = Scalar();
scal.val1 = c.val[0];
scal.val2 = c.val[1];
scal.val3 = c.val[2];
scal.val4 = c.val[3];
return scal;
}
// TermCriteria_New creates a new TermCriteria
TermCriteria TermCriteria_New(int typ, int maxCount, double epsilon) {
return new cv::TermCriteria(typ, maxCount, epsilon);
}
void Contours_Close(struct Contours cs) {
for (int i = 0; i < cs.length; i++) {
Points_Close(cs.contours[i]);
}
delete[] cs.contours;
}
void CStrings_Close(struct CStrings cstrs) {
for ( int i = 0; i < cstrs.length; i++ ) {
delete [] cstrs.strs[i];
}
delete [] cstrs.strs;
}
void KeyPoints_Close(struct KeyPoints ks) {
delete[] ks.keypoints;
}
void Points_Close(Points ps) {
for (size_t i = 0; i < ps.length; i++) {
Point_Close(ps.points[i]);
}
delete[] ps.points;
}
void Point_Close(Point p) {}
void Points2f_Close(Points2f ps) {
for (size_t i = 0; i < ps.length; i++) {
Point2f_Close(ps.points[i]);
}
delete[] ps.points;
}
void Point2f_Close(Point2f p) {}
void Rects_Close(struct Rects rs) {
delete[] rs.rects;
}
void DMatches_Close(struct DMatches ds) {
delete[] ds.dmatches;
}
void MultiDMatches_Close(struct MultiDMatches mds) {
for (size_t i = 0; i < mds.length; i++) {
DMatches_Close(mds.dmatches[i]);
}
delete[] mds.dmatches;
}
struct DMatches MultiDMatches_get(struct MultiDMatches mds, int index) {
return mds.dmatches[index];
}
// since it is next to impossible to iterate over mats.mats on the cgo side
Mat Mats_get(struct Mats mats, int i) {
return mats.mats[i];
}
void Mats_Close(struct Mats mats) {
delete[] mats.mats;
}
void ByteArray_Release(struct ByteArray buf) {
delete[] buf.data;
}
struct ByteArray toByteArray(const char* buf, int len) {
ByteArray ret = {new char[len], len};
memcpy(ret.data, buf, len);
return ret;
}
int64 GetCVTickCount() {
return cv::getTickCount();
}
double GetTickFrequency() {
return cv::getTickFrequency();
}
Mat Mat_rowRange(Mat m,int startrow,int endrow) {
return new cv::Mat(m->rowRange(startrow,endrow));
}
Mat Mat_colRange(Mat m,int startrow,int endrow) {
return new cv::Mat(m->colRange(startrow,endrow));
}
PointVector PointVector_New() {
return new std::vector< cv::Point >;
}
PointVector PointVector_NewFromPoints(Contour points) {
std::vector<cv::Point>* cntr = new std::vector<cv::Point>;
for (size_t i = 0; i < points.length; i++) {
cntr->push_back(cv::Point(points.points[i].x, points.points[i].y));
}
return cntr;
}
PointVector PointVector_NewFromMat(Mat mat) {
std::vector<cv::Point>* pts = new std::vector<cv::Point>;
*pts = (std::vector<cv::Point>) *mat;
return pts;
}
Point PointVector_At(PointVector pv, int idx) {
cv::Point p = pv->at(idx);
return Point{.x = p.x, .y = p.y};
}
void PointVector_Append(PointVector pv, Point p) {
pv->push_back(cv::Point(p.x, p.y));
}
int PointVector_Size(PointVector p) {
return p->size();
}
void PointVector_Close(PointVector p) {
p->clear();
delete p;
}
PointsVector PointsVector_New() {
return new std::vector< std::vector< cv::Point > >;
}
PointsVector PointsVector_NewFromPoints(Contours points) {
std::vector< std::vector< cv::Point > >* pv = new std::vector< std::vector< cv::Point > >;
for (size_t i = 0; i < points.length; i++) {
Contour contour = points.contours[i];
std::vector<cv::Point> cntr;
for (size_t i = 0; i < contour.length; i++) {
cntr.push_back(cv::Point(contour.points[i].x, contour.points[i].y));
}
pv->push_back(cntr);
}
return pv;
}