Complete initCameraMatrix2D, calibrateCamera interface.
This commit is contained in:
parent
2afcd2a05b
commit
40ed44c20c
4 changed files with 27 additions and 17 deletions
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
#include "gocv_calib3d.hpp"
|
#include "gocv_calib3d.hpp"
|
||||||
|
|
||||||
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts) {
|
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts, cv::Size imgSize, double aspectRatio) {
|
||||||
cv::Mat cameraMatrix;
|
cv::Mat cameraMatrix;
|
||||||
|
|
||||||
std::vector<VecPoint3f> objPtsArr;
|
std::vector<VecPoint3f> objPtsArr;
|
||||||
|
|
@ -14,17 +14,16 @@ cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts) {
|
||||||
objPtsArr.push_back(objPts);
|
objPtsArr.push_back(objPts);
|
||||||
imgPtsArr.push_back(imgPts);
|
imgPtsArr.push_back(imgPts);
|
||||||
|
|
||||||
cameraMatrix = cv::initCameraMatrix2D(objPtsArr, imgPtsArr, cv::Size(1920, 1080), 1);
|
cameraMatrix = cv::initCameraMatrix2D(objPtsArr, imgPtsArr, imgSize, aspectRatio);
|
||||||
return cameraMatrix;
|
return cameraMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
|
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
|
||||||
cv::Size imgSize, cv::Mat& cameraMatrix,
|
cv::Size imgSize, cv::Mat& cameraMatrix, cv::Mat distCoeffs,
|
||||||
cv::Mat& rvec, cv::Mat& tvec) {
|
cv::Mat& rvec, cv::Mat& tvec, int flags) {
|
||||||
std::vector<VecPoint3f> objPtsArr;
|
std::vector<VecPoint3f> objPtsArr;
|
||||||
std::vector<VecPoint2f> imgPtsArr;
|
std::vector<VecPoint2f> imgPtsArr;
|
||||||
std::vector<cv::Mat> rvecs, tvecs;
|
std::vector<cv::Mat> rvecs, tvecs;
|
||||||
cv::Mat distCoeffs;
|
|
||||||
double rtn;
|
double rtn;
|
||||||
|
|
||||||
objPtsArr.push_back(objPts);
|
objPtsArr.push_back(objPts);
|
||||||
|
|
@ -37,7 +36,7 @@ double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
|
||||||
|
|
||||||
rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, imgSize,
|
rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, imgSize,
|
||||||
cameraMatrix, distCoeffs,
|
cameraMatrix, distCoeffs,
|
||||||
rvecs, tvecs, 14575);
|
rvecs, tvecs, flags);
|
||||||
|
|
||||||
rvec = rvecs[0];
|
rvec = rvecs[0];
|
||||||
tvec = tvecs[0];
|
tvec = tvecs[0];
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,9 @@ import "github.com/gonum/matrix/mat64"
|
||||||
// Each column in the input matrix represents a point in real world (objPts) or
|
// Each column in the input matrix represents a point in real world (objPts) or
|
||||||
// in image (imgPts).
|
// in image (imgPts).
|
||||||
// Return: the camera matrix.
|
// Return: the camera matrix.
|
||||||
func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense) (camMat *mat64.Dense) {
|
func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense, dims [2]int,
|
||||||
|
aspectRatio float64) (camMat *mat64.Dense) {
|
||||||
|
|
||||||
objDim, nObjPts := objPts.Dims()
|
objDim, nObjPts := objPts.Dims()
|
||||||
imgDim, nImgPts := imgPts.Dims()
|
imgDim, nImgPts := imgPts.Dims()
|
||||||
|
|
||||||
|
|
@ -28,12 +30,16 @@ func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense) (camMat *mat64.Dense) {
|
||||||
imgPtsVec.Set(j, NewGcvPoint2f32(imgPts.Col(nil, j)...))
|
imgPtsVec.Set(j, NewGcvPoint2f32(imgPts.Col(nil, j)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
camMat = GcvMatToMat64(GcvInitCameraMatrix2D_(objPtsVec, imgPtsVec))
|
_imgSize := NewGcvSize2i(dims[0], dims[1])
|
||||||
|
|
||||||
|
camMat = GcvMatToMat64(GcvInitCameraMatrix2D_(
|
||||||
|
objPtsVec, imgPtsVec, _imgSize, aspectRatio))
|
||||||
return camMat
|
return camMat
|
||||||
}
|
}
|
||||||
|
|
||||||
func GcvCalibrateCamera(objPts, imgPts, camMat *mat64.Dense, dims [2]int) (
|
func GcvCalibrateCamera(objPts, imgPts, camMat, distCoeffs *mat64.Dense,
|
||||||
calCamMat, rvec, tvec *mat64.Dense) {
|
dims [2]int, flags int) (calCamMat, rvec, tvec *mat64.Dense) {
|
||||||
|
|
||||||
objDim, nObjPts := objPts.Dims()
|
objDim, nObjPts := objPts.Dims()
|
||||||
imgDim, nImgPts := imgPts.Dims()
|
imgDim, nImgPts := imgPts.Dims()
|
||||||
|
|
||||||
|
|
@ -53,13 +59,15 @@ func GcvCalibrateCamera(objPts, imgPts, camMat *mat64.Dense, dims [2]int) (
|
||||||
}
|
}
|
||||||
|
|
||||||
_camMat := Mat64ToGcvMat(camMat)
|
_camMat := Mat64ToGcvMat(camMat)
|
||||||
|
_distCoeffs := Mat64ToGcvMat(distCoeffs)
|
||||||
_rvec := NewGcvMat()
|
_rvec := NewGcvMat()
|
||||||
_tvec := NewGcvMat()
|
_tvec := NewGcvMat()
|
||||||
_imgSize := NewGcvSize2i(dims[0], dims[1])
|
_imgSize := NewGcvSize2i(dims[0], dims[1])
|
||||||
|
|
||||||
GcvCalibrateCamera_(
|
GcvCalibrateCamera_(
|
||||||
objPtsVec, imgPtsVec,
|
objPtsVec, imgPtsVec,
|
||||||
_imgSize, _camMat, _rvec, _tvec)
|
_imgSize, _camMat, _distCoeffs,
|
||||||
|
_rvec, _tvec, flags)
|
||||||
|
|
||||||
calCamMat = GcvMatToMat64(_camMat)
|
calCamMat = GcvMatToMat64(_camMat)
|
||||||
rvec = GcvMatToMat64(_rvec)
|
rvec = GcvMatToMat64(_rvec)
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,11 @@
|
||||||
typedef std::vector<cv::Point3f> VecPoint3f;
|
typedef std::vector<cv::Point3f> VecPoint3f;
|
||||||
typedef std::vector<cv::Point2f> VecPoint2f;
|
typedef std::vector<cv::Point2f> VecPoint2f;
|
||||||
|
|
||||||
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts);
|
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts,
|
||||||
|
cv::Size imgSize, double aspectRatio);
|
||||||
|
|
||||||
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
|
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
|
||||||
cv::Size2i imgSize, cv::Mat& cameraMatrix,
|
cv::Size imgSize, cv::Mat& cameraMatrix, cv::Mat distCoeffs,
|
||||||
cv::Mat& rvec, cv::Mat& tvec);
|
cv::Mat& rvec, cv::Mat& tvec, int flags);
|
||||||
|
|
||||||
void GcvRodrigues_(cv::Mat src, cv::Mat& dst);
|
void GcvRodrigues_(cv::Mat src, cv::Mat& dst);
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ func TestGcvInitCameraMatrix2D(t *testing.T) {
|
||||||
})
|
})
|
||||||
imgPts.TCopy(imgPts)
|
imgPts.TCopy(imgPts)
|
||||||
|
|
||||||
camMat := GcvInitCameraMatrix2D(objPts, imgPts)
|
camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)
|
||||||
assert.InDeltaSlice(t, []float64{1.47219772e+03, 0.00000000e+00, 9.59500000e+02},
|
assert.InDeltaSlice(t, []float64{1.47219772e+03, 0.00000000e+00, 9.59500000e+02},
|
||||||
camMat.Row(nil, 0), DELTA)
|
camMat.Row(nil, 0), DELTA)
|
||||||
assert.InDeltaSlice(t, []float64{0.00000000e+00, 1.47219772e+03, 5.39500000e+02},
|
assert.InDeltaSlice(t, []float64{0.00000000e+00, 1.47219772e+03, 5.39500000e+02},
|
||||||
|
|
@ -78,10 +78,12 @@ func TestGcvCalibrateCamera(t *testing.T) {
|
||||||
})
|
})
|
||||||
imgPts.TCopy(imgPts)
|
imgPts.TCopy(imgPts)
|
||||||
|
|
||||||
camMat := GcvInitCameraMatrix2D(objPts, imgPts)
|
camMat := GcvInitCameraMatrix2D(objPts, imgPts, [2]int{1920, 1080}, 1)
|
||||||
|
|
||||||
|
distCoeffs := mat64.NewDense(5, 1, []float64{0, 0, 0, 0, 0})
|
||||||
|
|
||||||
camMat, rvec, tvec := GcvCalibrateCamera(
|
camMat, rvec, tvec := GcvCalibrateCamera(
|
||||||
objPts, imgPts, camMat, [2]int{1920, 1080})
|
objPts, imgPts, camMat, distCoeffs, [2]int{1920, 1080}, 14575)
|
||||||
|
|
||||||
assert.InDeltaSlice(t, []float64{-46.15296606, 0., 959.5}, camMat.Row(nil, 0), DELTA)
|
assert.InDeltaSlice(t, []float64{-46.15296606, 0., 959.5}, camMat.Row(nil, 0), DELTA)
|
||||||
assert.InDeltaSlice(t, []float64{0., -46.15296606, 539.5}, camMat.Row(nil, 1), DELTA)
|
assert.InDeltaSlice(t, []float64{0., -46.15296606, 539.5}, camMat.Row(nil, 1), DELTA)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue