forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
1997 lines (1690 loc) · 63.1 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 = 1
// MatTypeCV16U is a Mat of 16-bit unsigned int
MatTypeCV16U = 2
// MatTypeCV16S is a Mat of 16-bit signed int
MatTypeCV16S = 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 = 4
// MatTypeCV32F is a Mat of 32-bit float
MatTypeCV32F = 5
// MatTypeCV64F is a Mat of 64-bit float
MatTypeCV64F = 6
// 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
)
// 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 = 1
// CompareGE src1 is greater than or equal to src2.
CompareGE = 2
// CompareLT src1 is less than src2.
CompareLT = 3
// CompareLE src1 is less than or equal to src2.
CompareLE = 4
// CompareNE src1 is unequal to src2.
CompareNE = 5
)
type Point2f struct {
X float32
Y float32
}
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
}
// NewMat returns a new empty Mat.
func NewMat() Mat {
return newMat(C.Mat_New())
}
// 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)))
}
// 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
}
return newMat(C.Mat_NewFromBytes(C.int(rows), C.int(cols), C.int(mt), *cBytes)), nil
}
// 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
}
// 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
}
// 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 {
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))
}
// 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 {
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))
}
// 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")
}
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")
}
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")
}
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")
}
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))
}
// 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))
}
// ToImage converts a Mat to a image.Image.
func (m *Mat) ToImage() (image.Image, error) {
t := m.Type()
if t != MatTypeCV8UC1 && t != MatTypeCV8UC3 && t != MatTypeCV8UC4 {
return nil, errors.New("ToImage supports only MatType CV8UC1, CV8UC3 and CV8UC4")
}
width := m.Cols()
height := m.Rows()
step := m.Step()
data := m.ToBytes()
channels := m.Channels()
if t == MatTypeCV8UC1 {
img := image.NewGray(image.Rect(0, 0, width, height))
c := color.Gray{Y: uint8(0)}
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
c.Y = uint8(data[y*step+x])
img.SetGray(x, y, c)
}
}
return img, nil
}
img := image.NewRGBA(image.Rect(0, 0, width, height))
c := color.RGBA{
R: uint8(0),
G: uint8(0),
B: uint8(0),
A: uint8(255),
}
for y := 0; y < height; y++ {
for x := 0; x < step; x = x + channels {
c.B = uint8(data[y*step+x])
c.G = uint8(data[y*step+x+1])
c.R = uint8(data[y*step+x+2])
if channels == 4 {
c.A = uint8(data[y*step+x+3])
}
img.SetRGBA(int(x/channels), y, c)
}
}
return img, nil
}
//ImageToMatRGBA converts image.Image to gocv.Mat,
//which represents RGBA image having 8bit for each component.
//Type of Mat is gocv.MatTypeCV8UC4.
func ImageToMatRGBA(img image.Image) (Mat, error) {
bounds := img.Bounds()
x := bounds.Dx()
y := bounds.Dy()
data := make([]byte, 0, x*y*4)
for j := bounds.Min.Y; j < bounds.Max.Y; j++ {
for i := bounds.Min.X; i < bounds.Max.X; i++ {
r, g, b, a := img.At(i, j).RGBA()
data = append(data, byte(b>>8), byte(g>>8), byte(r>>8), byte(a>>8))
}
}
return NewMatFromBytes(y, x, MatTypeCV8UC4, data)
}
//ImageToMatRGB converts image.Image to gocv.Mat,
//which represents RGB image having 8bit for each component.
//Type of Mat is gocv.MatTypeCV8UC3.
func ImageToMatRGB(img image.Image) (Mat, error) {
bounds := img.Bounds()
x := bounds.Dx()
y := bounds.Dy()
data := make([]byte, 0, x*y*3)
for j := bounds.Min.Y; j < bounds.Max.Y; j++ {
for i := bounds.Min.X; i < bounds.Max.X; i++ {
r, g, b, _ := img.At(i, j).RGBA()
data = append(data, byte(b>>8), byte(g>>8), byte(r>>8))
}
}
return NewMatFromBytes(y, x, MatTypeCV8UC3, data)
}
//ImageGrayToMatGray converts image.Gray to gocv.Mat,
//which represents grayscale image 8bit.
//Type of Mat is gocv.MatTypeCV8UC1.
func ImageGrayToMatGray(img *image.Gray) (Mat, error) {
bounds := img.Bounds()
x := bounds.Dx()
y := bounds.Dy()
data := make([]byte, 0, x*y)
for j := bounds.Min.Y; j < bounds.Max.Y; j++ {
for i := bounds.Min.X; i < bounds.Max.X; i++ {
data = append(data, img.GrayAt(i, j).Y)
}
}
return NewMatFromBytes(y, x, MatTypeCV8UC1, data)
}
// 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)
}
// BitwiseXor calculates the per-element bit-wise "exclusive or" operation
// on two arrays or an array and a scalar.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga84b2d8188ce506593dcc3f8cd00e8e2c
//
func BitwiseXor(src1 Mat, src2 Mat, dst *Mat) {
C.Mat_BitwiseXor(src1.p, src2.p, dst.p)
}
// BitwiseXorWithMask calculates the per-element bit-wise "exclusive or" operation
// on 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#ga84b2d8188ce506593dcc3f8cd00e8e2c
//
func BitwiseXorWithMask(src1 Mat, src2 Mat, dst *Mat, mask Mat) {
C.Mat_BitwiseXorWithMask(src1.p, src2.p, dst.p, mask.p)
}
// BatchDistance is a naive nearest neighbor finder.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga4ba778a1c57f83233b1d851c83f5a622
//
func BatchDistance(src1 Mat, src2 Mat, dist Mat, dtype int, nidx Mat, normType int, K int, mask Mat, update int, crosscheck bool) {
C.Mat_BatchDistance(src1.p, src2.p, dist.p, C.int(dtype), nidx.p, C.int(normType), C.int(K), mask.p, C.int(update), C.bool(crosscheck))
}
// BorderInterpolate computes the source location of an extrapolated pixel.
//
// For further details, please see:
// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga247f571aa6244827d3d798f13892da58
//
func BorderInterpolate(p int, len int, borderType CovarFlags) int {
ret := C.Mat_BorderInterpolate(C.int(p), C.int(len), C.int(borderType))
return int(ret)
}
// CovarFlags are the covariation flags used by functions such as BorderInterpolate.
//
// For further details, please see:
// https://docs.opencv.org/master/d0/de1/group__core.html#ga719ebd4a73f30f4fab258ab7616d0f0f
//
type CovarFlags int
const (
// CovarScrambled indicates to scramble the results.
CovarScrambled CovarFlags = 0
// CovarNormal indicates to use normal covariation.
CovarNormal = 1
// CovarUseAvg indicates to use average covariation.
CovarUseAvg = 2
// CovarScale indicates to use scaled covariation.
CovarScale = 4
// CovarRows indicates to use covariation on rows.
CovarRows = 8
// CovarCols indicates to use covariation on columns.
CovarCols = 16