forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calib3d.go
140 lines (118 loc) · 5.65 KB
/
calib3d.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
package gocv
/*
#include <stdlib.h>
#include "calib3d.h"
*/
import "C"
import "image"
// Calib is a wrapper around OpenCV's "Camera Calibration and 3D Reconstruction" of
// Fisheye Camera model
//
// For more details, please see:
// https://docs.opencv.org/trunk/db/d58/group__calib3d__fisheye.html
// CalibFlag value for calibration
type CalibFlag int32
const (
// CalibUseIntrinsicGuess indicates that cameraMatrix contains valid initial values
// of fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially
// set to the image center ( imageSize is used), and focal distances are computed
// in a least-squares fashion.
CalibUseIntrinsicGuess CalibFlag = 1 << iota
// CalibRecomputeExtrinsic indicates that extrinsic will be recomputed after each
// iteration of intrinsic optimization.
CalibRecomputeExtrinsic
// CalibCheckCond indicates that the functions will check validity of condition number
CalibCheckCond
// CalibFixSkew indicates that skew coefficient (alpha) is set to zero and stay zero
CalibFixSkew
// CalibFixK1 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK1
// CalibFixK2 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK2
// CalibFixK3 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK3
// CalibFixK4 indicates that selected distortion coefficients are set to zeros and stay zero
CalibFixK4
// CalibFixIntrinsic indicates that fix K1, K2? and D1, D2? so that only R, T matrices are estimated
CalibFixIntrinsic
// CalibFixPrincipalPoint indicates that the principal point is not changed during the global optimization.
// It stays at the center or at a different location specified when CalibUseIntrinsicGuess is set too.
CalibFixPrincipalPoint
)
// FisheyeUndistortImage transforms an image to compensate for fisheye lens distortion
func FisheyeUndistortImage(distorted Mat, undistorted *Mat, k, d Mat) {
C.Fisheye_UndistortImage(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr())
}
// FisheyeUndistortImageWithParams transforms an image to compensate for fisheye lens distortion with Knew matrix
func FisheyeUndistortImageWithParams(distorted Mat, undistorted *Mat, k, d, knew Mat, size image.Point) {
sz := C.struct_Size{
width: C.int(size.X),
height: C.int(size.Y),
}
C.Fisheye_UndistortImageWithParams(distorted.Ptr(), undistorted.Ptr(), k.Ptr(), d.Ptr(), knew.Ptr(), sz)
}
// InitUndistortRectifyMap computes the joint undistortion and rectification transformation and represents the result in the form of maps for remap
//
// For further details, please see:
// https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7dfb72c9cf9780a347fbe3d1c47e5d5a
//
func InitUndistortRectifyMap(cameraMatrix Mat, distCoeffs Mat, r Mat, newCameraMatrix Mat, size image.Point, m1type int, map1 Mat, map2 Mat) {
sz := C.struct_Size{
width: C.int(size.X),
height: C.int(size.Y),
}
C.InitUndistortRectifyMap(cameraMatrix.Ptr(), distCoeffs.Ptr(), r.Ptr(), newCameraMatrix.Ptr(), sz, C.int(m1type), map1.Ptr(), map2.Ptr())
}
// GetOptimalNewCameraMatrixWithParams computes and returns the optimal new camera matrix based on the free scaling parameter.
//
// For further details, please see:
// https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga7a6c4e032c97f03ba747966e6ad862b1
//
func GetOptimalNewCameraMatrixWithParams(cameraMatrix Mat, distCoeffs Mat, imageSize image.Point, alpha float64, newImgSize image.Point, centerPrincipalPoint bool) (Mat, image.Rectangle) {
sz := C.struct_Size{
width: C.int(imageSize.X),
height: C.int(imageSize.Y),
}
newSize := C.struct_Size{
width: C.int(newImgSize.X),
height: C.int(newImgSize.Y),
}
rt := C.struct_Rect{}
return newMat(C.GetOptimalNewCameraMatrixWithParams(cameraMatrix.Ptr(), distCoeffs.Ptr(), sz, C.double(alpha), newSize, &rt, C.bool(centerPrincipalPoint))), toRect(rt)
}
func Undistort(src Mat, dst *Mat, cameraMatrix Mat, distCoeffs Mat, newCameraMatrix Mat) {
C.Undistort(src.Ptr(), dst.Ptr(), cameraMatrix.Ptr(), distCoeffs.Ptr(), newCameraMatrix.Ptr())
}
// CalibCBFlag value for chessboard calibration
// For more details, please see:
// https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a
type CalibCBFlag int
const (
// Various operation flags that can be zero or a combination of the following values:
// Use adaptive thresholding to convert the image to black and white, rather than a fixed threshold level (computed from the average image brightness).
CalibCBAdaptiveThresh CalibCBFlag = 1 << iota
// Normalize the image gamma with equalizeHist before applying fixed or adaptive thresholding.
CalibCBNormalizeImage
// Use additional criteria (like contour area, perimeter, square-like shape) to filter out false quads extracted at the contour retrieval stage.
CalibCBFilterQuads
// Run a fast check on the image that looks for chessboard corners, and shortcut the call if none is found. This can drastically speed up the call in the degenerate condition when no chessboard is observed.
CalibCBFastCheck
CalibCBExhaustive
CalibCBAccuracy
CalibCBLarger
CalibCBMarker
)
func FindChessboardCorners(image Mat, patternSize image.Point, corners *Mat, flags CalibCBFlag) bool {
sz := C.struct_Size{
width: C.int(patternSize.X),
height: C.int(patternSize.Y),
}
return bool(C.FindChessboardCorners(image.Ptr(), sz, corners.Ptr(), C.int(flags)))
}
func DrawChessboardCorners(image *Mat, patternSize image.Point, corners Mat, patternWasFound bool) {
sz := C.struct_Size{
width: C.int(patternSize.X),
height: C.int(patternSize.Y),
}
C.DrawChessboardCorners(image.Ptr(), sz, corners.Ptr(), C.bool(patternWasFound))
}