Skip to content

Commit fb12538

Browse files
author
Chih-Wei Chang
committed
Wrap initCameraMatrix2D with mat64.
1 parent b434835 commit fb12538

File tree

6 files changed

+174
-63
lines changed

6 files changed

+174
-63
lines changed

gocv/gocv.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,52 @@ package gocv
33
// #cgo CXXFLAGS: -std=c++11
44
// #cgo darwin pkg-config: opencv
55
import "C"
6+
import "github.com/gonum/matrix/mat64"
67

7-
func NewGcvPoint3f(x, y, z float32) GcvPoint3f_ {
8+
func NewGcvPoint3f(x, y, z float64) GcvPoint3f_ {
89
return NewGcvPoint3f_(float32(x), float32(y), float32(z))
910
}
1011

1112
func NewGcvPoint3d(x, y, z float64) GcvPoint3d_ {
1213
return NewGcvPoint3d_(float64(x), float64(y), float64(z))
1314
}
1415

15-
func NewGcvPoint2f(x, y float32) GcvPoint2f_ {
16+
func NewGcvPoint2f(x, y float64) GcvPoint2f_ {
1617
return NewGcvPoint2f_(float32(x), float32(y))
1718
}
1819

1920
func NewGcvPoint2d(x, y float64) GcvPoint2d_ {
2021
return NewGcvPoint2d_(float64(x), float64(y))
2122
}
2223

23-
func NewGcvSize2f(x, y float32) GcvSize2f_ {
24+
func NewGcvSize2f(x, y float64) GcvSize2f_ {
2425
return NewGcvSize2f_(float32(x), float32(y))
2526
}
2627

2728
func NewGcvSize2d(x, y float64) GcvSize2d_ {
2829
return NewGcvSize2d_(float64(x), float64(y))
2930
}
31+
32+
// Convert Mat, which defined by SWIG, to mat64.Dense.
33+
// The reason is the latter is much easier to handle
34+
// in Go.
35+
// GcvMat is assumed to be 2-dimensional matrix.
36+
func MatToMat64(mat Mat) *mat64.Dense {
37+
col := mat.GetCols()
38+
row := mat.GetRows()
39+
40+
data := []float64{}
41+
42+
for i := 0; i < row; i++ {
43+
for j := 0; j < col; j++ {
44+
if fltPtr, ok := mat.GcvAtd(i, j).(*float64); ok {
45+
data = append(data, *fltPtr)
46+
} else {
47+
panic("Non *float64 passed to MatToMat64")
48+
}
49+
50+
}
51+
}
52+
53+
return mat64.NewDense(row, col, data)
54+
}

gocv/gocv_calib3d.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "gocv_calib3d.hpp"
77

8-
cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) {
8+
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts) {
99
cv::Mat cameraMatrix;
1010

1111
std::vector<VecPoint3f> objPtsArr;
@@ -15,30 +15,27 @@ cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) {
1515
imgPtsArr.push_back(imgPts);
1616

1717
cameraMatrix = cv::initCameraMatrix2D(objPtsArr, imgPtsArr, cv::Size(1920, 1080), 1);
18-
std::cout << cameraMatrix.type() << std::endl;
1918
return cameraMatrix;
2019
}
2120

22-
double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts,
23-
cv::Size imgSize, cv::Mat cameraMatrix) {
21+
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
22+
cv::Size imgSize, cv::Mat& cameraMatrix,
23+
cv::Mat& rvec, cv::Mat& tvec) {
2424
std::vector<VecPoint3f> objPtsArr;
2525
std::vector<VecPoint2f> imgPtsArr;
2626
std::vector<cv::Mat> rvecs, tvecs;
2727
cv::Mat distCoeffs;
28-
2928
double rtn;
3029

3130
objPtsArr.push_back(objPts);
3231
imgPtsArr.push_back(imgPts);
3332

34-
std::cout << "init Camera" << cameraMatrix << std::endl;
35-
3633
rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, imgSize,
37-
cameraMatrix, distCoeffs, rvecs, tvecs);
34+
cameraMatrix, distCoeffs,
35+
rvecs, tvecs);
3836

39-
std::cout << "final Camera" << cameraMatrix << std::endl;
40-
std::cout << "final rvecs" << rvecs[0] << std::endl;
41-
std::cout << "final tvecs" << tvecs[0] << std::endl;
37+
rvec = rvecs[0];
38+
tvec = tvecs[0];
4239

4340
return rtn;
4441
}

gocv/gocv_calib3d.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package gocv
2+
3+
// #cgo CXXFLAGS: -std=c++11
4+
// #cgo darwin pkg-config: opencv
5+
import "C"
6+
import "github.com/gonum/matrix/mat64"
7+
8+
// GcvInitCameraMatrix2D takes one N-by-3 matrix and one
9+
// N-by-2 Matrix as input.
10+
// Each row in the input matrix represents a point in real
11+
// world (objPts) or in image (imgPts).
12+
// Return: the camera matrix.
13+
func GcvInitCameraMatrix2D(objPts, imgPts *mat64.Dense) (camMat *mat64.Dense) {
14+
nObjPts, objCol := objPts.Dims()
15+
nImgPts, imgCol := imgPts.Dims()
16+
17+
if objCol != 3 || imgCol != 2 || nObjPts != nImgPts {
18+
panic("Invalid dimensions for objPts and imgPts")
19+
}
20+
21+
objPtsVec := NewGcvPoint3fVector(int64(nObjPts))
22+
imgPtsVec := NewGcvPoint2fVector(int64(nObjPts))
23+
24+
for i := 0; i < nObjPts; i++ {
25+
objPtsVec.Set(i, NewGcvPoint3f(
26+
objPts.At(i, 0), objPts.At(i, 1), objPts.At(i, 2)))
27+
}
28+
29+
for i := 0; i < nObjPts; i++ {
30+
imgPtsVec.Set(i, NewGcvPoint2f(
31+
imgPts.At(i, 0), imgPts.At(i, 1)))
32+
}
33+
34+
camMat = MatToMat64(GcvInitCameraMatrix2D_(objPtsVec, imgPtsVec))
35+
return camMat
36+
}
37+
38+
// func TestGcvCalibrateCamera(t *testing.T) {
39+
// objPts := NewGcvPoint3fVector(int64(4))
40+
// objPts.Set(0, NewGcvPoint3f(0, 25, 0))
41+
// objPts.Set(1, NewGcvPoint3f(0, -25, 0))
42+
// objPts.Set(2, NewGcvPoint3f(-47, 25, 0))
43+
// objPts.Set(3, NewGcvPoint3f(-47, -25, 0))
44+
45+
// imgPts := NewGcvPoint2fVector(int64(4))
46+
// imgPts.Set(0, NewGcvPoint2f(1136.4140625, 1041.89208984))
47+
// imgPts.Set(1, NewGcvPoint2f(1845.33190918, 671.39581299))
48+
// imgPts.Set(2, NewGcvPoint2f(302.73373413, 634.79998779))
49+
// imgPts.Set(3, NewGcvPoint2f(1051.46154785, 352.76107788))
50+
51+
// imgSize := NewGcvSize2i(1920, 1080)
52+
53+
// camMat := GcvInitCameraMatrix2D(objPts, imgPts)
54+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0)))
55+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1)))
56+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1)))
57+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2)))
58+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2)))
59+
60+
// rvec := NewMat()
61+
// tvec := NewMat()
62+
63+
// GcvCalibrateCamera(objPts, imgPts, imgSize, camMat, rvec, tvec)
64+
65+
// MatToMat64(camMat)
66+
// }

gocv/gocv_calib3d.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
typedef std::vector<cv::Point3f> VecPoint3f;
66
typedef std::vector<cv::Point2f> VecPoint2f;
77

8-
cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts);
8+
cv::Mat GcvInitCameraMatrix2D_(VecPoint3f objPts, VecPoint2f imgPts);
99

10-
double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts,
11-
cv::Size2i imgSize, cv::Mat cameraMatrix);
10+
double GcvCalibrateCamera_(VecPoint3f objPts, VecPoint2f imgPts,
11+
cv::Size2i imgSize, cv::Mat& cameraMatrix,
12+
cv::Mat& rvec, cv::Mat& tvec);

gocv/gocv_core.i

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
%include "std_vector.i"
22

33
%{
4-
#include "opencv2/core/types_c.h"
5-
#include "opencv2/core/version.hpp"
64
#include "opencv2/core/core.hpp"
75
%}
86

@@ -256,12 +254,39 @@ namespace cv {
256254
void pop_back(size_t nelems=1);
257255

258256
//! special versions for 2D arrays (especially convenient for referencing image pixels)
257+
258+
template<typename _Tp> _Tp& at(int i0=0);
259+
template<typename _Tp> const _Tp& at(int i0=0) const;
260+
261+
template<typename _Tp> _Tp& at(int i0, int i1);
262+
template<typename _Tp> const _Tp& at(int i0, int i1) const;
263+
264+
template<typename _Tp> _Tp& at(int i0, int i1, int i2);
265+
template<typename _Tp> const _Tp& at(int i0, int i1, int i2) const;
266+
267+
template<typename _Tp> _Tp& at(const int* idx);
268+
template<typename _Tp> const _Tp& at(const int* idx) const;
269+
270+
template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
271+
template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
259272
template<typename _Tp> _Tp& at(cv::Point pt);
260273
template<typename _Tp> const _Tp& at(cv::Point pt) const;
261-
%template(gcvAtd) at<double>;
274+
262275
%template(gcvAtf) at<float>;
263-
};
276+
%template(gcvAtd) at<double>;
264277

278+
/*! includes several bit-fields:
279+
- the magic signature
280+
- continuity flag
281+
- depth
282+
- number of channels
283+
*/
284+
int flags;
285+
//! the matrix dimensionality, >= 2
286+
int dims;
287+
//! the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
288+
int rows, cols;
289+
};
265290
}
266291

