forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgproc.go
2215 lines (1908 loc) · 73 KB
/
imgproc.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 "imgproc.h"
*/
import "C"
import (
"errors"
"image"
"image/color"
"reflect"
"unsafe"
)
// ArcLength calculates a contour perimeter or a curve length.
//
// For further details, please see:
//
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga8d26483c636be6b35c3ec6335798a47c
//
func ArcLength(curve PointVector, isClosed bool) float64 {
return float64(C.ArcLength(curve.p, C.bool(isClosed)))
}
// ApproxPolyDP approximates a polygonal curve(s) with the specified precision.
//
// For further details, please see:
//
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga0012a5fdaea70b8a9970165d98722b4c
//
func ApproxPolyDP(curve PointVector, epsilon float64, closed bool) PointVector {
return PointVector{p: C.ApproxPolyDP(curve.p, C.double(epsilon), C.bool(closed))}
}
// ConvexHull finds the convex hull of a point set.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga014b28e56cb8854c0de4a211cb2be656
//
func ConvexHull(points PointVector, hull *Mat, clockwise bool, returnPoints bool) {
C.ConvexHull(points.p, hull.p, C.bool(clockwise), C.bool(returnPoints))
}
// ConvexityDefects finds the convexity defects of a contour.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gada4437098113fd8683c932e0567f47ba
//
func ConvexityDefects(contour PointVector, hull Mat, result *Mat) {
C.ConvexityDefects(contour.p, hull.p, result.p)
}
// CvtColor converts an image from one color space to another.
// It converts the src Mat image to the dst Mat using the
// code param containing the desired ColorConversionCode color space.
//
// For further details, please see:
// http://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga4e0972be5de079fed4e3a10e24ef5ef0
//
func CvtColor(src Mat, dst *Mat, code ColorConversionCode) {
C.CvtColor(src.p, dst.p, C.int(code))
}
// EqualizeHist normalizes the brightness and increases the contrast of the image.
//
// For further details, please see:
// https://docs.opencv.org/master/d6/dc7/group__imgproc__hist.html#ga7e54091f0c937d49bf84152a16f76d6e
func EqualizeHist(src Mat, dst *Mat) {
C.EqualizeHist(src.p, dst.p)
}
// CalcHist Calculates a histogram of a set of images
//
// For futher details, please see:
// https://docs.opencv.org/master/d6/dc7/group__imgproc__hist.html#ga6ca1876785483836f72a77ced8ea759a
func CalcHist(src []Mat, channels []int, mask Mat, hist *Mat, size []int, ranges []float64, acc bool) {
cMatArray := make([]C.Mat, len(src))
for i, r := range src {
cMatArray[i] = r.p
}
cMats := C.struct_Mats{
mats: (*C.Mat)(&cMatArray[0]),
length: C.int(len(src)),
}
chansInts := []C.int{}
for _, v := range channels {
chansInts = append(chansInts, C.int(v))
}
chansVector := C.struct_IntVector{}
chansVector.val = (*C.int)(&chansInts[0])
chansVector.length = (C.int)(len(chansInts))
sizeInts := []C.int{}
for _, v := range size {
sizeInts = append(sizeInts, C.int(v))
}
sizeVector := C.struct_IntVector{}
sizeVector.val = (*C.int)(&sizeInts[0])
sizeVector.length = (C.int)(len(sizeInts))
rangeFloats := []C.float{}
for _, v := range ranges {
rangeFloats = append(rangeFloats, C.float(v))
}
rangeVector := C.struct_FloatVector{}
rangeVector.val = (*C.float)(&rangeFloats[0])
rangeVector.length = (C.int)(len(rangeFloats))
C.CalcHist(cMats, chansVector, mask.p, hist.p, sizeVector, rangeVector, C.bool(acc))
}
// CalcBackProject calculates the back projection of a histogram.
//
// For futher details, please see:
// https://docs.opencv.org/3.4/d6/dc7/group__imgproc__hist.html#ga3a0af640716b456c3d14af8aee12e3ca
func CalcBackProject(src []Mat, channels []int, hist Mat, backProject *Mat, ranges []float64, uniform bool) {
cMatArray := make([]C.Mat, len(src))
for i, r := range src {
cMatArray[i] = r.p
}
cMats := C.struct_Mats{
mats: (*C.Mat)(&cMatArray[0]),
length: C.int(len(src)),
}
chansInts := []C.int{}
for _, v := range channels {
chansInts = append(chansInts, C.int(v))
}
chansVector := C.struct_IntVector{}
chansVector.val = (*C.int)(&chansInts[0])
chansVector.length = (C.int)(len(chansInts))
rangeFloats := []C.float{}
for _, v := range ranges {
rangeFloats = append(rangeFloats, C.float(v))
}
rangeVector := C.struct_FloatVector{}
rangeVector.val = (*C.float)(&rangeFloats[0])
rangeVector.length = (C.int)(len(rangeFloats))
C.CalcBackProject(cMats, chansVector, hist.p, backProject.p, rangeVector, C.bool(uniform))
}
// HistCompMethod is the method for Histogram comparison
// For more information, see https://docs.opencv.org/master/d6/dc7/group__imgproc__hist.html#ga994f53817d621e2e4228fc646342d386
type HistCompMethod int
const (
// HistCmpCorrel calculates the Correlation
HistCmpCorrel HistCompMethod = 0
// HistCmpChiSqr calculates the Chi-Square
HistCmpChiSqr HistCompMethod = 1
// HistCmpIntersect calculates the Intersection
HistCmpIntersect HistCompMethod = 2
// HistCmpBhattacharya applies the HistCmpBhattacharya by calculating the Bhattacharya distance.
HistCmpBhattacharya HistCompMethod = 3
// HistCmpHellinger applies the HistCmpBhattacharya comparison. It is a synonym to HistCmpBhattacharya.
HistCmpHellinger = HistCmpBhattacharya
// HistCmpChiSqrAlt applies the Alternative Chi-Square (regularly used for texture comparsion).
HistCmpChiSqrAlt HistCompMethod = 4
// HistCmpKlDiv applies the Kullback-Liebler divergence comparison.
HistCmpKlDiv HistCompMethod = 5
)
// CompareHist Compares two histograms.
//
// For further details, please see:
// https://docs.opencv.org/master/d6/dc7/group__imgproc__hist.html#gaf4190090efa5c47cb367cf97a9a519bd
func CompareHist(hist1 Mat, hist2 Mat, method HistCompMethod) float32 {
return float32(C.CompareHist(hist1.p, hist2.p, C.int(method)))
}
// ClipLine clips the line against the image rectangle.
// For further details, please see:
// https://docs.opencv.org/master/d6/d6e/group__imgproc__draw.html#gaf483cb46ad6b049bc35ec67052ef1c2c
//
func ClipLine(imgSize image.Point, pt1 image.Point, pt2 image.Point) bool {
pSize := C.struct_Size{
width: C.int(imgSize.X),
height: C.int(imgSize.Y),
}
rPt1 := C.struct_Point{
x: C.int(pt1.X),
y: C.int(pt1.Y),
}
rPt2 := C.struct_Point{
x: C.int(pt2.X),
y: C.int(pt2.Y),
}
return bool(C.ClipLine(pSize, rPt1, rPt2))
}
// BilateralFilter applies a bilateral filter to an image.
//
// Bilateral filtering is described here:
// http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html
//
// BilateralFilter can reduce unwanted noise very well while keeping edges
// fairly sharp. However, it is very slow compared to most filters.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed
//
func BilateralFilter(src Mat, dst *Mat, diameter int, sigmaColor float64, sigmaSpace float64) {
C.BilateralFilter(src.p, dst.p, C.int(diameter), C.double(sigmaColor), C.double(sigmaSpace))
}
// Blur blurs an image Mat using a normalized box filter.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37
//
func Blur(src Mat, dst *Mat, ksize image.Point) {
pSize := C.struct_Size{
width: C.int(ksize.X),
height: C.int(ksize.Y),
}
C.Blur(src.p, dst.p, pSize)
}
// BoxFilter blurs an image using the box filter.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gad533230ebf2d42509547d514f7d3fbc3
//
func BoxFilter(src Mat, dst *Mat, depth int, ksize image.Point) {
pSize := C.struct_Size{
height: C.int(ksize.X),
width: C.int(ksize.Y),
}
C.BoxFilter(src.p, dst.p, C.int(depth), pSize)
}
// SqBoxFilter calculates the normalized sum of squares of the pixel values overlapping the filter.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga045028184a9ef65d7d2579e5c4bff6c0
//
func SqBoxFilter(src Mat, dst *Mat, depth int, ksize image.Point) {
pSize := C.struct_Size{
height: C.int(ksize.X),
width: C.int(ksize.Y),
}
C.SqBoxFilter(src.p, dst.p, C.int(depth), pSize)
}
// Dilate dilates an image by using a specific structuring element.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c
//
func Dilate(src Mat, dst *Mat, kernel Mat) {
C.Dilate(src.p, dst.p, kernel.p)
}
// DilateWithParams dilates an image by using a specific structuring element.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c
func DilateWithParams(src Mat, dst *Mat, kernel Mat, anchor image.Point, iterations, borderType BorderType, borderValue color.RGBA) {
cAnchor := C.struct_Point{
x: C.int(anchor.X),
y: C.int(anchor.Y),
}
bv := C.struct_Scalar{
val1: C.double(borderValue.B),
val2: C.double(borderValue.G),
val3: C.double(borderValue.R),
val4: C.double(borderValue.A),
}
C.DilateWithParams(src.p, dst.p, kernel.p, cAnchor, C.int(iterations), C.int(borderType), bv)
}
// DistanceTransformLabelTypes are the types of the DistanceTransform algorithm flag
type DistanceTransformLabelTypes int
const (
// DistanceLabelCComp assigns the same label to each connected component of zeros in the source image
// (as well as all the non-zero pixels closest to the connected component).
DistanceLabelCComp DistanceTransformLabelTypes = 0
// DistanceLabelPixel assigns its own label to each zero pixel (and all the non-zero pixels closest to it).
DistanceLabelPixel
)
// DistanceTransformMasks are the marsk sizes for distance transform
type DistanceTransformMasks int
const (
// DistanceMask3 is a mask of size 3
DistanceMask3 DistanceTransformMasks = 0
// DistanceMask5 is a mask of size 3
DistanceMask5
// DistanceMaskPrecise is not currently supported
DistanceMaskPrecise
)
// DistanceTransform Calculates the distance to the closest zero pixel for each pixel of the source image.
//
// For further details, please see:
// https://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga8a0b7fdfcb7a13dde018988ba3a43042
//
func DistanceTransform(src Mat, dst *Mat, labels *Mat, distType DistanceTypes, maskSize DistanceTransformMasks, labelType DistanceTransformLabelTypes) {
C.DistanceTransform(src.p, dst.p, labels.p, C.int(distType), C.int(maskSize), C.int(labelType))
}
// Erode erodes an image by using a specific structuring element.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaeb1e0c1033e3f6b891a25d0511362aeb
//
func Erode(src Mat, dst *Mat, kernel Mat) {
C.Erode(src.p, dst.p, kernel.p)
}
// ErodeWithParams erodes an image by using a specific structuring element.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaeb1e0c1033e3f6b891a25d0511362aeb
//
func ErodeWithParams(src Mat, dst *Mat, kernel Mat, anchor image.Point, iterations, borderType int) {
cAnchor := C.struct_Point{
x: C.int(anchor.X),
y: C.int(anchor.Y),
}
C.ErodeWithParams(src.p, dst.p, kernel.p, cAnchor, C.int(iterations), C.int(borderType))
}
// RetrievalMode is the mode of the contour retrieval algorithm.
type RetrievalMode int
const (
// RetrievalExternal retrieves only the extreme outer contours.
// It sets `hierarchy[i][2]=hierarchy[i][3]=-1` for all the contours.
RetrievalExternal RetrievalMode = 0
// RetrievalList retrieves all of the contours without establishing
// any hierarchical relationships.
RetrievalList RetrievalMode = 1
// RetrievalCComp retrieves all of the contours and organizes them into
// a two-level hierarchy. At the top level, there are external boundaries
// of the components. At the second level, there are boundaries of the holes.
// If there is another contour inside a hole of a connected component, it
// is still put at the top level.
RetrievalCComp RetrievalMode = 2
// RetrievalTree retrieves all of the contours and reconstructs a full
// hierarchy of nested contours.
RetrievalTree RetrievalMode = 3
// RetrievalFloodfill lacks a description in the original header.
RetrievalFloodfill RetrievalMode = 4
)
// ContourApproximationMode is the mode of the contour approximation algorithm.
type ContourApproximationMode int
const (
// ChainApproxNone stores absolutely all the contour points. That is,
// any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be
// either horizontal, vertical or diagonal neighbors, that is,
// max(abs(x1-x2),abs(y2-y1))==1.
ChainApproxNone ContourApproximationMode = 1
// ChainApproxSimple compresses horizontal, vertical, and diagonal segments
// and leaves only their end points.
// For example, an up-right rectangular contour is encoded with 4 points.
ChainApproxSimple ContourApproximationMode = 2
// ChainApproxTC89L1 applies one of the flavors of the Teh-Chin chain
// approximation algorithms.
ChainApproxTC89L1 ContourApproximationMode = 3
// ChainApproxTC89KCOS applies one of the flavors of the Teh-Chin chain
// approximation algorithms.
ChainApproxTC89KCOS ContourApproximationMode = 4
)
// BoundingRect calculates the up-right bounding rectangle of a point set.
//
// For further details, please see:
// https://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#gacb413ddce8e48ff3ca61ed7cf626a366
//
func BoundingRect(contour PointVector) image.Rectangle {
r := C.BoundingRect(contour.p)
rect := image.Rect(int(r.x), int(r.y), int(r.x+r.width), int(r.y+r.height))
return rect
}
// BoxPoints finds the four vertices of a rotated rect. Useful to draw the rotated rectangle.
//
// For further Details, please see:
// https://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#gaf78d467e024b4d7936cf9397185d2f5c
//
func BoxPoints(rect RotatedRect, pts *Mat) {
rPoints := toCPoints(rect.Points)
rRect := C.struct_Rect{
x: C.int(rect.BoundingRect.Min.X),
y: C.int(rect.BoundingRect.Min.Y),
width: C.int(rect.BoundingRect.Max.X - rect.BoundingRect.Min.X),
height: C.int(rect.BoundingRect.Max.Y - rect.BoundingRect.Min.Y),
}
rCenter := C.struct_Point{
x: C.int(rect.Center.X),
y: C.int(rect.Center.Y),
}
rSize := C.struct_Size{
width: C.int(rect.Width),
height: C.int(rect.Height),
}
r := C.struct_RotatedRect{
pts: rPoints,
boundingRect: rRect,
center: rCenter,
size: rSize,
angle: C.double(rect.Angle),
}
C.BoxPoints(r, pts.p)
}
// ContourArea calculates a contour area.
//
// For further details, please see:
// https://docs.opencv.org/3.3.0/d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1
//
func ContourArea(contour PointVector) float64 {
result := C.ContourArea(contour.p)
return float64(result)
}
type RotatedRect struct {
Points []image.Point
BoundingRect image.Rectangle
Center image.Point
Width int
Height int
Angle float64
}
// toPoints converts C.Contour to []image.Points
//
func toPoints(points C.Contour) []image.Point {
pArray := points.points
pLength := int(points.length)
pHdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(pArray)),
Len: pLength,
Cap: pLength,
}
sPoints := *(*[]C.Point)(unsafe.Pointer(&pHdr))
points4 := make([]image.Point, pLength)
for j, pt := range sPoints {
points4[j] = image.Pt(int(pt.x), int(pt.y))
}
return points4
}
// MinAreaRect finds a rotated rectangle of the minimum area enclosing the input 2D point set.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9
//
func MinAreaRect(points PointVector) RotatedRect {
result := C.MinAreaRect(points.p)
defer C.Points_Close(result.pts)
return RotatedRect{
Points: toPoints(result.pts),
BoundingRect: image.Rect(int(result.boundingRect.x), int(result.boundingRect.y), int(result.boundingRect.x)+int(result.boundingRect.width), int(result.boundingRect.y)+int(result.boundingRect.height)),
Center: image.Pt(int(result.center.x), int(result.center.y)),
Width: int(result.size.width),
Height: int(result.size.height),
Angle: float64(result.angle),
}
}
// FitEllipse Fits an ellipse around a set of 2D points.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gaf259efaad93098103d6c27b9e4900ffa
//
func FitEllipse(pts PointVector) RotatedRect {
cRect := C.FitEllipse(pts.p)
defer C.Points_Close(cRect.pts)
return RotatedRect{
Points: toPoints(cRect.pts),
BoundingRect: image.Rect(int(cRect.boundingRect.x), int(cRect.boundingRect.y), int(cRect.boundingRect.x)+int(cRect.boundingRect.width), int(cRect.boundingRect.y)+int(cRect.boundingRect.height)),
Center: image.Pt(int(cRect.center.x), int(cRect.center.y)),
Width: int(cRect.size.width),
Height: int(cRect.size.height),
Angle: float64(cRect.angle),
}
}
// MinEnclosingCircle finds a circle of the minimum area enclosing the input 2D point set.
//
// For further details, please see:
// https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#ga8ce13c24081bbc7151e9326f412190f1
func MinEnclosingCircle(pts PointVector) (x, y, radius float32) {
cCenterPoint := C.struct_Point2f{}
var cRadius C.float
C.MinEnclosingCircle(pts.p, &cCenterPoint, &cRadius)
x, y = float32(cCenterPoint.x), float32(cCenterPoint.y)
radius = float32(cRadius)
return x, y, radius
}
// FindContours finds contours in a binary image.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga95f5b48d01abc7c2e0732db24689837b
//
func FindContours(src Mat, mode RetrievalMode, method ContourApproximationMode) PointsVector {
hierarchy := NewMat()
defer hierarchy.Close()
return FindContoursWithParams(src, &hierarchy, mode, method)
}
// FindContoursWithParams finds contours in a binary image.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
//
func FindContoursWithParams(src Mat, hierarchy *Mat, mode RetrievalMode, method ContourApproximationMode) PointsVector {
return PointsVector{p: C.FindContours(src.p, hierarchy.p, C.int(mode), C.int(method))}
}
// PointPolygonTest performs a point-in-contour test.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga1a539e8db2135af2566103705d7a5722
//
func PointPolygonTest(pts PointVector, pt image.Point, measureDist bool) float64 {
cp := C.struct_Point{
x: C.int(pt.X),
y: C.int(pt.Y),
}
return float64(C.PointPolygonTest(pts.p, cp, C.bool(measureDist)))
}
//ConnectedComponentsAlgorithmType specifies the type for ConnectedComponents
type ConnectedComponentsAlgorithmType int
const (
// SAUF algorithm for 8-way connectivity, SAUF algorithm for 4-way connectivity.
CCL_WU ConnectedComponentsAlgorithmType = 0
// BBDT algorithm for 8-way connectivity, SAUF algorithm for 4-way connectivity.
CCL_DEFAULT ConnectedComponentsAlgorithmType = 1
// BBDT algorithm for 8-way connectivity, SAUF algorithm for 4-way connectivity
CCL_GRANA ConnectedComponentsAlgorithmType = 2
)
// ConnectedComponents computes the connected components labeled image of boolean image.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gaedef8c7340499ca391d459122e51bef5
//
func ConnectedComponents(src Mat, labels *Mat) int {
return int(C.ConnectedComponents(src.p, labels.p, C.int(8), C.int(MatTypeCV32S), C.int(CCL_DEFAULT)))
}
// ConnectedComponents computes the connected components labeled image of boolean image.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#gaedef8c7340499ca391d459122e51bef5
//
func ConnectedComponentsWithParams(src Mat, labels *Mat, conn int, ltype MatType,
ccltype ConnectedComponentsAlgorithmType) int {
return int(C.ConnectedComponents(src.p, labels.p, C.int(conn), C.int(ltype), C.int(ccltype)))
}
// ConnectedComponentsTypes are the connected components algorithm output formats
type ConnectedComponentsTypes int
const (
//The leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction.
CC_STAT_LEFT ConnectedComponentsTypes = 0
//The topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction.
CC_STAT_TOP ConnectedComponentsTypes = 1
// The horizontal size of the bounding box.
CC_STAT_WIDTH ConnectedComponentsTypes = 2
// The vertical size of the bounding box.
CC_STAT_HEIGHT ConnectedComponentsTypes = 3
// The total area (in pixels) of the connected component.
CC_STAT_AREA ConnectedComponentsTypes = 4
CC_STAT_MAX ConnectedComponentsTypes = 5
)
// ConnectedComponentsWithStats computes the connected components labeled image of boolean
// image and also produces a statistics output for each label.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga107a78bf7cd25dec05fb4dfc5c9e765f
//
func ConnectedComponentsWithStats(src Mat, labels *Mat, stats *Mat, centroids *Mat) int {
return int(C.ConnectedComponentsWithStats(src.p, labels.p, stats.p, centroids.p,
C.int(8), C.int(MatTypeCV32S), C.int(CCL_DEFAULT)))
}
// ConnectedComponentsWithStats computes the connected components labeled image of boolean
// image and also produces a statistics output for each label.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga107a78bf7cd25dec05fb4dfc5c9e765f
//
func ConnectedComponentsWithStatsWithParams(src Mat, labels *Mat, stats *Mat, centroids *Mat,
conn int, ltype MatType, ccltype ConnectedComponentsAlgorithmType) int {
return int(C.ConnectedComponentsWithStats(src.p, labels.p, stats.p, centroids.p, C.int(conn),
C.int(ltype), C.int(ccltype)))
}
// TemplateMatchMode is the type of the template matching operation.
type TemplateMatchMode int
const (
// TmSqdiff maps to TM_SQDIFF
TmSqdiff TemplateMatchMode = 0
// TmSqdiffNormed maps to TM_SQDIFF_NORMED
TmSqdiffNormed TemplateMatchMode = 1
// TmCcorr maps to TM_CCORR
TmCcorr TemplateMatchMode = 2
// TmCcorrNormed maps to TM_CCORR_NORMED
TmCcorrNormed TemplateMatchMode = 3
// TmCcoeff maps to TM_CCOEFF
TmCcoeff TemplateMatchMode = 4
// TmCcoeffNormed maps to TM_CCOEFF_NORMED
TmCcoeffNormed TemplateMatchMode = 5
)
// MatchTemplate compares a template against overlapped image regions.
//
// For further details, please see:
// https://docs.opencv.org/master/df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be
//
func MatchTemplate(image Mat, templ Mat, result *Mat, method TemplateMatchMode, mask Mat) {
C.MatchTemplate(image.p, templ.p, result.p, C.int(method), mask.p)
}
// Moments calculates all of the moments up to the third order of a polygon
// or rasterized shape.
//
// For further details, please see:
// https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga556a180f43cab22649c23ada36a8a139
//
func Moments(src Mat, binaryImage bool) map[string]float64 {
r := C.Moments(src.p, C.bool(binaryImage))
result := make(map[string]float64)
result["m00"] = float64(r.m00)
result["m10"] = float64(r.m10)
result["m01"] = float64(r.m01)
result["m20"] = float64(r.m20)
result["m11"] = float64(r.m11)
result["m02"] = float64(r.m02)
result["m30"] = float64(r.m30)
result["m21"] = float64(r.m21)
result["m12"] = float64(r.m12)
result["m03"] = float64(r.m03)
result["mu20"] = float64(r.mu20)
result["mu11"] = float64(r.mu11)
result["mu02"] = float64(r.mu02)
result["mu30"] = float64(r.mu30)
result["mu21"] = float64(r.mu21)
result["mu12"] = float64(r.mu12)
result["mu03"] = float64(r.mu03)
result["nu20"] = float64(r.nu20)
result["nu11"] = float64(r.nu11)
result["nu02"] = float64(r.nu02)
result["nu30"] = float64(r.nu30)
result["nu21"] = float64(r.nu21)
result["nu12"] = float64(r.nu12)
result["nu03"] = float64(r.nu03)
return result
}
// PyrDown blurs an image and downsamples it.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaf9bba239dfca11654cb7f50f889fc2ff
//
func PyrDown(src Mat, dst *Mat, ksize image.Point, borderType BorderType) {
pSize := C.struct_Size{
height: C.int(ksize.X),
width: C.int(ksize.Y),
}
C.PyrDown(src.p, dst.p, pSize, C.int(borderType))
}
// PyrUp upsamples an image and then blurs it.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gada75b59bdaaca411ed6fee10085eb784
//
func PyrUp(src Mat, dst *Mat, ksize image.Point, borderType BorderType) {
pSize := C.struct_Size{
height: C.int(ksize.X),
width: C.int(ksize.Y),
}
C.PyrUp(src.p, dst.p, pSize, C.int(borderType))
}
// MorphologyDefaultBorder returns "magic" border value for erosion and dilation.
// It is automatically transformed to Scalar::all(-DBL_MAX) for dilation.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga94756fad83d9d24d29c9bf478558c40a
//
func MorphologyDefaultBorderValue() Scalar {
var scalar C.Scalar = C.MorphologyDefaultBorderValue()
return NewScalar(float64(scalar.val1), float64(scalar.val2), float64(scalar.val3), float64(scalar.val4))
}
// MorphologyEx performs advanced morphological transformations.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga67493776e3ad1a3df63883829375201f
//
func MorphologyEx(src Mat, dst *Mat, op MorphType, kernel Mat) {
C.MorphologyEx(src.p, dst.p, C.int(op), kernel.p)
}
// MorphologyExWithParams performs advanced morphological transformations.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga67493776e3ad1a3df63883829375201f
//
func MorphologyExWithParams(src Mat, dst *Mat, op MorphType, kernel Mat, iterations int, borderType BorderType) {
pt := C.struct_Point{
x: C.int(-1),
y: C.int(-1),
}
C.MorphologyExWithParams(src.p, dst.p, C.int(op), kernel.p, pt, C.int(iterations), C.int(borderType))
}
// MorphShape is the shape of the structuring element used for Morphing operations.
type MorphShape int
const (
// MorphRect is the rectangular morph shape.
MorphRect MorphShape = 0
// MorphCross is the cross morph shape.
MorphCross MorphShape = 1
// MorphEllipse is the ellipse morph shape.
MorphEllipse MorphShape = 2
)
// GetStructuringElement returns a structuring element of the specified size
// and shape for morphological operations.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gac342a1bb6eabf6f55c803b09268e36dc
//
func GetStructuringElement(shape MorphShape, ksize image.Point) Mat {
sz := C.struct_Size{
width: C.int(ksize.X),
height: C.int(ksize.Y),
}
return newMat(C.GetStructuringElement(C.int(shape), sz))
}
// MorphType type of morphological operation.
type MorphType int
const (
// MorphErode operation
MorphErode MorphType = 0
// MorphDilate operation
MorphDilate MorphType = 1
// MorphOpen operation
MorphOpen MorphType = 2
// MorphClose operation
MorphClose MorphType = 3
// MorphGradient operation
MorphGradient MorphType = 4
// MorphTophat operation
MorphTophat MorphType = 5
// MorphBlackhat operation
MorphBlackhat MorphType = 6
// MorphHitmiss operation
MorphHitmiss MorphType = 7
)
// BorderType type of border.
type BorderType int
const (
// BorderConstant border type
BorderConstant BorderType = 0
// BorderReplicate border type
BorderReplicate BorderType = 1
// BorderReflect border type
BorderReflect BorderType = 2
// BorderWrap border type
BorderWrap BorderType = 3
// BorderReflect101 border type
BorderReflect101 BorderType = 4
// BorderTransparent border type
BorderTransparent BorderType = 5
// BorderDefault border type
BorderDefault = BorderReflect101
// BorderIsolated border type
BorderIsolated BorderType = 16
)
// GaussianBlur blurs an image Mat using a Gaussian filter.
// The function convolves the src Mat image into the dst Mat using
// the specified Gaussian kernel params.
//
// For further details, please see:
// http://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1
//
func GaussianBlur(src Mat, dst *Mat, ksize image.Point, sigmaX float64,
sigmaY float64, borderType BorderType) {
pSize := C.struct_Size{
width: C.int(ksize.X),
height: C.int(ksize.Y),
}
C.GaussianBlur(src.p, dst.p, pSize, C.double(sigmaX), C.double(sigmaY), C.int(borderType))
}
// GetGaussianKernel returns Gaussian filter coefficients.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gac05a120c1ae92a6060dd0db190a61afa
func GetGaussianKernel(ksize int, sigma float64) Mat {
return newMat(C.GetGaussianKernel(C.int(ksize), C.double(sigma), C.int(MatTypeCV64F)))
}
// GetGaussianKernelWithParams returns Gaussian filter coefficients.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gac05a120c1ae92a6060dd0db190a61afa
func GetGaussianKernelWithParams(ksize int, sigma float64, ktype MatType) Mat {
return newMat(C.GetGaussianKernel(C.int(ksize), C.double(sigma), C.int(ktype)))
}
// Sobel calculates the first, second, third, or mixed image derivatives using an extended Sobel operator
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d
//
func Sobel(src Mat, dst *Mat, ddepth MatType, dx, dy, ksize int, scale, delta float64, borderType BorderType) {
C.Sobel(src.p, dst.p, C.int(ddepth), C.int(dx), C.int(dy), C.int(ksize), C.double(scale), C.double(delta), C.int(borderType))
}
// SpatialGradient calculates the first order image derivative in both x and y using a Sobel operator.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga405d03b20c782b65a4daf54d233239a2
//
func SpatialGradient(src Mat, dx, dy *Mat, ksize MatType, borderType BorderType) {
C.SpatialGradient(src.p, dx.p, dy.p, C.int(ksize), C.int(borderType))
}
// Laplacian calculates the Laplacian of an image.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gad78703e4c8fe703d479c1860d76429e6
//
func Laplacian(src Mat, dst *Mat, dDepth MatType, size int, scale float64,
delta float64, borderType BorderType) {
C.Laplacian(src.p, dst.p, C.int(dDepth), C.int(size), C.double(scale), C.double(delta), C.int(borderType))
}
// Scharr calculates the first x- or y- image derivative using Scharr operator.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaa13106761eedf14798f37aa2d60404c9
//
func Scharr(src Mat, dst *Mat, dDepth MatType, dx int, dy int, scale float64,
delta float64, borderType BorderType) {
C.Scharr(src.p, dst.p, C.int(dDepth), C.int(dx), C.int(dy), C.double(scale), C.double(delta), C.int(borderType))
}
// MedianBlur blurs an image using the median filter.
//
// For further details, please see:
// https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9
//
func MedianBlur(src Mat, dst *Mat, ksize int) {
C.MedianBlur(src.p, dst.p, C.int(ksize))
}
// Canny finds edges in an image using the Canny algorithm.
// The function finds edges in the input image image and marks
// them in the output map edges using the Canny algorithm.
// The smallest value between threshold1 and threshold2 is used
// for edge linking. The largest value is used to
// find initial segments of strong edges.
// See http://en.wikipedia.org/wiki/Canny_edge_detector
//
// For further details, please see:
// http://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de
//
func Canny(src Mat, edges *Mat, t1 float32, t2 float32) {
C.Canny(src.p, edges.p, C.double(t1), C.double(t2))
}
// CornerSubPix Refines the corner locations. The function iterates to find
// the sub-pixel accurate location of corners or radial saddle points.
//
// For further details, please see:
// https://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga354e0d7c86d0d9da75de9b9701a9a87e
//
func CornerSubPix(img Mat, corners *Mat, winSize image.Point, zeroZone image.Point, criteria TermCriteria) {
winSz := C.struct_Size{
width: C.int(winSize.X),
height: C.int(winSize.Y),
}
zeroSz := C.struct_Size{
width: C.int(zeroZone.X),
height: C.int(zeroZone.Y),
}
C.CornerSubPix(img.p, corners.p, winSz, zeroSz, criteria.p)
return
}
// GoodFeaturesToTrack determines strong corners on an image. The function
// finds the most prominent corners in the image or in the specified image region.
//
// For further details, please see:
// https://docs.opencv.org/master/dd/d1a/group__imgproc__feature.html#ga1d6bb77486c8f92d79c8793ad995d541
//
func GoodFeaturesToTrack(img Mat, corners *Mat, maxCorners int, quality float64, minDist float64) {
C.GoodFeaturesToTrack(img.p, corners.p, C.int(maxCorners), C.double(quality), C.double(minDist))
}
// GrabCutMode is the flag for GrabCut algorithm.
type GrabCutMode int
const (
// GCInitWithRect makes the function initialize the state and the mask using the provided rectangle.
// After that it runs the itercount iterations of the algorithm.
GCInitWithRect GrabCutMode = 0
// GCInitWithMask makes the function initialize the state using the provided mask.
// GCInitWithMask and GCInitWithRect can be combined.
// Then all the pixels outside of the ROI are automatically initialized with GC_BGD.
GCInitWithMask GrabCutMode = 1
// GCEval means that the algorithm should just resume.
GCEval GrabCutMode = 2
// GCEvalFreezeModel means that the algorithm should just run a single iteration of the GrabCut algorithm
// with the fixed model