From c9330d178252b374ccde2ce432641c4d6882b830 Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Mon, 16 Feb 2015 00:35:37 +0800 Subject: [PATCH] Try to move all code under same package. It seems we have to put all code under the same package, otherwise the type mapping given by SWIG will be a little hard to maintain. --- opencv2/gcv_core/gcv_core.cpp | 44 ++++++++++++++++++ opencv2/gcv_core/gcv_core.hpp | 11 +++++ opencv2/gcv_core/gcv_core.swigcxx | 76 +++++++++++++++++++++++++++++++ opencv2/gcv_core/gcv_core_test.go | 26 +++++++++++ 4 files changed, 157 insertions(+) create mode 100644 opencv2/gcv_core/gcv_core.hpp diff --git a/opencv2/gcv_core/gcv_core.cpp b/opencv2/gcv_core/gcv_core.cpp index e69de29..03cc031 100644 --- a/opencv2/gcv_core/gcv_core.cpp +++ b/opencv2/gcv_core/gcv_core.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +#include "gcv_core.hpp" + +cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) { + cv::Mat cameraMatrix; + + std::vector objPtsArr; + std::vector imgPtsArr; + + objPtsArr.push_back(objPts); + imgPtsArr.push_back(imgPts); + + cameraMatrix = cv::initCameraMatrix2D(objPtsArr, imgPtsArr, cv::Size(1920, 1080), 1); + std::cout << cameraMatrix.type() << std::endl; + return cameraMatrix; +} + +double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts, + cv::Size imgSize, cv::Mat cameraMatrix) { + std::vector objPtsArr; + std::vector imgPtsArr; + std::vector rvecs, tvecs; + cv::Mat distCoeffs; + + double rtn; + + objPtsArr.push_back(objPts); + imgPtsArr.push_back(imgPts); + + std::cout << "init Camera" << cameraMatrix << std::endl; + + rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, imgSize, + cameraMatrix, distCoeffs, rvecs, tvecs); + + std::cout << "final Camera" << cameraMatrix << std::endl; + std::cout << "final rvecs" << rvecs[0] << std::endl; + std::cout << "final tvecs" << tvecs[0] << std::endl; + + return rtn; +} diff --git a/opencv2/gcv_core/gcv_core.hpp b/opencv2/gcv_core/gcv_core.hpp new file mode 100644 index 0000000..3710c3c --- /dev/null +++ b/opencv2/gcv_core/gcv_core.hpp @@ -0,0 +1,11 @@ +#include +#include +#include + +typedef std::vector VecPoint3f; +typedef std::vector VecPoint2f; + +cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts); + +double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts, + cv::Size2i imgSize, cv::Mat cameraMatrix); diff --git a/opencv2/gcv_core/gcv_core.swigcxx b/opencv2/gcv_core/gcv_core.swigcxx index 6fdc6d9..dd7ea96 100644 --- a/opencv2/gcv_core/gcv_core.swigcxx +++ b/opencv2/gcv_core/gcv_core.swigcxx @@ -5,8 +5,11 @@ #include "opencv2/core/types_c.h" #include "opencv2/core/version.hpp" #include "opencv2/core/core.hpp" +#include "gcv_core.hpp" %} +%include "gcv_core.hpp" + /* Classes defined in core.hpp */ namespace cv { @@ -190,6 +193,79 @@ namespace cv { %template(GcvPoint3i) Point3_; %template(GcvPoint3f_) Point3_; %template(GcvPoint3d_) Point3_; + + + /* ----------------- Mat ----------------- */ + class Mat + { + public: + //! default constructor + Mat(); + //! constructs 2D matrix of the specified size and type + // (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.) + Mat(int rows, int cols, int type); + Mat(cv::Size size, int type); + //! constucts 2D matrix and fills it with the specified value _s. + Mat(int rows, int cols, int type, const cv::Scalar& s); + Mat(cv::Size size, int type, const cv::Scalar& s); + + //! copy constructor + Mat(const Mat& m); + + //! builds matrix from std::vector with or without copying the data + template explicit Mat(const vector<_Tp>& vec, bool copyData=false); + //! builds matrix from cv::Vec; the data is copied by default + template explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true); + //! builds matrix from cv::Matx; the data is copied by default + template explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true); + //! builds matrix from a 2D point + template explicit Mat(const Point_<_Tp>& pt, bool copyData=true); + //! builds matrix from a 3D point + template explicit Mat(const Point3_<_Tp>& pt, bool copyData=true); + + //! destructor - calls release() + ~Mat(); + + //! returns a new matrix header for the specified row + Mat row(int y) const; + //! returns a new matrix header for the specified column + Mat col(int x) const; + //! ... for the specified row span + Mat rowRange(int startrow, int endrow) const; + //! ... for the specified column span + Mat colRange(int startcol, int endcol) const; + //! ... for the specified diagonal + // (d=0 - the main diagonal, + // >0 - a diagonal from the lower half, + // <0 - a diagonal from the upper half) + Mat diag(int d=0) const; + //! constructs a square diagonal matrix which main diagonal is vector "d" + static Mat diag(const Mat& d); + + //! returns deep copy of the matrix, i.e. the data is copied + Mat clone() const; + + void assignTo( Mat& m, int type=-1 ) const; + + //! creates alternative matrix header for the same data, with different + // number of channels and/or different number of rows. see cvReshape. + Mat reshape(int cn, int rows=0) const; + Mat reshape(int cn, int newndims, const int* newsz) const; + + //! adds element to the end of 1d matrix (or possibly multiple elements when _Tp=Mat) + template void push_back(const _Tp& elem); + template void push_back(const Mat_<_Tp>& elem); + void push_back(const Mat& m); + //! removes several hyper-planes from bottom of the matrix + void pop_back(size_t nelems=1); + + //! special versions for 2D arrays (especially convenient for referencing image pixels) + template _Tp& at(cv::Point pt); + template const _Tp& at(cv::Point pt) const; + %template(gcvAtd) at; + %template(gcvAtf) at; + }; + } /* Additional STL types */ diff --git a/opencv2/gcv_core/gcv_core_test.go b/opencv2/gcv_core/gcv_core_test.go index c74133a..c2dfa4f 100644 --- a/opencv2/gcv_core/gcv_core_test.go +++ b/opencv2/gcv_core/gcv_core_test.go @@ -20,3 +20,29 @@ func TestNewGcvSize2d(t *testing.T) { size := NewGcvSize2d(3, 1) spew.Dump(size) } + +func TestMat(t *testing.T) { + mat := NewMat() + mat2 := NewMat(mat) + spew.Dump(mat2) +} +func TestGcvInitCameraMatrix2D(t *testing.T) { + objPts := NewGcvPoint3fVector(int64(4)) + objPts.Set(0, NewGcvPoint3f(0, 25, 0)) + objPts.Set(1, NewGcvPoint3f(0, -25, 0)) + objPts.Set(2, NewGcvPoint3f(-47, 25, 0)) + objPts.Set(3, NewGcvPoint3f(-47, -25, 0)) + + imgPts := NewGcvPoint2fVector(int64(4)) + imgPts.Set(0, NewGcvPoint2f(1136.4140625, 1041.89208984)) + imgPts.Set(1, NewGcvPoint2f(1845.33190918, 671.39581299)) + imgPts.Set(2, NewGcvPoint2f(302.73373413, 634.79998779)) + imgPts.Set(3, NewGcvPoint2f(1051.46154785, 352.76107788)) + + camMat := GcvInitCameraMatrix2D(objPts, imgPts) + spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 0))) + spew.Dump(camMat.GcvAtd(NewGcvSize2i(0, 1))) + spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 1))) + spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2))) + spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2))) +}