267292
/* Additional STL types */

gocv/gocv_test.go

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/davecgh/go-spew/spew"
7+
"github.com/gonum/matrix/mat64"
78
)
89

910
func TestNewGcvPoint3f(t *testing.T) {
@@ -28,52 +29,48 @@ func TestMat(t *testing.T) {
2829
}
2930

3031
func TestGcvInitCameraMatrix2D(t *testing.T) {
31-
objPts := NewGcvPoint3fVector(int64(4))
32-
objPts.Set(0, NewGcvPoint3f(0, 25, 0))
33-
objPts.Set(1, NewGcvPoint3f(0, -25, 0))
34-
objPts.Set(2, NewGcvPoint3f(-47, 25, 0))
35-
objPts.Set(3, NewGcvPoint3f(-47, -25, 0))
36-
37-
imgPts := NewGcvPoint2fVector(int64(4))
38-
imgPts.Set(0, NewGcvPoint2f(1136.4140625, 1041.89208984))
39-
imgPts.Set(1, NewGcvPoint2f(1845.33190918, 671.39581299))
40-
imgPts.Set(2, NewGcvPoint2f(302.73373413, 634.79998779))
41-
imgPts.Set(3, NewGcvPoint2f(1051.46154785, 352.76107788))
32+
objPts := mat64.NewDense(4, 3, []float64{
33+
0, 25, 0,
34+
0, -25, 0,
35+
-47, 25, 0,
36+
-47, -25, 0})
37+
38+
imgPts := mat64.NewDense(4, 2, []float64{
39+
1136.4140625, 1041.89208984,
40+
1845.33190918, 671.39581299,
41+
302.73373413, 634.79998779,
42+
1051.46154785, 352.76107788})
4243

4344
camMat := GcvInitCameraMatrix2D(objPts, imgPts)
44-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0)))
45-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1)))
46-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1)))
47-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2)))
48-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2)))
45+
spew.Dump(camMat)
4946
}
5047

51-
func TestGcvCalibrateCamera(t *testing.T) {
52-
objPts := NewGcvPoint3fVector(int64(4))
53-
objPts.Set(0, NewGcvPoint3f(0, 25, 0))
54-
objPts.Set(1, NewGcvPoint3f(0, -25, 0))
55-
objPts.Set(2, NewGcvPoint3f(-47, 25, 0))
56-
objPts.Set(3, NewGcvPoint3f(-47, -25, 0))
48+
// func TestGcvCalibrateCamera(t *testing.T) {
49+
// objPts := NewGcvPoint3fVector(int64(4))
50+
// objPts.Set(0, NewGcvPoint3f(0, 25, 0))
51+
// objPts.Set(1, NewGcvPoint3f(0, -25, 0))
52+
// objPts.Set(2, NewGcvPoint3f(-47, 25, 0))
53+
// objPts.Set(3, NewGcvPoint3f(-47, -25, 0))
5754

58-
imgPts := NewGcvPoint2fVector(int64(4))
59-
imgPts.Set(0, NewGcvPoint2f(1136.4140625, 1041.89208984))
60-
imgPts.Set(1, NewGcvPoint2f(1845.33190918, 671.39581299))
61-
imgPts.Set(2, NewGcvPoint2f(302.73373413, 634.79998779))
62-
imgPts.Set(3, NewGcvPoint2f(1051.46154785, 352.76107788))
55+
// imgPts := NewGcvPoint2fVector(int64(4))
56+
// imgPts.Set(0, NewGcvPoint2f(1136.4140625, 1041.89208984))
57+
// imgPts.Set(1, NewGcvPoint2f(1845.33190918, 671.39581299))
58+
// imgPts.Set(2, NewGcvPoint2f(302.73373413, 634.79998779))
59+
// imgPts.Set(3, NewGcvPoint2f(1051.46154785, 352.76107788))
6360

64-
imgSize := NewGcvSize2i(1920, 1080)
61+
// imgSize := NewGcvSize2i(1920, 1080)
6562

66-
camMat := GcvInitCameraMatrix2D(objPts, imgPts)
67-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0)))
68-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1)))
69-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1)))
70-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2)))
71-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2)))
72-
73-
GcvCalibrateCamera(objPts, imgPts, imgSize, camMat)
74-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0)))
75-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1)))
76-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1)))
77-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2)))
78-
spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2)))
79-
}
63+
// camMat := GcvInitCameraMatrix2D(objPts, imgPts)
64+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0)))
65+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1)))
66+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1)))
67+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2)))
68+
// spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2)))
69+
70+
// rvec := NewMat()
71+
// tvec := NewMat()
72+
73+
// GcvCalibrateCamera(objPts, imgPts, imgSize, camMat, rvec, tvec)
74+
75+
// MatToMat64(camMat)
76+
// }

0 commit comments

Comments
 (0)