From a2e19543f74809c64397240b67054315c5f2811f Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Sun, 15 Feb 2015 16:11:09 +0800 Subject: [PATCH] Wrap basic types. --- opencv2/gcv_calib3d/gcv_calib3d.cpp | 5 +- opencv2/gcv_calib3d/gcv_calib3d.hpp | 2 +- opencv2/gcv_calib3d/gcv_calib3d_test.go | 62 +++--- .../gcv_utils.go => gcv_core/gcv_core.go} | 2 +- opencv2/gcv_core/gcv_core.swigcxx | 202 ++++++++++++++++++ opencv2/gcv_utils/gcv_utils.hpp | 12 -- opencv2/gcv_utils/gcv_utils.swigcxx | 16 -- 7 files changed, 244 insertions(+), 57 deletions(-) rename opencv2/{gcv_utils/gcv_utils.go => gcv_core/gcv_core.go} (80%) create mode 100644 opencv2/gcv_core/gcv_core.swigcxx delete mode 100644 opencv2/gcv_utils/gcv_utils.hpp delete mode 100644 opencv2/gcv_utils/gcv_utils.swigcxx diff --git a/opencv2/gcv_calib3d/gcv_calib3d.cpp b/opencv2/gcv_calib3d/gcv_calib3d.cpp index 19fbafb..1b3f997 100644 --- a/opencv2/gcv_calib3d/gcv_calib3d.cpp +++ b/opencv2/gcv_calib3d/gcv_calib3d.cpp @@ -19,7 +19,7 @@ cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) { } double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts, - std::vector imgSize, cv::Mat cameraMatrix) { + cv::Size imgSize, cv::Mat cameraMatrix) { std::vector objPtsArr; std::vector imgPtsArr; std::vector rvecs, tvecs; @@ -32,8 +32,7 @@ double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts, std::cout << "init Camera" << cameraMatrix << std::endl; - rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, - cv::Size2i(imgSize[0], imgSize[1]), + rtn = cv::calibrateCamera(objPtsArr, imgPtsArr, imgSize, cameraMatrix, distCoeffs, rvecs, tvecs); std::cout << "final Camera" << cameraMatrix << std::endl; diff --git a/opencv2/gcv_calib3d/gcv_calib3d.hpp b/opencv2/gcv_calib3d/gcv_calib3d.hpp index 15d2295..3710c3c 100644 --- a/opencv2/gcv_calib3d/gcv_calib3d.hpp +++ b/opencv2/gcv_calib3d/gcv_calib3d.hpp @@ -8,4 +8,4 @@ typedef std::vector VecPoint2f; cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts); double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts, - std::vector imgSize, cv::Mat cameraMatrix); + cv::Size2i imgSize, cv::Mat cameraMatrix); diff --git a/opencv2/gcv_calib3d/gcv_calib3d_test.go b/opencv2/gcv_calib3d/gcv_calib3d_test.go index abd3e94..55e01ae 100644 --- a/opencv2/gcv_calib3d/gcv_calib3d_test.go +++ b/opencv2/gcv_calib3d/gcv_calib3d_test.go @@ -2,7 +2,7 @@ package gcv_calib3d import "testing" -import "github.com/lazywei/go-opencv/opencv2/gcv_utils" +import "github.com/lazywei/go-opencv/opencv2/gcv_core" // [[[ 0. 25. 0.] // [ 0. -25. 0.] @@ -18,37 +18,51 @@ import "github.com/lazywei/go-opencv/opencv2/gcv_utils" // [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]] func TestGcvInitCameraMatrix2D(t *testing.T) { - objPts := gcv_utils.NewGcvPoint3fVector(int64(4)) - objPts.Set(0, gcv_utils.GetPoint3f(0, 25, 0)) - objPts.Set(1, gcv_utils.GetPoint3f(0, -25, 0)) - objPts.Set(2, gcv_utils.GetPoint3f(-47, 25, 0)) - objPts.Set(3, gcv_utils.GetPoint3f(-47, -25, 0)) + objPts := gcv_core.NewGcvPoint3fVector(int64(4)) + objPts.Set(0, gcv_core.NewGcvPoint3f( + float32(0), float32(25), float32(0))) + objPts.Set(1, gcv_core.NewGcvPoint3f( + float32(0), float32(-25), float32(0))) + objPts.Set(2, gcv_core.NewGcvPoint3f( + float32(-47), float32(25), float32(0))) + objPts.Set(3, gcv_core.NewGcvPoint3f( + float32(-47), float32(-25), float32(0))) - imgPts := gcv_utils.NewGcvPoint2fVector(int64(4)) - imgPts.Set(0, gcv_utils.GetPoint2f(1136.4140625, 1041.89208984)) - imgPts.Set(1, gcv_utils.GetPoint2f(1845.33190918, 671.39581299)) - imgPts.Set(2, gcv_utils.GetPoint2f(302.73373413, 634.79998779)) - imgPts.Set(3, gcv_utils.GetPoint2f(1051.46154785, 352.76107788)) + imgPts := gcv_core.NewGcvPoint2fVector(int64(4)) + imgPts.Set(0, gcv_core.NewGcvPoint2f( + float32(1136.4140625), float32(1041.89208984))) + imgPts.Set(1, gcv_core.NewGcvPoint2f( + float32(1845.33190918), float32(671.39581299))) + imgPts.Set(2, gcv_core.NewGcvPoint2f( + float32(302.73373413), float32(634.79998779))) + imgPts.Set(3, gcv_core.NewGcvPoint2f( + float32(1051.46154785), float32(352.76107788))) GcvInitCameraMatrix2D(objPts, imgPts) } func TestGcvCalibrateCamera(t *testing.T) { - objPts := gcv_utils.NewGcvPoint3fVector(int64(4)) - objPts.Set(0, gcv_utils.GetPoint3f(0, 25, 0)) - objPts.Set(1, gcv_utils.GetPoint3f(0, -25, 0)) - objPts.Set(2, gcv_utils.GetPoint3f(-47, 25, 0)) - objPts.Set(3, gcv_utils.GetPoint3f(-47, -25, 0)) + objPts := gcv_core.NewGcvPoint3fVector(int64(4)) + objPts.Set(0, gcv_core.NewGcvPoint3f( + float32(0), float32(25), float32(0))) + objPts.Set(1, gcv_core.NewGcvPoint3f( + float32(0), float32(-25), float32(0))) + objPts.Set(2, gcv_core.NewGcvPoint3f( + float32(-47), float32(25), float32(0))) + objPts.Set(3, gcv_core.NewGcvPoint3f( + float32(-47), float32(-25), float32(0))) - imgPts := gcv_utils.NewGcvPoint2fVector(int64(4)) - imgPts.Set(0, gcv_utils.GetPoint2f(1136.4140625, 1041.89208984)) - imgPts.Set(1, gcv_utils.GetPoint2f(1845.33190918, 671.39581299)) - imgPts.Set(2, gcv_utils.GetPoint2f(302.73373413, 634.79998779)) - imgPts.Set(3, gcv_utils.GetPoint2f(1051.46154785, 352.76107788)) + imgPts := gcv_core.NewGcvPoint2fVector(int64(4)) + imgPts.Set(0, gcv_core.NewGcvPoint2f( + float32(1136.4140625), float32(1041.89208984))) + imgPts.Set(1, gcv_core.NewGcvPoint2f( + float32(1845.33190918), float32(671.39581299))) + imgPts.Set(2, gcv_core.NewGcvPoint2f( + float32(302.73373413), float32(634.79998779))) + imgPts.Set(3, gcv_core.NewGcvPoint2f( + float32(1051.46154785), float32(352.76107788))) - imgSize := gcv_utils.NewGcvIntVector(int64(2)) - imgSize.Set(0, 1920) - imgSize.Set(1, 1080) + imgSize := gcv_core.NewGcvSize2i(1920, 1080) camMat := GcvInitCameraMatrix2D(objPts, imgPts) diff --git a/opencv2/gcv_utils/gcv_utils.go b/opencv2/gcv_core/gcv_core.go similarity index 80% rename from opencv2/gcv_utils/gcv_utils.go rename to opencv2/gcv_core/gcv_core.go index 45b5f12..489b765 100644 --- a/opencv2/gcv_utils/gcv_utils.go +++ b/opencv2/gcv_core/gcv_core.go @@ -1,4 +1,4 @@ -package gcv_utils +package gcv_core // #cgo CXXFLAGS: -std=c++11 // #cgo darwin pkg-config: opencv diff --git a/opencv2/gcv_core/gcv_core.swigcxx b/opencv2/gcv_core/gcv_core.swigcxx new file mode 100644 index 0000000..72ebb24 --- /dev/null +++ b/opencv2/gcv_core/gcv_core.swigcxx @@ -0,0 +1,202 @@ +%module gcv_core +%include "std_vector.i" + +%{ +#include "opencv2/core/types_c.h" +#include "opencv2/core/version.hpp" +#include "opencv2/core/core.hpp" +%} + +/* Classes defined in core.hpp */ +namespace cv { + + template class Size_; + template class Point_; + template class Rect_; + template class Vec; + + //////////////////////////////// Point_ //////////////////////////////// + + /*! + template 2D point class. + + The class defines a point in 2D space. Data type of the point coordinates is specified + as a template parameter. There are a few shorter aliases available for user convenience. + See cv::Point, cv::Point2i, cv::Point2f and cv::Point2d. + */ + template class Point_ + { + public: + typedef _Tp value_type; + + // various constructors + Point_(); + Point_(_Tp _x, _Tp _y); + Point_(const Point_& pt); + Point_(const CvPoint& pt); + Point_(const CvPoint2D32f& pt); + Point_(const Size_<_Tp>& sz); + Point_(const Vec<_Tp, 2>& v); + + Point_& operator = (const Point_& pt); + //! conversion to another data type + template operator Point_<_Tp2>() const; + + //! conversion to the old-style C structures + operator CvPoint() const; + operator CvPoint2D32f() const; + operator Vec<_Tp, 2>() const; + + //! dot product + _Tp dot(const Point_& pt) const; + //! dot product computed in double-precision arithmetics + double ddot(const Point_& pt) const; + //! cross-product + double cross(const Point_& pt) const; + //! checks whether the point is inside the specified rectangle + bool inside(const Rect_<_Tp>& r) const; + + _Tp x, y; //< the point coordinates + }; + + /*! + template 3D point class. + + The class defines a point in 3D space. Data type of the point coordinates is specified + as a template parameter. + + \see cv::Point3i, cv::Point3f and cv::Point3d + */ + template class Point3_ + { + public: + typedef _Tp value_type; + + // various constructors + Point3_(); + Point3_(_Tp _x, _Tp _y, _Tp _z); + Point3_(const Point3_& pt); + explicit Point3_(const Point_<_Tp>& pt); + Point3_(const CvPoint3D32f& pt); + Point3_(const Vec<_Tp, 3>& v); + + Point3_& operator = (const Point3_& pt); + //! conversion to another data type + template operator Point3_<_Tp2>() const; + //! conversion to the old-style CvPoint... + operator CvPoint3D32f() const; + //! conversion to cv::Vec<> + operator Vec<_Tp, 3>() const; + + //! dot product + _Tp dot(const Point3_& pt) const; + //! dot product computed in double-precision arithmetics + double ddot(const Point3_& pt) const; + //! cross product of the 2 3D points + Point3_ cross(const Point3_& pt) const; + + _Tp x, y, z; //< the point coordinates + }; + + //////////////////////////////// Size_ //////////////////////////////// + + /*! + The 2D size class + + The class represents the size of a 2D rectangle, image size, matrix size etc. + Normally, cv::Size ~ cv::Size_ is used. + */ + template class Size_ + { + public: + typedef _Tp value_type; + + //! various constructors + Size_(); + Size_(_Tp _width, _Tp _height); + Size_(const Size_& sz); + Size_(const CvSize& sz); + Size_(const CvSize2D32f& sz); + Size_(const Point_<_Tp>& pt); + + Size_& operator = (const Size_& sz); + //! the area (width*height) + _Tp area() const; + + //! conversion of another data type. + template operator Size_<_Tp2>() const; + + //! conversion to the old-style OpenCV types + operator CvSize() const; + operator CvSize2D32f() const; + + _Tp width, height; // the width and the height + }; + + //////////////////////////////// Rect_ //////////////////////////////// + + /*! + The 2D up-right rectangle class + + The class represents a 2D rectangle with coordinates of the specified data type. + Normally, cv::Rect ~ cv::Rect_ is used. + */ + template class Rect_ + { + public: + typedef _Tp value_type; + + //! various constructors + Rect_(); + Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height); + Rect_(const Rect_& r); + Rect_(const CvRect& r); + Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz); + Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2); + + Rect_& operator = ( const Rect_& r ); + //! the top-left corner + Point_<_Tp> tl() const; + //! the bottom-right corner + Point_<_Tp> br() const; + + //! size (width, height) of the rectangle + Size_<_Tp> size() const; + //! area (width*height) of the rectangle + _Tp area() const; + + //! conversion to another data type + template operator Rect_<_Tp2>() const; + //! conversion to the old-style CvRect + operator CvRect() const; + + //! checks whether the rectangle contains the point + bool contains(const Point_<_Tp>& pt) const; + + _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle + }; + + + %template(GcvSize2i) Size_; + %template(GcvSize2d) Size_; + %template(GcvSize2f) Size_; + + %template(GcvRect) Rect_; + + %template(GcvPoint2i) Point_; + %template(GcvPoint2f) Point_; + %template(GcvPoint2d) Point_; + + %template(GcvPoint3i) Point3_; + %template(GcvPoint3f) Point3_; + %template(GcvPoint3d) Point3_; +} + +/* Additional STL types */ +namespace std { + %template(GcvPoint3fVector) vector; + %template(GcvPoint2fVector) vector; + + %template(GcvIntVector) vector; + %template(GcvFloatVector) vector; +}; diff --git a/opencv2/gcv_utils/gcv_utils.hpp b/opencv2/gcv_utils/gcv_utils.hpp deleted file mode 100644 index b7814d5..0000000 --- a/opencv2/gcv_utils/gcv_utils.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#include -#include - -using namespace std; - -cv::Point3f GetPoint3f(float x, float y, float z) { - return cv::Point3f(x, y, z); -} - -cv::Point2f GetPoint2f(float x, float y) { - return cv::Point2f(x, y); -} diff --git a/opencv2/gcv_utils/gcv_utils.swigcxx b/opencv2/gcv_utils/gcv_utils.swigcxx deleted file mode 100644 index abfbf7f..0000000 --- a/opencv2/gcv_utils/gcv_utils.swigcxx +++ /dev/null @@ -1,16 +0,0 @@ -%module gcv_utils -%include "std_vector.i" - -%{ -#include "gcv_utils.hpp" -%} - -%include "gcv_utils.hpp" - -namespace std { - %template(GcvPoint3fVector) vector; - %template(GcvPoint2fVector) vector; - - %template(GcvIntVector) vector; - %template(GcvFloatVector) vector; -};