-
Notifications
You must be signed in to change notification settings - Fork 870
/
core.go
2934 lines (2450 loc) · 93.7 KB
/
core.go
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
package gocv
/*
#include <stdlib.h>
#include "core.h"
*/
import "C"
import (
"errors"
"image"
"image/color"
"reflect"
"unsafe"
)
const (
// MatChannels1 is a single channel Mat.
MatChannels1 = 0
// MatChannels2 is 2 channel Mat.
MatChannels2 = 8
// MatChannels3 is 3 channel Mat.
MatChannels3 = 16
// MatChannels4 is 4 channel Mat.
MatChannels4 = 24
)
// MatType is the type for the various different kinds of Mat you can create.
type MatType int
const (
// MatTypeCV8U is a Mat of 8-bit unsigned int
MatTypeCV8U MatType = 0
// MatTypeCV8S is a Mat of 8-bit signed int
MatTypeCV8S MatType = 1
// MatTypeCV16U is a Mat of 16-bit unsigned int
MatTypeCV16U MatType = 2
// MatTypeCV16S is a Mat of 16-bit signed int
MatTypeCV16S MatType = 3
// MatTypeCV16SC2 is a Mat of 16-bit signed int with 2 channels
MatTypeCV16SC2 = MatTypeCV16S + MatChannels2
// MatTypeCV32S is a Mat of 32-bit signed int
MatTypeCV32S MatType = 4
// MatTypeCV32F is a Mat of 32-bit float
MatTypeCV32F MatType = 5
// MatTypeCV64F is a Mat of 64-bit float
MatTypeCV64F MatType = 6
// MatTypeCV16F is a Mat of 16-bit (half) float
MatTypeCV16F MatType = 7
// MatTypeCV8UC1 is a Mat of 8-bit unsigned int with a single channel
MatTypeCV8UC1 = MatTypeCV8U + MatChannels1
// MatTypeCV8UC2 is a Mat of 8-bit unsigned int with 2 channels
MatTypeCV8UC2 = MatTypeCV8U + MatChannels2
// MatTypeCV8UC3 is a Mat of 8-bit unsigned int with 3 channels
MatTypeCV8UC3 = MatTypeCV8U + MatChannels3
// MatTypeCV8UC4 is a Mat of 8-bit unsigned int with 4 channels
MatTypeCV8UC4 = MatTypeCV8U + MatChannels4
// MatTypeCV8SC1 is a Mat of 8-bit signed int with a single channel
MatTypeCV8SC1 = MatTypeCV8S + MatChannels1
// MatTypeCV8SC2 is a Mat of 8-bit signed int with 2 channels
MatTypeCV8SC2 = MatTypeCV8S + MatChannels2
// MatTypeCV8SC3 is a Mat of 8-bit signed int with 3 channels
MatTypeCV8SC3 = MatTypeCV8S + MatChannels3
// MatTypeCV8SC4 is a Mat of 8-bit signed int with 4 channels
MatTypeCV8SC4 = MatTypeCV8S + MatChannels4
// MatTypeCV16UC1 is a Mat of 16-bit unsigned int with a single channel
MatTypeCV16UC1 = MatTypeCV16U + MatChannels1
// MatTypeCV16UC2 is a Mat of 16-bit unsigned int with 2 channels
MatTypeCV16UC2 = MatTypeCV16U + MatChannels2
// MatTypeCV16UC3 is a Mat of 16-bit unsigned int with 3 channels
MatTypeCV16UC3 = MatTypeCV16U + MatChannels3
// MatTypeCV16UC4 is a Mat of 16-bit unsigned int with 4 channels
MatTypeCV16UC4 = MatTypeCV16U + MatChannels4
// MatTypeCV16SC1 is a Mat of 16-bit signed int with a single channel
MatTypeCV16SC1 = MatTypeCV16S + MatChannels1
// MatTypeCV16SC3 is a Mat of 16-bit signed int with 3 channels
MatTypeCV16SC3 = MatTypeCV16S + MatChannels3
// MatTypeCV16SC4 is a Mat of 16-bit signed int with 4 channels
MatTypeCV16SC4 = MatTypeCV16S + MatChannels4
// MatTypeCV32SC1 is a Mat of 32-bit signed int with a single channel
MatTypeCV32SC1 = MatTypeCV32S + MatChannels1
// MatTypeCV32SC2 is a Mat of 32-bit signed int with 2 channels
MatTypeCV32SC2 = MatTypeCV32S + MatChannels2
// MatTypeCV32SC3 is a Mat of 32-bit signed int with 3 channels
MatTypeCV32SC3 = MatTypeCV32S + MatChannels3
// MatTypeCV32SC4 is a Mat of 32-bit signed int with 4 channels
MatTypeCV32SC4 = MatTypeCV32S + MatChannels4
// MatTypeCV32FC1 is a Mat of 32-bit float int with a single channel
MatTypeCV32FC1 = MatTypeCV32F + MatChannels1
// MatTypeCV32FC2 is a Mat of 32-bit float int with 2 channels
MatTypeCV32FC2 = MatTypeCV32F + MatChannels2
// MatTypeCV32FC3 is a Mat of 32-bit float int with 3 channels
MatTypeCV32FC3 = MatTypeCV32F + MatChannels3
// MatTypeCV32FC4 is a Mat of 32-bit float int with 4 channels
MatTypeCV32FC4 = MatTypeCV32F + MatChannels4
// MatTypeCV64FC1 is a Mat of 64-bit float int with a single channel
MatTypeCV64FC1 = MatTypeCV64F + MatChannels1
// MatTypeCV64FC2 is a Mat of 64-bit float int with 2 channels
MatTypeCV64FC2 = MatTypeCV64F + MatChannels2
// MatTypeCV64FC3 is a Mat of 64-bit float int with 3 channels
MatTypeCV64FC3 = MatTypeCV64F + MatChannels3
// MatTypeCV64FC4 is a Mat of 64-bit float int with 4 channels
MatTypeCV64FC4 = MatTypeCV64F + MatChannels4
// MatTypeCV16FC1 is a Mat of 16-bit float with a single channel
MatTypeCV16FC1 = MatTypeCV16F + MatChannels1
// MatTypeCV16FC2 is a Mat of 16-bit float with 2 channels
MatTypeCV16FC2 = MatTypeCV16F + MatChannels2
// MatTypeCV16FC3 is a Mat of 16-bit float with 3 channels
MatTypeCV16FC3 = MatTypeCV16F + MatChannels3
// MatTypeCV16FC4 is a Mat of 16-bit float with 4 channels
MatTypeCV16FC4 = MatTypeCV16F + MatChannels4
)
// CompareType is used for Compare operations to indicate which kind of
// comparison to use.
type CompareType int
const (
// CompareEQ src1 is equal to src2.
CompareEQ CompareType = 0
// CompareGT src1 is greater than src2.
CompareGT CompareType = 1
// CompareGE src1 is greater than or equal to src2.
CompareGE CompareType = 2
// CompareLT src1 is less than src2.
CompareLT CompareType = 3
// CompareLE src1 is less than or equal to src2.
CompareLE CompareType = 4
// CompareNE src1 is unequal to src2.
CompareNE CompareType = 5
)
type Point2f struct {
X float32
Y float32
}
func NewPoint2f(x, y float32) Point2f {
return Point2f{x, y}
}
var ErrEmptyByteSlice = errors.New("empty byte array")
// Mat represents an n-dimensional dense numerical single-channel
// or multi-channel array. It can be used to store real or complex-valued
// vectors and matrices, grayscale or color images, voxel volumes,
// vector fields, point clouds, tensors, and histograms.
//
// For further details, please see:
// http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html
type Mat struct {
p C.Mat
// Non-nil if Mat was created with a []byte (using NewMatFromBytes()). Nil otherwise.
d []byte
}
// NewMat returns a new empty Mat.
func NewMat() Mat {
return newMat(C.Mat_New())
}
// NewMatFromCMat returns a new Mat from an unsafe.Pointer(C.Mat).
func NewMatFromCMat(c_mat unsafe.Pointer) Mat {
return newMat(C.Mat(c_mat))
}
// NewMatWithSize returns a new Mat with a specific size and type.
func NewMatWithSize(rows int, cols int, mt MatType) Mat {
return newMat(C.Mat_NewWithSize(C.int(rows), C.int(cols), C.int(mt)))
}
// NewMatWithSizes returns a new multidimensional Mat with a specific size and type.
func NewMatWithSizes(sizes []int, mt MatType) Mat {
sizesArray := make([]C.int, len(sizes))
for i, s := range sizes {
sizesArray[i] = C.int(s)
}
sizesIntVector := C.IntVector{
val: (*C.int)(&sizesArray[0]),
length: C.int(len(sizes)),
}
return newMat(C.Mat_NewWithSizes(sizesIntVector, C.int(mt)))
}
// NewMatWithSizesWithScalar returns a new multidimensional Mat with a specific size, type and scalar value.
func NewMatWithSizesWithScalar(sizes []int, mt MatType, s Scalar) Mat {
csizes := []C.int{}
for _, v := range sizes {
csizes = append(csizes, C.int(v))
}
sizesVector := C.struct_IntVector{}
sizesVector.val = (*C.int)(&csizes[0])
sizesVector.length = (C.int)(len(csizes))
sVal := C.struct_Scalar{
val1: C.double(s.Val1),
val2: C.double(s.Val2),
val3: C.double(s.Val3),
val4: C.double(s.Val4),
}
return newMat(C.Mat_NewWithSizesFromScalar(sizesVector, C.int(mt), sVal))
}
// NewMatWithSizesWithScalar returns a new multidimensional Mat with a specific size, type and preexisting data.
func NewMatWithSizesFromBytes(sizes []int, mt MatType, data []byte) (Mat, error) {
cBytes, err := toByteArray(data)
if err != nil {
return Mat{}, err
}
csizes := []C.int{}
for _, v := range sizes {
csizes = append(csizes, C.int(v))
}
sizesVector := C.struct_IntVector{}
sizesVector.val = (*C.int)(&csizes[0])
sizesVector.length = (C.int)(len(csizes))
return newMat(C.Mat_NewWithSizesFromBytes(sizesVector, C.int(mt), *cBytes)), nil
}
// NewMatFromScalar returns a new Mat for a specific Scalar value
func NewMatFromScalar(s Scalar, mt MatType) Mat {
sVal := C.struct_Scalar{
val1: C.double(s.Val1),
val2: C.double(s.Val2),
val3: C.double(s.Val3),
val4: C.double(s.Val4),
}
return newMat(C.Mat_NewFromScalar(sVal, C.int(mt)))
}
// NewMatWithSizeFromScalar returns a new Mat for a specific Scala value with a specific size and type
// This simplifies creation of specific color filters or creating Mats of specific colors and sizes
func NewMatWithSizeFromScalar(s Scalar, rows int, cols int, mt MatType) Mat {
sVal := C.struct_Scalar{
val1: C.double(s.Val1),
val2: C.double(s.Val2),
val3: C.double(s.Val3),
val4: C.double(s.Val4),
}
return newMat(C.Mat_NewWithSizeFromScalar(sVal, C.int(rows), C.int(cols), C.int(mt)))
}
// NewMatFromBytes returns a new Mat with a specific size and type, initialized from a []byte.
func NewMatFromBytes(rows int, cols int, mt MatType, data []byte) (Mat, error) {
cBytes, err := toByteArray(data)
if err != nil {
return Mat{}, err
}
mat := newMat(C.Mat_NewFromBytes(C.int(rows), C.int(cols), C.int(mt), *cBytes))
// Store a reference to the backing data slice. This is needed because we pass the backing
// array directly to C code and without keeping a Go reference to it, it might end up
// garbage collected which would result in crashes.
//
// TODO(bga): This could live in newMat() but I wanted to reduce the change surface.
// TODO(bga): Code that needs access to the array from Go could use this directly.
mat.d = data
return mat, nil
}
// Returns an identity matrix of the specified size and type.
//
// The method returns a Matlab-style identity matrix initializer, similarly to Mat::zeros. Similarly to Mat::ones.
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a2cf9b9acde7a9852542bbc20ef851ed2
func Eye(rows int, cols int, mt MatType) Mat {
return newMat(C.Eye(C.int(rows), C.int(cols), C.int(mt)))
}
// Returns a zero array of the specified size and type.
//
// The method returns a Matlab-style zero array initializer.
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556
func Zeros(rows int, cols int, mt MatType) Mat {
return newMat(C.Zeros(C.int(rows), C.int(cols), C.int(mt)))
}
// Returns an array of all 1's of the specified size and type.
//
// The method returns a Matlab-style 1's array initializer
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a69ae0402d116fc9c71908d8508dc2f09
func Ones(rows int, cols int, mt MatType) Mat {
return newMat(C.Ones(C.int(rows), C.int(cols), C.int(mt)))
}
// FromPtr returns a new Mat with a specific size and type, initialized from a Mat Ptr.
func (m *Mat) FromPtr(rows int, cols int, mt MatType, prow int, pcol int) (Mat, error) {
return newMat(C.Mat_FromPtr(m.p, C.int(rows), C.int(cols), C.int(mt), C.int(prow), C.int(pcol))), nil
}
// Ptr returns the Mat's underlying object pointer.
func (m *Mat) Ptr() C.Mat {
return m.p
}
// Empty determines if the Mat is empty or not.
func (m *Mat) Empty() bool {
isEmpty := C.Mat_Empty(m.p)
return isEmpty != 0
}
// Closed determines if the Mat is closed or not.
func (m *Mat) Closed() bool {
return m.p == nil
}
// IsContinuous determines if the Mat is continuous.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#aa90cea495029c7d1ee0a41361ccecdf3
func (m *Mat) IsContinuous() bool {
return bool(C.Mat_IsContinuous(m.p))
}
// Inv inverses a matrix.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a039eb3c6740a850696a12519a4b8bfc6
func (m *Mat) Inv() {
C.Mat_Inv(m.p)
}
// Col creates a matrix header for the specified matrix column.
// The underlying data of the new matrix is shared with the original matrix.
func (m *Mat) Col(col int) Mat {
return newMat(C.Mat_Col(m.p, C.int(col)))
}
// Row creates a matrix header for the specified matrix row.
// The underlying data of the new matrix is shared with the original matrix.
func (m *Mat) Row(row int) Mat {
return newMat(C.Mat_Row(m.p, C.int(row)))
}
// Clone returns a cloned full copy of the Mat.
func (m *Mat) Clone() Mat {
return newMat(C.Mat_Clone(m.p))
}
// CopyTo copies Mat into destination Mat.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a33fd5d125b4c302b0c9aa86980791a77
func (m *Mat) CopyTo(dst *Mat) {
C.Mat_CopyTo(m.p, dst.p)
return
}
// CopyToWithMask copies Mat into destination Mat after applying the mask Mat.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a626fe5f96d02525e2604d2ad46dd574f
func (m *Mat) CopyToWithMask(dst *Mat, mask Mat) {
C.Mat_CopyToWithMask(m.p, dst.p, mask.p)
return
}
// ConvertTo converts Mat into destination Mat.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#adf88c60c5b4980e05bb556080916978b
func (m *Mat) ConvertTo(dst *Mat, mt MatType) {
C.Mat_ConvertTo(m.p, dst.p, C.int(mt))
return
}
func (m *Mat) ConvertToWithParams(dst *Mat, mt MatType, alpha, beta float32) {
C.Mat_ConvertToWithParams(m.p, dst.p, C.int(mt), C.float(alpha), C.float(beta))
return
}
// Total returns the total number of array elements.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#aa4d317d43fb0cba9c2503f3c61b866c8
func (m *Mat) Total() int {
return int(C.Mat_Total(m.p))
}
// Size returns an array with one element for each dimension containing the size of that dimension for the Mat.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#aa4d317d43fb0cba9c2503f3c61b866c8
func (m *Mat) Size() (dims []int) {
cdims := C.IntVector{}
C.Mat_Size(m.p, &cdims)
defer C.IntVector_Close(cdims)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cdims.val)),
Len: int(cdims.length),
Cap: int(cdims.length),
}
pdims := *(*[]C.int)(unsafe.Pointer(h))
for i := 0; i < int(cdims.length); i++ {
dims = append(dims, int(pdims[i]))
}
return
}
// ToBytes copies the underlying Mat data to a byte array.
//
// For further details, please see:
// https://docs.opencv.org/3.3.1/d3/d63/classcv_1_1Mat.html#a4d33bed1c850265370d2af0ff02e1564
func (m *Mat) ToBytes() []byte {
b := C.Mat_DataPtr(m.p)
return toGoBytes(b)
}
// DataPtrUint8 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrUint8() ([]uint8, error) {
if !m.IsContinuous() {
return nil, errors.New("DataPtrUint8 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length),
Cap: int(p.length),
}
return *(*[]uint8)(unsafe.Pointer(h)), nil
}
// DataPtrInt8 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrInt8() ([]int8, error) {
if m.Type()&MatTypeCV8S != MatTypeCV8S {
return nil, errors.New("DataPtrInt8 only supports MatTypeCV8S")
}
if !m.IsContinuous() {
return nil, errors.New("DataPtrInt8 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length),
Cap: int(p.length),
}
return *(*[]int8)(unsafe.Pointer(h)), nil
}
// DataPtrUint16 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrUint16() ([]uint16, error) {
if m.Type()&MatTypeCV16U != MatTypeCV16U {
return nil, errors.New("DataPtrUint16 only supports MatTypeCV16U")
}
if !m.IsContinuous() {
return nil, errors.New("DataPtrUint16 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length) / 2,
Cap: int(p.length) / 2,
}
return *(*[]uint16)(unsafe.Pointer(h)), nil
}
// DataPtrInt16 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrInt16() ([]int16, error) {
if m.Type()&MatTypeCV16S != MatTypeCV16S {
return nil, errors.New("DataPtrInt16 only supports MatTypeCV16S")
}
if !m.IsContinuous() {
return nil, errors.New("DataPtrInt16 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length) / 2,
Cap: int(p.length) / 2,
}
return *(*[]int16)(unsafe.Pointer(h)), nil
}
// DataPtrFloat32 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrFloat32() ([]float32, error) {
if m.Type()&MatTypeCV32F != MatTypeCV32F {
return nil, errors.New("DataPtrFloat32 only supports MatTypeCV32F")
}
if !m.IsContinuous() {
return nil, errors.New("DataPtrFloat32 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length) / 4,
Cap: int(p.length) / 4,
}
return *(*[]float32)(unsafe.Pointer(h)), nil
}
// DataPtrFloat64 returns a slice that references the OpenCV allocated data.
//
// The data is no longer valid once the Mat has been closed. Any data that
// needs to be accessed after the Mat is closed must be copied into Go memory.
func (m *Mat) DataPtrFloat64() ([]float64, error) {
if m.Type()&MatTypeCV64F != MatTypeCV64F {
return nil, errors.New("DataPtrFloat64 only supports MatTypeCV64F")
}
if !m.IsContinuous() {
return nil, errors.New("DataPtrFloat64 requires continuous Mat")
}
p := C.Mat_DataPtr(m.p)
h := &reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(p.data)),
Len: int(p.length) / 8,
Cap: int(p.length) / 8,
}
return *(*[]float64)(unsafe.Pointer(h)), nil
}
// Region returns a new Mat that points to a region of this Mat. Changes made to the
// region Mat will affect the original Mat, since they are pointers to the underlying
// OpenCV Mat object.
func (m *Mat) Region(rio image.Rectangle) Mat {
cRect := C.struct_Rect{
x: C.int(rio.Min.X),
y: C.int(rio.Min.Y),
width: C.int(rio.Size().X),
height: C.int(rio.Size().Y),
}
return newMat(C.Mat_Region(m.p, cRect))
}
// Reshape changes the shape and/or the number of channels of a 2D matrix without copying the data.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a4eb96e3251417fa88b78e2abd6cfd7d8
func (m *Mat) Reshape(cn int, rows int) Mat {
return newMat(C.Mat_Reshape(m.p, C.int(cn), C.int(rows)))
}
// ConvertFp16 converts a Mat to half-precision floating point.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga9c25d9ef44a2a48ecc3774b30cb80082
func (m *Mat) ConvertFp16() Mat {
return newMat(C.Mat_ConvertFp16(m.p))
}
// Mean calculates the mean value M of array elements, independently for each channel, and return it as Scalar
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga191389f8a0e58180bb13a727782cd461
func (m *Mat) Mean() Scalar {
s := C.Mat_Mean(m.p)
return NewScalar(float64(s.val1), float64(s.val2), float64(s.val3), float64(s.val4))
}
// MeanWithMask calculates the mean value M of array elements,independently for each channel,
// and returns it as Scalar vector while applying the mask.
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga191389f8a0e58180bb13a727782cd461
func (m *Mat) MeanWithMask(mask Mat) Scalar {
s := C.Mat_MeanWithMask(m.p, mask.p)
return NewScalar(float64(s.val1), float64(s.val2), float64(s.val3), float64(s.val4))
}
// Sqrt calculates a square root of array elements.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga186222c3919657890f88df5a1f64a7d7
func (m *Mat) Sqrt() Mat {
return newMat(C.Mat_Sqrt(m.p))
}
// Sum calculates the per-channel pixel sum of an image.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga716e10a2dd9e228e4d3c95818f106722
func (m *Mat) Sum() Scalar {
s := C.Mat_Sum(m.p)
return NewScalar(float64(s.val1), float64(s.val2), float64(s.val3), float64(s.val4))
}
// PatchNaNs converts NaN's to zeros.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga62286befb7cde3568ff8c7d14d5079da
func (m *Mat) PatchNaNs() {
C.Mat_PatchNaNs(m.p)
}
// LUT performs a look-up table transform of an array.
//
// The function LUT fills the output array with values from the look-up table.
// Indices of the entries are taken from the input array.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gab55b8d062b7f5587720ede032d34156f
func LUT(src, wbLUT Mat, dst *Mat) {
C.LUT(src.p, wbLUT.p, dst.p)
}
// Rows returns the number of rows for this Mat.
func (m *Mat) Rows() int {
return int(C.Mat_Rows(m.p))
}
// Cols returns the number of columns for this Mat.
func (m *Mat) Cols() int {
return int(C.Mat_Cols(m.p))
}
// Channels returns the number of channels for this Mat.
func (m *Mat) Channels() int {
return int(C.Mat_Channels(m.p))
}
// Type returns the type for this Mat.
func (m *Mat) Type() MatType {
return MatType(C.Mat_Type(m.p))
}
// Step returns the number of bytes each matrix row occupies.
func (m *Mat) Step() int {
return int(C.Mat_Step(m.p))
}
// ElemSize returns the matrix element size in bytes.
func (m *Mat) ElemSize() int {
return int(C.Mat_ElemSize(m.p))
}
// GetUCharAt returns a value from a specific row/col
// in this Mat expecting it to be of type uchar aka CV_8U.
func (m *Mat) GetUCharAt(row int, col int) uint8 {
return uint8(C.Mat_GetUChar(m.p, C.int(row), C.int(col)))
}
// GetUCharAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type uchar aka CV_8U.
func (m *Mat) GetUCharAt3(x, y, z int) uint8 {
return uint8(C.Mat_GetUChar3(m.p, C.int(x), C.int(y), C.int(z)))
}
// GetSCharAt returns a value from a specific row/col
// in this Mat expecting it to be of type schar aka CV_8S.
func (m *Mat) GetSCharAt(row int, col int) int8 {
return int8(C.Mat_GetSChar(m.p, C.int(row), C.int(col)))
}
// GetSCharAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type schar aka CV_8S.
func (m *Mat) GetSCharAt3(x, y, z int) int8 {
return int8(C.Mat_GetSChar3(m.p, C.int(x), C.int(y), C.int(z)))
}
// GetShortAt returns a value from a specific row/col
// in this Mat expecting it to be of type short aka CV_16S.
func (m *Mat) GetShortAt(row int, col int) int16 {
return int16(C.Mat_GetShort(m.p, C.int(row), C.int(col)))
}
// GetShortAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type short aka CV_16S.
func (m *Mat) GetShortAt3(x, y, z int) int16 {
return int16(C.Mat_GetShort3(m.p, C.int(x), C.int(y), C.int(z)))
}
// GetIntAt returns a value from a specific row/col
// in this Mat expecting it to be of type int aka CV_32S.
func (m *Mat) GetIntAt(row int, col int) int32 {
return int32(C.Mat_GetInt(m.p, C.int(row), C.int(col)))
}
// GetIntAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type int aka CV_32S.
func (m *Mat) GetIntAt3(x, y, z int) int32 {
return int32(C.Mat_GetInt3(m.p, C.int(x), C.int(y), C.int(z)))
}
// GetFloatAt returns a value from a specific row/col
// in this Mat expecting it to be of type float aka CV_32F.
func (m *Mat) GetFloatAt(row int, col int) float32 {
return float32(C.Mat_GetFloat(m.p, C.int(row), C.int(col)))
}
// GetFloatAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type float aka CV_32F.
func (m *Mat) GetFloatAt3(x, y, z int) float32 {
return float32(C.Mat_GetFloat3(m.p, C.int(x), C.int(y), C.int(z)))
}
// GetDoubleAt returns a value from a specific row/col
// in this Mat expecting it to be of type double aka CV_64F.
func (m *Mat) GetDoubleAt(row int, col int) float64 {
return float64(C.Mat_GetDouble(m.p, C.int(row), C.int(col)))
}
// GetDoubleAt3 returns a value from a specific x, y, z coordinate location
// in this Mat expecting it to be of type double aka CV_64F.
func (m *Mat) GetDoubleAt3(x, y, z int) float64 {
return float64(C.Mat_GetDouble3(m.p, C.int(x), C.int(y), C.int(z)))
}
// SetTo sets all or some of the array elements to the specified scalar value.
func (m *Mat) SetTo(s Scalar) {
sVal := C.struct_Scalar{
val1: C.double(s.Val1),
val2: C.double(s.Val2),
val3: C.double(s.Val3),
val4: C.double(s.Val4),
}
C.Mat_SetTo(m.p, sVal)
}
// SetUCharAt sets a value at a specific row/col
// in this Mat expecting it to be of type uchar aka CV_8U.
func (m *Mat) SetUCharAt(row int, col int, val uint8) {
C.Mat_SetUChar(m.p, C.int(row), C.int(col), C.uint8_t(val))
}
// SetUCharAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type uchar aka CV_8U.
func (m *Mat) SetUCharAt3(x, y, z int, val uint8) {
C.Mat_SetUChar3(m.p, C.int(x), C.int(y), C.int(z), C.uint8_t(val))
}
// SetSCharAt sets a value at a specific row/col
// in this Mat expecting it to be of type schar aka CV_8S.
func (m *Mat) SetSCharAt(row int, col int, val int8) {
C.Mat_SetSChar(m.p, C.int(row), C.int(col), C.int8_t(val))
}
// SetSCharAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type schar aka CV_8S.
func (m *Mat) SetSCharAt3(x, y, z int, val int8) {
C.Mat_SetSChar3(m.p, C.int(x), C.int(y), C.int(z), C.int8_t(val))
}
// SetShortAt sets a value at a specific row/col
// in this Mat expecting it to be of type short aka CV_16S.
func (m *Mat) SetShortAt(row int, col int, val int16) {
C.Mat_SetShort(m.p, C.int(row), C.int(col), C.int16_t(val))
}
// SetShortAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type short aka CV_16S.
func (m *Mat) SetShortAt3(x, y, z int, val int16) {
C.Mat_SetShort3(m.p, C.int(x), C.int(y), C.int(z), C.int16_t(val))
}
// SetIntAt sets a value at a specific row/col
// in this Mat expecting it to be of type int aka CV_32S.
func (m *Mat) SetIntAt(row int, col int, val int32) {
C.Mat_SetInt(m.p, C.int(row), C.int(col), C.int32_t(val))
}
// SetIntAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type int aka CV_32S.
func (m *Mat) SetIntAt3(x, y, z int, val int32) {
C.Mat_SetInt3(m.p, C.int(x), C.int(y), C.int(z), C.int32_t(val))
}
// SetFloatAt sets a value at a specific row/col
// in this Mat expecting it to be of type float aka CV_32F.
func (m *Mat) SetFloatAt(row int, col int, val float32) {
C.Mat_SetFloat(m.p, C.int(row), C.int(col), C.float(val))
}
// SetFloatAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type float aka CV_32F.
func (m *Mat) SetFloatAt3(x, y, z int, val float32) {
C.Mat_SetFloat3(m.p, C.int(x), C.int(y), C.int(z), C.float(val))
}
// SetDoubleAt sets a value at a specific row/col
// in this Mat expecting it to be of type double aka CV_64F.
func (m *Mat) SetDoubleAt(row int, col int, val float64) {
C.Mat_SetDouble(m.p, C.int(row), C.int(col), C.double(val))
}
// SetDoubleAt3 sets a value at a specific x, y, z coordinate location
// in this Mat expecting it to be of type double aka CV_64F.
func (m *Mat) SetDoubleAt3(x, y, z int, val float64) {
C.Mat_SetDouble3(m.p, C.int(x), C.int(y), C.int(z), C.double(val))
}
// AddUChar adds a uchar value to each element in the Mat. Performs a
// mat += val operation.
func (m *Mat) AddUChar(val uint8) {
C.Mat_AddUChar(m.p, C.uint8_t(val))
}
// SubtractUChar subtracts a uchar value from each element in the Mat. Performs a
// mat -= val operation.
func (m *Mat) SubtractUChar(val uint8) {
C.Mat_SubtractUChar(m.p, C.uint8_t(val))
}
// MultiplyUChar multiplies each element in the Mat by a uint value. Performs a
// mat *= val operation.
func (m *Mat) MultiplyUChar(val uint8) {
C.Mat_MultiplyUChar(m.p, C.uint8_t(val))
}
// DivideUChar divides each element in the Mat by a uint value. Performs a
// mat /= val operation.
func (m *Mat) DivideUChar(val uint8) {
C.Mat_DivideUChar(m.p, C.uint8_t(val))
}
// AddFloat adds a float value to each element in the Mat. Performs a
// mat += val operation.
func (m *Mat) AddFloat(val float32) {
C.Mat_AddFloat(m.p, C.float(val))
}
// SubtractFloat subtracts a float value from each element in the Mat. Performs a
// mat -= val operation.
func (m *Mat) SubtractFloat(val float32) {
C.Mat_SubtractFloat(m.p, C.float(val))
}
// MultiplyFloat multiplies each element in the Mat by a float value. Performs a
// mat *= val operation.
func (m *Mat) MultiplyFloat(val float32) {
C.Mat_MultiplyFloat(m.p, C.float(val))
}
// DivideFloat divides each element in the Mat by a float value. Performs a
// mat /= val operation.
func (m *Mat) DivideFloat(val float32) {
C.Mat_DivideFloat(m.p, C.float(val))
}
// MultiplyMatrix multiplies matrix (m*x)
func (m *Mat) MultiplyMatrix(x Mat) Mat {
return newMat(C.Mat_MultiplyMatrix(m.p, x.p))
}
// T transpose matrix
// https://docs.opencv.org/4.1.2/d3/d63/classcv_1_1Mat.html#aaa428c60ccb6d8ea5de18f63dfac8e11
func (m *Mat) T() Mat {
return newMat(C.Mat_T(m.p))
}
// AbsDiff calculates the per-element absolute difference between two arrays
// or between an array and a scalar.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga6fef31bc8c4071cbc114a758a2b79c14
func AbsDiff(src1, src2 Mat, dst *Mat) {
C.Mat_AbsDiff(src1.p, src2.p, dst.p)
}
// Add calculates the per-element sum of two arrays or an array and a scalar.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga10ac1bfb180e2cfda1701d06c24fdbd6
func Add(src1, src2 Mat, dst *Mat) {
C.Mat_Add(src1.p, src2.p, dst.p)
}
// AddWeighted calculates the weighted sum of two arrays.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gafafb2513349db3bcff51f54ee5592a19
func AddWeighted(src1 Mat, alpha float64, src2 Mat, beta float64, gamma float64, dst *Mat) {
C.Mat_AddWeighted(src1.p, C.double(alpha),
src2.p, C.double(beta), C.double(gamma), dst.p)
}
// BitwiseAnd computes bitwise conjunction of the two arrays (dst = src1 & src2).
// Calculates the per-element bit-wise conjunction of two arrays
// or an array and a scalar.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga60b4d04b251ba5eb1392c34425497e14
func BitwiseAnd(src1 Mat, src2 Mat, dst *Mat) {
C.Mat_BitwiseAnd(src1.p, src2.p, dst.p)
}
// BitwiseAndWithMask computes bitwise conjunction of the two arrays (dst = src1 & src2).
// Calculates the per-element bit-wise conjunction of two arrays
// or an array and a scalar. It has an additional parameter for a mask.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga60b4d04b251ba5eb1392c34425497e14
func BitwiseAndWithMask(src1 Mat, src2 Mat, dst *Mat, mask Mat) {
C.Mat_BitwiseAndWithMask(src1.p, src2.p, dst.p, mask.p)
}
// BitwiseNot inverts every bit of an array.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga0002cf8b418479f4cb49a75442baee2f
func BitwiseNot(src1 Mat, dst *Mat) {
C.Mat_BitwiseNot(src1.p, dst.p)
}
// BitwiseNotWithMask inverts every bit of an array. It has an additional parameter for a mask.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga0002cf8b418479f4cb49a75442baee2f
func BitwiseNotWithMask(src1 Mat, dst *Mat, mask Mat) {
C.Mat_BitwiseNotWithMask(src1.p, dst.p, mask.p)
}
// BitwiseOr calculates the per-element bit-wise disjunction of two arrays
// or an array and a scalar.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gab85523db362a4e26ff0c703793a719b4
func BitwiseOr(src1 Mat, src2 Mat, dst *Mat) {
C.Mat_BitwiseOr(src1.p, src2.p, dst.p)
}
// BitwiseOrWithMask calculates the per-element bit-wise disjunction of two arrays
// or an array and a scalar. It has an additional parameter for a mask.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#gab85523db362a4e26ff0c703793a719b4
func BitwiseOrWithMask(src1 Mat, src2 Mat, dst *Mat, mask Mat) {
C.Mat_BitwiseOrWithMask(src1.p, src2.p, dst.p, mask.p)
}