-
Notifications
You must be signed in to change notification settings - Fork 870
/
core_test.go
3414 lines (2895 loc) · 78.9 KB
/
core_test.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
import (
"bytes"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"runtime"
"strings"
"testing"
)
func TestMat(t *testing.T) {
mat := NewMat()
defer mat.Close()
if !mat.Empty() {
t.Error("New Mat should be empty")
}
}
func TestMatClosed(t *testing.T) {
mat := NewMat()
mat.Close()
if !mat.Closed() {
t.Error("Closed Mat should be closed")
}
}
func TestMatWithSizes(t *testing.T) {
t.Run("create mat with multidimensional array", func(t *testing.T) {
sizes := []int{100, 100, 100}
mat := NewMatWithSizes(sizes, MatTypeCV8U)
defer mat.Close()
if mat.Empty() {
t.Error("NewMatWithSizes should not be empty")
}
for i, val := range mat.Size() {
if val != sizes[i] {
t.Errorf("NewMatWithSizes incorrect size: %v\n", mat.Size())
}
}
if mat.Rows() != -1 {
t.Errorf("NewMatWithSizes incorrect row count: %v\n", mat.Rows())
}
if mat.Cols() != -1 {
t.Errorf("NewMatWithSizes incorrect col count: %v\n", mat.Cols())
}
if mat.Channels() != 1 {
t.Errorf("NewMatWithSizes incorrect channels count: %v\n", mat.Channels())
}
if mat.Type() != MatTypeCV8U {
t.Errorf("NewMatWithSizes incorrect type: %v\n", mat.Type())
}
if mat.Total() != 100*100*100 {
t.Errorf("NewMatWithSizes incorrect total: %v\n", mat.Total())
}
})
t.Run("create 2x3x3 multidimensional array with 3 channels and scalar", func(t *testing.T) {
sizes := []int{2, 3, 3}
s := NewScalar(255.0, 105.0, 180.0, 0.0)
mat := NewMatWithSizesWithScalar(sizes, MatTypeCV32FC3, s)
defer mat.Close()
if mat.Empty() {
t.Error("NewMatWithSizesWithScalar should not be empty")
}
for i, val := range mat.Size() {
if val != sizes[i] {
t.Errorf("NewMatWithSizesWithScalar incorrect size: %v\n", mat.Size())
}
}
if mat.Rows() != -1 {
t.Errorf("NewMatWithSizesWithScalar incorrect row count: %v\n", mat.Rows())
}
if mat.Cols() != -1 {
t.Errorf("NewMatWithSizesWithScalar incorrect col count: %v\n", mat.Cols())
}
if mat.Channels() != 3 {
t.Errorf("NewMatWithSizesWithScalar incorrect channels count: %v\n", mat.Channels())
}
if mat.Type() != MatTypeCV32FC3 {
t.Errorf("NewMatWithSizesWithScalar incorrect type: %v\n", mat.Type())
}
if mat.Total() != 2*3*3 {
t.Errorf("NewMatWithSizesWithScalar incorrect total: %v\n", mat.Total())
}
matChans := Split(mat)
scalar := []float32{255.0, 105.0, 180.0}
for c := 0; c < mat.Channels(); c++ {
for x := 0; x < sizes[0]; x++ {
for y := 0; y < sizes[1]; y++ {
for z := 0; z < sizes[2]; z++ {
if s := matChans[c].GetFloatAt3(x, y, z); s != scalar[c] {
t.Errorf("NewMatWithSizesWithScalar incorrect scalar: %v\n", s)
}
}
}
}
}
for _, ch := range matChans {
ch.Close()
}
})
t.Run("create 1x2x3 multidimensional array with 3 channel and data", func(t *testing.T) {
sizes := []int{1, 2, 3}
// generate byte array
s := NewScalar(255.0, 123.0, 55.0, 0.0)
mat1 := NewMatWithSizesWithScalar(sizes, MatTypeCV32FC2, s)
defer mat1.Close()
b := mat1.ToBytes()
mat, err := NewMatWithSizesFromBytes(sizes, MatTypeCV32FC2, b)
defer mat.Close()
if err != nil {
t.Errorf("NewMatWithSizesFromBytes %v\n", err)
}
if mat.Empty() {
t.Error("NewMatWithSizesFromBytes should not be empty")
}
mat2, err := NewMatWithSizesFromBytes(sizes, MatTypeCV32FC2, nil)
defer mat2.Close()
if err == nil {
t.Error("NewMatWithSizesFromBytes should return error with empty bytes")
}
b1 := mat.ToBytes()
if !bytes.Equal(b, b1) {
t.Error("NewMatWithSizesFromBytes byte arrays not equal")
}
for i, val := range mat.Size() {
if val != sizes[i] {
t.Errorf("NewMatWithSizesFromBytes incorrect size: %v\n", mat.Size())
}
}
if mat.Rows() != -1 {
t.Errorf("NewMatWithSizesFromBytes incorrect row count: %v\n", mat.Rows())
}
if mat.Cols() != -1 {
t.Errorf("NewMatWithSizesFromBytes incorrect col count: %v\n", mat.Cols())
}
if mat.Channels() != 2 {
t.Errorf("NewMatWithSizesFromBytes incorrect channels count: %v\n", mat.Channels())
}
if mat.Type() != MatTypeCV32FC2 {
t.Errorf("NewMatWithSizesFromBytes incorrect type: %v\n", mat.Type())
}
if mat.Total() != 1*2*3 {
t.Errorf("NewMatWithSizesFromBytes incorrect total: %v\n", mat.Total())
}
matChans := Split(mat)
scalar := []float32{255.0, 123.0, 55.0}
for c := 0; c < mat.Channels(); c++ {
for x := 0; x < sizes[0]; x++ {
for y := 0; y < sizes[1]; y++ {
for z := 0; z < sizes[2]; z++ {
if s := matChans[c].GetFloatAt3(x, y, z); s != scalar[c] {
t.Errorf("NewMatWithSizesFromBytes incorrect value: %v\n", s)
}
}
}
}
}
for _, ch := range matChans {
ch.Close()
}
})
}
func TestMatFromBytesWithEmptyByteSlice(t *testing.T) {
_, err := NewMatFromBytes(600, 800, MatTypeCV8U, []byte{})
if err == nil {
t.Error("TestMatFromBytesWithEmptyByteSlise: " +
"must fail because of an empty byte slice")
}
if !strings.Contains(err.Error(), ErrEmptyByteSlice.Error()) {
t.Errorf("TestMatFromBytesWithEmptyByteSlice: "+
"error must contain the following description: "+
"%v, but have: %v", ErrEmptyByteSlice, err)
}
}
func TestMatFromBytesSliceGarbageCollected(t *testing.T) {
data := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
m, err := NewMatFromBytes(2, 5, MatTypeCV8U, data)
if err != nil {
t.Error("TestMatFromBytesSliceGarbageCollected: " +
"failed to create Mat")
}
defer m.Close()
// Force garbage collection. As data is not used after this, its backing array should
// be collected.
runtime.GC()
v := m.GetUCharAt(0, 0)
if v != 0 {
t.Errorf("TestMatFromBytesSliceGarbageCollected: "+
"unexpected value. Want %d, got %d.", 0, v)
}
}
func TestMatWithSize(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat.Close()
if mat.Empty() {
t.Error("NewMatWithSize should not be empty")
}
if mat.Rows() != 101 {
t.Errorf("NewMatWithSize incorrect row count: %v\n", mat.Rows())
}
if mat.Cols() != 102 {
t.Errorf("NewMatWithSize incorrect col count: %v\n", mat.Cols())
}
if mat.Channels() != 1 {
t.Errorf("NewMatWithSize incorrect channels count: %v\n", mat.Channels())
}
if mat.Type() != 0 {
t.Errorf("NewMatWithSize incorrect type: %v\n", mat.Type())
}
if mat.Step() != 102 {
t.Errorf("NewMatWithSize incorrect step count: %v\n", mat.Step())
}
}
func TestMatWithSizeFromScalar(t *testing.T) {
s := NewScalar(255.0, 105.0, 180.0, 0.0)
mat := NewMatWithSizeFromScalar(s, 2, 3, MatTypeCV8UC3)
defer mat.Close()
if mat.Empty() {
t.Error("NewMatWithSizeFromScalar should not be empty")
}
if mat.Rows() != 2 {
t.Errorf("NewMatWithSizeFromScalar incorrect row count: %v\n", mat.Rows())
}
if mat.Cols() != 3 {
t.Errorf("NewMatWithSizeFromScalar incorrect col count: %v\n", mat.Cols())
}
if mat.Channels() != 3 {
t.Errorf("NewMatWithSizeFromScalar incorrect channels count: %v\n", mat.Channels())
}
if mat.Type() != 16 {
t.Errorf("NewMatWithSizeFromScalar incorrect type: %v\n", mat.Type())
}
if mat.Total() != 6 {
t.Errorf("incorrect total: %v\n", mat.Total())
}
if mat.Step() != 9 {
t.Errorf("NewMatWithSizeFromScalar incorrect step count: %v\n", mat.Step())
}
sz := mat.Size()
if sz[0] != 2 && sz[1] != 3 {
t.Errorf("NewMatWithSize incorrect size: %v\n", sz)
}
matChans := Split(mat)
scalarByte := []byte{255, 105, 180}
for c := 0; c < mat.Channels(); c++ {
for i := 0; i < mat.Rows(); i++ {
for j := 0; j < mat.Cols(); j++ {
if s := matChans[c].GetUCharAt(i, j); s != scalarByte[c] {
t.Errorf("NewMatWithSizeFromScalar incorrect scalar: %v\n", s)
}
}
}
}
for _, i := range matChans {
i.Close()
}
}
func TestMatFromPtr(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat.Close()
pmat, _ := mat.FromPtr(11, 12, MatTypeCV8U, 10, 10)
defer pmat.Close()
if pmat.Rows() != 11 {
t.Errorf("Mat copy incorrect row count: %v\n", pmat.Rows())
}
if pmat.Cols() != 12 {
t.Errorf("Mat copy incorrect col count: %v\n", pmat.Cols())
}
}
func TestMatClone(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat.Close()
clone := mat.Clone()
defer clone.Close()
if clone.Rows() != 101 {
t.Errorf("Mat clone incorrect row count: %v\n", clone.Rows())
}
if clone.Cols() != 102 {
t.Errorf("Mat clone incorrect col count: %v\n", clone.Cols())
}
}
func TestMatCopyTo(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat.Close()
copy := NewMat()
defer copy.Close()
mat.CopyTo(©)
if copy.Rows() != 101 {
t.Errorf("Mat copy incorrect row count: %v\n", copy.Rows())
}
if copy.Cols() != 102 {
t.Errorf("Mat copy incorrect col count: %v\n", copy.Cols())
}
}
func TestMatCopyToWithMask(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat.Close()
mask := NewMatWithSize(101, 102, MatTypeCV8U)
defer mask.Close()
diff := NewMat()
defer diff.Close()
mat.SetUCharAt(0, 0, 255)
mat.SetUCharAt(0, 1, 255)
mask.SetUCharAt(0, 0, 255)
copy := NewMat()
defer copy.Close()
mat.CopyToWithMask(©, mask)
if copy.Rows() != 101 {
t.Errorf("Mat copy incorrect row count: %v\n", copy.Rows())
}
if copy.Cols() != 102 {
t.Errorf("Mat copy incorrect col count: %v\n", copy.Cols())
}
if copy.GetUCharAt(0, 0) != 255 || copy.GetUCharAt(0, 1) != 0 {
t.Errorf("Mask failed to apply to source image")
}
Compare(mat, copy, &diff, CompareEQ)
if CountNonZero(diff) == 0 {
t.Errorf("Mat CopyToWithMask incorrect diff: %v\n", CountNonZero(diff))
}
}
func TestMatToBytes(t *testing.T) {
mat1 := NewMatWithSize(101, 102, MatTypeCV8U)
defer mat1.Close()
b := mat1.ToBytes()
if len(b) != 101*102 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
copy, err := NewMatFromBytes(101, 102, MatTypeCV8U, b)
if err != nil {
t.Error(err.Error())
}
defer copy.Close()
if copy.Rows() != 101 {
t.Errorf("Mat from bytes incorrect row count: %v\n", copy.Rows())
}
if copy.Cols() != 102 {
t.Errorf("Mat region incorrect col count: %v\n", copy.Cols())
}
mat2 := NewMatWithSize(101, 102, MatTypeCV16S)
defer mat2.Close()
b = mat2.ToBytes()
if len(b) != 101*102*2 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
mat3 := NewMatFromScalar(NewScalar(255.0, 105.0, 180.0, 0.0), MatTypeCV8UC3)
defer mat3.Close()
b = mat3.ToBytes()
if len(b) != 3 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
if bytes.Compare(b, []byte{255, 105, 180}) != 0 {
t.Errorf("Mat bytes unexpected values: %v\n", b)
}
}
func TestMatEye(t *testing.T) {
data := []byte{1, 0, 0, 1}
e := Eye(2, 2, MatTypeCV8U)
if bytes.Compare(e.ToBytes(), data) != 0 {
t.Errorf("Mat bytes are not equal")
}
e.Close()
data2 := []byte{1, 0, 0, 0, 1, 0}
e2 := Eye(2, 3, MatTypeCV8U)
if bytes.Compare(e2.ToBytes(), data2) != 0 {
t.Errorf("Mat bytes are not equal")
}
val := e2.GetUCharAt(0, 2)
if val != 0 {
t.Errorf("Mat bytes unexpected value at [0,2]: %v\n", val)
}
e2.Close()
}
func TestMatZeros(t *testing.T) {
expected := NewMatWithSize(2, 3, MatTypeCV8U)
z := Zeros(2, 3, MatTypeCV8U)
if bytes.Compare(z.ToBytes(), expected.ToBytes()) != 0 {
t.Errorf("Mat bytes are not equal")
}
expected.Close()
z.Close()
expected2 := NewMatWithSize(2, 3, MatTypeCV64F)
z2 := Zeros(2, 3, MatTypeCV64F)
if bytes.Compare(z2.ToBytes(), expected2.ToBytes()) != 0 {
t.Errorf("Mat bytes are not equal")
}
expected2.Close()
z2.Close()
}
func TestMatOnes(t *testing.T) {
expected := NewMatWithSizeFromScalar(Scalar{Val1: 1}, 2, 3, MatTypeCV8U)
o := Ones(2, 3, MatTypeCV8U)
if bytes.Compare(o.ToBytes(), expected.ToBytes()) != 0 {
t.Errorf("Mat bytes are not equal")
}
defer expected.Close()
defer o.Close()
expected2 := NewMatWithSizeFromScalar(Scalar{Val1: 1}, 2, 1, MatTypeCV64F)
o2 := Ones(2, 1, MatTypeCV64F)
if bytes.Compare(o2.ToBytes(), expected2.ToBytes()) != 0 {
t.Errorf("Mat bytes are not equal")
}
expected2.Close()
o2.Close()
}
func TestMatDataPtr(t *testing.T) {
const (
rows = 101
cols = 102
)
t.Run("Uint8", func(t *testing.T) {
testPoints := []struct {
row int
col int
val uint8
}{
{row: 0, col: 0, val: 10},
{row: 30, col: 31, val: 20},
{row: rows - 1, col: cols - 1, val: 30},
}
mat1 := NewMatWithSize(rows, cols, MatTypeCV8U)
defer mat1.Close()
b, err := mat1.DataPtrUint8()
if err != nil {
t.Error(err)
}
if len(b) != 101*102 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetUCharAt(p.row, p.col, p.val)
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %d, but it was %d", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV32F)
defer mat2.Close()
b, err = mat2.DataPtrUint8()
if err != nil {
t.Error(err)
}
if len(b) != 3*9*4 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrUint8()
if err == nil {
t.Errorf("Expected error.")
}
})
t.Run("Int8", func(t *testing.T) {
testPoints := []struct {
row int
col int
val int8
}{
{row: 0, col: 0, val: 10},
{row: 30, col: 31, val: 20},
{row: rows - 1, col: cols - 1, val: 30},
}
mat1 := NewMatWithSize(101, 102, MatTypeCV8S)
defer mat1.Close()
b, err := mat1.DataPtrInt8()
if err != nil {
t.Error(err)
}
if len(b) != rows*cols {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetSCharAt(p.row, p.col, p.val)
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %d, but it was %d", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV32F)
defer mat2.Close()
b, err = mat2.DataPtrInt8()
if err != nil {
t.Error(err)
}
if len(b) != 3*9*4 {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrInt8()
if err == nil {
t.Errorf("Expected error.")
}
})
t.Run("Uint16", func(t *testing.T) {
testPoints := []struct {
row int
col int
val uint16
}{
{row: 0, col: 0, val: 10},
{row: 30, col: 31, val: 20},
{row: rows - 1, col: cols - 1, val: 30},
}
mat1 := NewMatWithSize(rows, cols, MatTypeCV16U)
defer mat1.Close()
b, err := mat1.DataPtrUint16()
if err != nil {
t.Error(err)
}
if len(b) != rows*cols {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetShortAt(p.row, p.col, int16(p.val))
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %d, but it was %d", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV32F)
defer mat2.Close()
_, err = mat2.DataPtrUint16()
if err == nil {
t.Errorf("Expected error.")
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrUint16()
if err == nil {
t.Errorf("Expected error.")
}
})
t.Run("Int16", func(t *testing.T) {
testPoints := []struct {
row int
col int
val int16
}{
{row: 0, col: 0, val: 10},
{row: 30, col: 31, val: 20},
{row: rows - 1, col: cols - 1, val: 30},
}
mat1 := NewMatWithSize(rows, cols, MatTypeCV16S)
defer mat1.Close()
b, err := mat1.DataPtrInt16()
if err != nil {
t.Error(err)
}
if len(b) != rows*cols {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetShortAt(p.row, p.col, p.val)
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %d, but it was %d", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV32F)
defer mat2.Close()
_, err = mat2.DataPtrInt16()
if err == nil {
t.Errorf("Expected error.")
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrInt16()
if err == nil {
t.Errorf("Expected error.")
}
})
t.Run("Float32", func(t *testing.T) {
testPoints := []struct {
row int
col int
val float32
}{
{row: 0, col: 0, val: 10.5},
{row: 30, col: 31, val: 20.5},
{row: rows - 1, col: cols - 1, val: 30.5},
}
mat1 := NewMatWithSize(rows, cols, MatTypeCV32F)
defer mat1.Close()
b, err := mat1.DataPtrFloat32()
if err != nil {
t.Error(err)
}
if len(b) != rows*cols {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetFloatAt(p.row, p.col, p.val)
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %f, but it was %f", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV16S)
defer mat2.Close()
_, err = mat2.DataPtrFloat32()
if err == nil {
t.Errorf("Expected error.")
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrFloat32()
if err == nil {
t.Errorf("Expected error.")
}
})
t.Run("Float64", func(t *testing.T) {
testPoints := []struct {
row int
col int
val float64
}{
{row: 0, col: 0, val: 10.5},
{row: 30, col: 31, val: 20.5},
{row: rows - 1, col: cols - 1, val: 30.5},
}
mat1 := NewMatWithSize(rows, cols, MatTypeCV64F)
defer mat1.Close()
b, err := mat1.DataPtrFloat64()
if err != nil {
t.Error(err)
}
if len(b) != rows*cols {
t.Errorf("Mat bytes incorrect length: %v\n", len(b))
}
for _, p := range testPoints {
mat1.SetDoubleAt(p.row, p.col, p.val)
if got := b[p.row*cols+p.col]; got != p.val {
t.Errorf("Expected %d,%d = %f, but it was %f", p.row, p.col, p.val, got)
}
}
mat2 := NewMatWithSize(3, 9, MatTypeCV16S)
defer mat2.Close()
_, err = mat2.DataPtrFloat64()
if err == nil {
t.Errorf("Expected error.")
}
mat3 := mat1.Region(image.Rect(25, 25, 75, 75))
defer mat3.Close()
_, err = mat3.DataPtrFloat64()
if err == nil {
t.Errorf("Expected error.")
}
})
}
func TestMatRegion(t *testing.T) {
mat := NewMatWithSize(100, 100, MatTypeCV8U)
defer mat.Close()
region := mat.Region(image.Rect(20, 25, 80, 75))
defer region.Close()
if region.Rows() != 50 {
t.Errorf("Mat region incorrect row count: %v\n", region.Rows())
}
if region.Cols() != 60 {
t.Errorf("Mat region incorrect col count: %v\n", region.Cols())
}
}
func TestMatReshape(t *testing.T) {
mat := NewMatWithSize(100, 100, MatTypeCV8UC4)
defer mat.Close()
r := mat.Reshape(1, 1)
defer r.Close()
if r.Rows() != 1 {
t.Errorf("Mat reshape incorrect row count: %v\n", r.Rows())
}
if r.Cols() != 40000 {
t.Errorf("Mat reshape incorrect col count: %v\n", r.Cols())
}
}
func TestMatPatchNaNs(t *testing.T) {
mat := NewMatWithSize(100, 100, MatTypeCV32F)
defer mat.Close()
mat.PatchNaNs()
if mat.Empty() {
t.Error("TestMatPatchNaNs error.")
}
}
func TestMatConvert(t *testing.T) {
src := NewMatWithSize(100, 100, MatTypeCV32F)
defer src.Close()
dst := NewMat()
defer dst.Close()
src.ConvertTo(&dst, MatTypeCV16S)
if dst.Empty() {
t.Error("TestConvert dst should not be empty.")
}
}
func TestMatConvertWithParams(t *testing.T) {
src := NewMatWithSize(100, 100, MatTypeCV8U)
defer src.Close()
dst := NewMat()
defer dst.Close()
src.ConvertToWithParams(&dst, MatTypeCV32F, 1.0/255.0, 0.0)
if dst.Empty() {
t.Error("TestConvertWithParams dst should not be empty.")
}
}
func TestMatConvertFp16(t *testing.T) {
src := NewMatWithSize(100, 100, MatTypeCV32F)
defer src.Close()
dst := src.ConvertFp16()
defer dst.Close()
if dst.Empty() {
t.Error("TestConvertFp16 dst should not be empty.")
}
}
func TestMatSqrt(t *testing.T) {
src := NewMatWithSize(100, 100, MatTypeCV32F)
defer src.Close()
dst := src.Sqrt()
defer dst.Close()
if dst.Empty() {
t.Error("TestSqrt dst should not be empty.")
}
}
func TestMatMean(t *testing.T) {
mat := NewMatWithSize(100, 100, MatTypeCV8U)
defer mat.Close()
mean := mat.Mean()
if mean.Val1 != 0 {
t.Errorf("Mat Mean incorrect Val1")
}
}
func TestMatMeanWithMask(t *testing.T) {
mat := NewMatWithSize(100, 100, MatTypeCV8U)
defer mat.Close()
mask := NewMatWithSize(100, 100, MatTypeCV8U)
defer mask.Close()
mean := mat.MeanWithMask(mask)
if mean.Val1 != 0 {
t.Errorf("Mat Mean incorrect Val1")
}
}
func TestLUT(t *testing.T) {
src := IMRead("images/gocvlogo.jpg", IMReadColor)
if src.Empty() {
t.Error("Invalid read of Source Mat in LUT test")
}
defer src.Close()
lut := IMRead("images/lut.png", IMReadColor)
if lut.Empty() {
t.Error("Invalid read of LUT Mat in LUT test")
}
defer lut.Close()
dst := NewMat()
defer dst.Close()
LUT(src, lut, &dst)
if dst.Cols() != 400 || dst.Rows() != 343 {
t.Errorf("Expected dst size of 200x172 got %dx%d", dst.Cols(), dst.Rows())
}
}
func TestMatAccessors(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
if mat.GetUCharAt(50, 50) != 0 {
t.Errorf("GetUCharAt incorrect value: %v\n", mat.GetUCharAt(50, 50))
}
if mat.GetUCharAt3(50, 50, 0) != 0 {
t.Errorf("GetUCharAt3 incorrect value: %v\n", mat.GetUCharAt3(50, 50, 0))
}
mat.Close()
mat = NewMatWithSize(101, 102, MatTypeCV8S)
if mat.GetSCharAt(50, 50) != 0 {
t.Errorf("GetSCharAt incorrect value: %v\n", mat.GetSCharAt(50, 50))
}
if mat.GetSCharAt3(50, 50, 0) != 0 {
t.Errorf("GetSCharAt3 incorrect value: %v\n", mat.GetSCharAt3(50, 50, 0))
}
mat.Close()
mat = NewMatWithSize(101, 102, MatTypeCV16S)
if mat.GetShortAt(50, 50) != 0 {
t.Errorf("GetShortAt incorrect value: %v\n", mat.GetShortAt(50, 50))
}
if mat.GetShortAt3(50, 50, 0) != 0 {
t.Errorf("GetShortAt3 incorrect value: %v\n", mat.GetShortAt3(50, 50, 0))
}
mat.Close()
mat = NewMatWithSize(101, 102, MatTypeCV32S)
if mat.GetIntAt(50, 50) != 0 {
t.Errorf("GetIntAt incorrect value: %v\n", mat.GetIntAt(50, 50))
}
if mat.GetIntAt3(50, 50, 0) != 0 {
t.Errorf("GetIntAt3 incorrect value: %v\n", mat.GetIntAt3(50, 50, 0))
}
mat.Close()
mat = NewMatWithSize(101, 102, MatTypeCV32F)
if mat.GetFloatAt(50, 50) != 0.0 {
t.Errorf("GetFloatAt incorrect value: %v\n", mat.GetFloatAt(50, 50))
}
if mat.GetFloatAt3(50, 50, 0) != 0.0 {
t.Errorf("GetFloatAt3 incorrect value: %v\n", mat.GetFloatAt3(50, 50, 0))
}
mat.Close()
mat = NewMatWithSize(101, 102, MatTypeCV64F)
if mat.GetDoubleAt(50, 50) != 0.0 {
t.Errorf("GetDoubleAt incorrect value: %v\n", mat.GetDoubleAt(50, 50))
}
if mat.GetDoubleAt3(50, 50, 0) != 0.0 {
t.Errorf("GetDoubleAt3 incorrect value: %v\n", mat.GetDoubleAt3(50, 50, 0))
}
mat.Close()
}
func TestMatMutators(t *testing.T) {
t.Run("SetTo", func(t *testing.T) {
mat := NewMatWithSizeFromScalar(NewScalar(0, 0, 0, 0), 1, 1, MatTypeCV8U)
mat.SetTo(NewScalar(255, 255, 255, 255))
for z := 0; z < mat.Channels(); z++ {
if mat.GetUCharAt3(0, 0, z) != 255 {
t.Errorf("SetTo incorrect value: z=%v: %v\n", z, mat.GetUCharAt3(0, 0, z))
}
}
mat.Close()
})
t.Run("SetUCharAt", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
mat.SetUCharAt(50, 50, 25)
if mat.GetUCharAt(50, 50) != 25 {
t.Errorf("SetUCharAt incorrect value: %v\n", mat.GetUCharAt(50, 50))
}
mat.Close()
})
t.Run("SetUCharAt3", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8U)
mat.SetUCharAt3(50, 50, 0, 25)
if mat.GetUCharAt3(50, 50, 0) != 25 {
t.Errorf("SetUCharAt3 incorrect value: %v\n", mat.GetUCharAt3(50, 50, 0))
}
mat.Close()
})
t.Run("SetSCharAt", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8S)
mat.SetSCharAt(50, 50, 25)
if mat.GetSCharAt(50, 50) != 25 {
t.Errorf("SetSCharAt incorrect value: %v\n", mat.GetSCharAt(50, 50))
}
mat.Close()
})
t.Run("SetSCharAt3", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV8S)
mat.SetSCharAt3(50, 50, 0, 25)
if mat.GetSCharAt3(50, 50, 0) != 25 {
t.Errorf("SetSCharAt3 incorrect value: %v\n", mat.GetSCharAt3(50, 50, 0))
}
mat.Close()
})
t.Run("SetShortAt", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV16S)
mat.SetShortAt(50, 50, 25)
if mat.GetShortAt(50, 50) != 25 {
t.Errorf("SetShortAt incorrect value: %v\n", mat.GetShortAt(50, 50))
}
mat.Close()
})
t.Run("SetShortAt3", func(t *testing.T) {
mat := NewMatWithSize(101, 102, MatTypeCV16S)
mat.SetShortAt3(50, 50, 0, 25)
if mat.GetShortAt3(50, 50, 0) != 25 {
t.Errorf("SetShortAt3 incorrect value: %v\n", mat.GetShortAt3(50, 50, 0))