From a2e19543f74809c64397240b67054315c5f2811f Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Sun, 15 Feb 2015 16:11:09 +0800 Subject: [PATCH 1/5] 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; -}; From b7a619ff90c9fbf282ede69303aabb8988d34e71 Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Sun, 15 Feb 2015 21:46:32 +0800 Subject: [PATCH 2/5] Better wrapping and documentation. --- README.md | 31 +++++++-------- opencv2/gcv_calib3d/gcv_calib3d_test.go | 50 +++++++++---------------- opencv2/gcv_core/README.md | 27 +++++++++++++ opencv2/gcv_core/gcv_core.cpp | 0 opencv2/gcv_core/gcv_core.go | 24 ++++++++++++ opencv2/gcv_core/gcv_core.swigcxx | 12 +++--- opencv2/gcv_core/gcv_core_test.go | 22 +++++++++++ 7 files changed, 113 insertions(+), 53 deletions(-) create mode 100644 opencv2/gcv_core/README.md create mode 100644 opencv2/gcv_core/gcv_core.cpp create mode 100644 opencv2/gcv_core/gcv_core_test.go diff --git a/README.md b/README.md index 1018df8..abdee3c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ Install Go and OpenCV, you might want to install both of them via `apt-get` or ` ``` go get github.com/lazywei/go-opencv -cd ${GoOpenCVRoot}/samples && go run hellocv.go +cd $GOPATH/src/github.com/lazywei/go-opencv/samples +go run hellocv.go ``` ### Windows @@ -29,7 +30,7 @@ cd ${GoOpenCVRoot}/samples && go run hellocv.go # include\opencv --> ${MinGWRoot}\include\opencv # include\opencv2 --> ${MinGWRoot}\include\opencv2 -go get code.google.com/p/go-opencv/trunk/opencv +go get github.com/lazywei/go-opencv cd ${GoOpenCVRoot}/trunk/samples && go run hellocv.go ``` @@ -39,7 +40,7 @@ After OpenCV 2.x+, the core team no longer develop and maintain C API. Therefore If you want to use CV2's API, please refer to the code under `opencv2/` directory. There has no too much document for CV2 wrapper yet, but you can still find the usage example in `*_test.go`. -Note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv::Point3f`) hasn't been wrapped fully yet. For now, we have some specific wrappers. We will try to wrapped those data structures fully as soon as possible. +Note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv::Point3f`) are wrapped partially for now. For more detail on how to use these types, please refer to [GcvCore's README](opencv2/gcv_core/README.md). ## Example @@ -48,23 +49,23 @@ Note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv::Point3f`) h ```go package main -import "github.com/lazywei/go-opencv/opencv2/gcv_utils" +import "github.com/lazywei/go-opencv/opencv2/gcv_core" import "github.com/lazywei/go-opencv/opencv2/gcv_calib3d" func main() { - 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(0, 25, 0)) + objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) + objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) + objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 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(1136.4140625, 1041.89208984)) + imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) + imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) + imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) - cameraMatrix := GcvInitCameraMatrix2D(objPts, imgPts) + GcvInitCameraMatrix2D(objPts, imgPts) } ``` diff --git a/opencv2/gcv_calib3d/gcv_calib3d_test.go b/opencv2/gcv_calib3d/gcv_calib3d_test.go index 55e01ae..0e9eccd 100644 --- a/opencv2/gcv_calib3d/gcv_calib3d_test.go +++ b/opencv2/gcv_calib3d/gcv_calib3d_test.go @@ -19,48 +19,34 @@ import "github.com/lazywei/go-opencv/opencv2/gcv_core" func TestGcvInitCameraMatrix2D(t *testing.T) { 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))) + objPts.Set(0, gcv_core.NewGcvPoint3f(0, 25, 0)) + objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) + objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) + objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 0)) 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))) + imgPts.Set(0, gcv_core.NewGcvPoint2f(1136.4140625, 1041.89208984)) + imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) + imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) + imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) GcvInitCameraMatrix2D(objPts, imgPts) } func TestGcvCalibrateCamera(t *testing.T) { 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))) + objPts.Set(0, gcv_core.NewGcvPoint3f(0, 25, 0)) + objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) + objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) + objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 0)) 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))) + imgPts.Set(0, gcv_core.NewGcvPoint2f(1136.4140625, 1041.89208984)) + imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) + imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) + imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) + + GcvInitCameraMatrix2D(objPts, imgPts) imgSize := gcv_core.NewGcvSize2i(1920, 1080) diff --git a/opencv2/gcv_core/README.md b/opencv2/gcv_core/README.md new file mode 100644 index 0000000..0ce0500 --- /dev/null +++ b/opencv2/gcv_core/README.md @@ -0,0 +1,27 @@ +Gcv Core +======== + +Wrap the core types in OpenCV. + +## Supporting Types and Examples + +| OpenCV C++ | Go OpenCV | Constructor | +|---------------|---------------|-------------------------------| +| `cv::Point2i` | `GcvPoint2i` | `NewGcvPoint2i(x, y int)` | +| `cv::Point2f` | `GcvPoint2f_` | `NewGcvPoint2f(x, y float32)` | +| `cv::Point2d` | `GcvPoint2d_` | `NewGcvPoint2d(x, y float64)` | +| `cv::Point3i` | `GcvPoint3i` | `NewGcvPoint3i(x, y, z int)` | +| `cv::Point3f` | `GcvPoint3f_` | `NewGcvPoint3f(x, y, z float32)` | +| `cv::Point3d` | `GcvPoint3d_` | `NewGcvPoint3d(x, y, z float64)` | +| `cv::Size2i` | `GcvSize2i` | `NewGcvSize2i(x, y int)` | +| `cv::Size2f` | `GcvSize2f_` | `NewGcvSize2f(x, y float32)` | +| `cv::Size2d` | `GcvSize2d_` | `NewGcvSize2d(x, y float64)` | + +---------- + +### Note for Renamed Types + +Some of the types are renamed to `*_`. The reason is that we'd like to wrap a better interface for them. +For example, the original `NewPoint2f` takes strictly two `float32`, and we are not able to pass `float64` or `int`, which doesn't make too much sense. +After wrapping an extra level, we are now able to pass `int`, `float32`, and `float64` to these methods. +Also note that **renaming doesn't affect any usage**, except you are manipulating the types yourself. diff --git a/opencv2/gcv_core/gcv_core.cpp b/opencv2/gcv_core/gcv_core.cpp new file mode 100644 index 0000000..e69de29 diff --git a/opencv2/gcv_core/gcv_core.go b/opencv2/gcv_core/gcv_core.go index 489b765..e2c49c7 100644 --- a/opencv2/gcv_core/gcv_core.go +++ b/opencv2/gcv_core/gcv_core.go @@ -3,3 +3,27 @@ package gcv_core // #cgo CXXFLAGS: -std=c++11 // #cgo darwin pkg-config: opencv import "C" + +func NewGcvPoint3f(x, y, z float32) GcvPoint3f_ { + return NewGcvPoint3f_(float32(x), float32(y), float32(z)) +} + +func NewGcvPoint3d(x, y, z float64) GcvPoint3d_ { + return NewGcvPoint3d_(float64(x), float64(y), float64(z)) +} + +func NewGcvPoint2f(x, y float32) GcvPoint2f_ { + return NewGcvPoint2f_(float32(x), float32(y)) +} + +func NewGcvPoint2d(x, y float64) GcvPoint2d_ { + return NewGcvPoint2d_(float64(x), float64(y)) +} + +func NewGcvSize2f(x, y float32) GcvSize2f_ { + return NewGcvSize2f_(float32(x), float32(y)) +} + +func NewGcvSize2d(x, y float64) GcvSize2d_ { + return NewGcvSize2d_(float64(x), float64(y)) +} diff --git a/opencv2/gcv_core/gcv_core.swigcxx b/opencv2/gcv_core/gcv_core.swigcxx index 72ebb24..6fdc6d9 100644 --- a/opencv2/gcv_core/gcv_core.swigcxx +++ b/opencv2/gcv_core/gcv_core.swigcxx @@ -178,18 +178,18 @@ namespace cv { %template(GcvSize2i) Size_; - %template(GcvSize2d) Size_; - %template(GcvSize2f) Size_; + %template(GcvSize2d_) Size_; + %template(GcvSize2f_) Size_; %template(GcvRect) Rect_; %template(GcvPoint2i) Point_; - %template(GcvPoint2f) Point_; - %template(GcvPoint2d) Point_; + %template(GcvPoint2f_) Point_; + %template(GcvPoint2d_) Point_; %template(GcvPoint3i) Point3_; - %template(GcvPoint3f) Point3_; - %template(GcvPoint3d) Point3_; + %template(GcvPoint3f_) Point3_; + %template(GcvPoint3d_) Point3_; } /* Additional STL types */ diff --git a/opencv2/gcv_core/gcv_core_test.go b/opencv2/gcv_core/gcv_core_test.go new file mode 100644 index 0000000..c74133a --- /dev/null +++ b/opencv2/gcv_core/gcv_core_test.go @@ -0,0 +1,22 @@ +package gcv_core + +import ( + "testing" + + "github.com/davecgh/go-spew/spew" +) + +func TestNewGcvPoint3f(t *testing.T) { + pt := NewGcvPoint3f(3, 1, 2) + spew.Dump(pt) +} + +func TestNewGcvPoint2f(t *testing.T) { + pt := NewGcvPoint2f(3, 1) + spew.Dump(pt) +} + +func TestNewGcvSize2d(t *testing.T) { + size := NewGcvSize2d(3, 1) + spew.Dump(size) +} From c9330d178252b374ccde2ce432641c4d6882b830 Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Mon, 16 Feb 2015 00:35:37 +0800 Subject: [PATCH 3/5] 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))) +} From 1f479722a326cd089e7091af63ff9e11e7d5370f Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Mon, 16 Feb 2015 12:44:44 +0800 Subject: [PATCH 4/5] Move all code to `gocv` package. --- README.md | 8 +-- {opencv2/gcv_core => gocv}/README.md | 4 +- .../gcv_core/gcv_core.cpp => gocv/gocv.cpp | 2 +- opencv2/gcv_core/gcv_core.go => gocv/gocv.go | 2 +- .../gcv_calib3d.hpp => gocv/gocv.hpp | 0 .../gcv_core.swigcxx => gocv/gocv.swigcxx | 6 +- .../gcv_core_test.go => gocv/gocv_test.go | 22 +++++++- opencv2/gcv_calib3d/gcv_calib3d.cpp | 43 -------------- opencv2/gcv_calib3d/gcv_calib3d.go | 5 -- opencv2/gcv_calib3d/gcv_calib3d.swigcxx | 7 --- opencv2/gcv_calib3d/gcv_calib3d_test.go | 56 ------------------- opencv2/gcv_core/gcv_core.hpp | 11 ---- opencv2/opencv2.go | 1 - 13 files changed, 32 insertions(+), 135 deletions(-) rename {opencv2/gcv_core => gocv}/README.md (96%) rename opencv2/gcv_core/gcv_core.cpp => gocv/gocv.cpp (98%) rename opencv2/gcv_core/gcv_core.go => gocv/gocv.go (97%) rename opencv2/gcv_calib3d/gcv_calib3d.hpp => gocv/gocv.hpp (100%) rename opencv2/gcv_core/gcv_core.swigcxx => gocv/gocv.swigcxx (99%) rename opencv2/gcv_core/gcv_core_test.go => gocv/gocv_test.go (63%) delete mode 100644 opencv2/gcv_calib3d/gcv_calib3d.cpp delete mode 100644 opencv2/gcv_calib3d/gcv_calib3d.go delete mode 100644 opencv2/gcv_calib3d/gcv_calib3d.swigcxx delete mode 100644 opencv2/gcv_calib3d/gcv_calib3d_test.go delete mode 100644 opencv2/gcv_core/gcv_core.hpp delete mode 100644 opencv2/opencv2.go diff --git a/README.md b/README.md index abdee3c..77df34a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Go OpenCV binding A Golang binding for [OpenCV](http://opencv.org/). -OpenCV 1.x C API bindings through CGO, and OpenCV 2+ C++ API through SWIG. +OpenCV 1.x C API bindings through CGO, and OpenCV 2+ C++ API ([`gocv/`](gocv/)) through SWIG. [**DISCLAIMER**](https://github.com/lazywei/go-opencv#disclaimer) @@ -36,11 +36,11 @@ cd ${GoOpenCVRoot}/trunk/samples && go run hellocv.go ## [WIP] OpenCV2 -After OpenCV 2.x+, the core team no longer develop and maintain C API. Therefore, CGO will not be used in CV2 binding. Instead, we are using SWIG for wrapping. The support for OpenCV2 is currently under development. +After OpenCV 2.x+, the core team no longer develop and maintain C API. Therefore, CGO will not be used in CV2 binding. Instead, we are using SWIG for wrapping. The support for OpenCV2 is currently under development, and whole code will be placed under `gocv` package. -If you want to use CV2's API, please refer to the code under `opencv2/` directory. There has no too much document for CV2 wrapper yet, but you can still find the usage example in `*_test.go`. +If you want to use CV2's API, please refer to the code under `gocv/` directory. There is no too many documents for CV2 wrapper yet, but you can still find the example usages in `*_test.go`. -Note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv::Point3f`) are wrapped partially for now. For more detail on how to use these types, please refer to [GcvCore's README](opencv2/gcv_core/README.md). +Please also note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv::Point3f`) are wrapped partially for now. For more detail on how to use these types, please refer to [GoCV's README](gocv/README.md). ## Example diff --git a/opencv2/gcv_core/README.md b/gocv/README.md similarity index 96% rename from opencv2/gcv_core/README.md rename to gocv/README.md index 0ce0500..898e4d5 100644 --- a/opencv2/gcv_core/README.md +++ b/gocv/README.md @@ -1,5 +1,5 @@ -Gcv Core -======== +Go OpenCV (GOlang openCV) +======================= Wrap the core types in OpenCV. diff --git a/opencv2/gcv_core/gcv_core.cpp b/gocv/gocv.cpp similarity index 98% rename from opencv2/gcv_core/gcv_core.cpp rename to gocv/gocv.cpp index 03cc031..3458147 100644 --- a/opencv2/gcv_core/gcv_core.cpp +++ b/gocv/gocv.cpp @@ -3,7 +3,7 @@ #include #include -#include "gcv_core.hpp" +#include "gocv.hpp" cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) { cv::Mat cameraMatrix; diff --git a/opencv2/gcv_core/gcv_core.go b/gocv/gocv.go similarity index 97% rename from opencv2/gcv_core/gcv_core.go rename to gocv/gocv.go index e2c49c7..4f2e5f9 100644 --- a/opencv2/gcv_core/gcv_core.go +++ b/gocv/gocv.go @@ -1,4 +1,4 @@ -package gcv_core +package gocv // #cgo CXXFLAGS: -std=c++11 // #cgo darwin pkg-config: opencv diff --git a/opencv2/gcv_calib3d/gcv_calib3d.hpp b/gocv/gocv.hpp similarity index 100% rename from opencv2/gcv_calib3d/gcv_calib3d.hpp rename to gocv/gocv.hpp diff --git a/opencv2/gcv_core/gcv_core.swigcxx b/gocv/gocv.swigcxx similarity index 99% rename from opencv2/gcv_core/gcv_core.swigcxx rename to gocv/gocv.swigcxx index dd7ea96..be86413 100644 --- a/opencv2/gcv_core/gcv_core.swigcxx +++ b/gocv/gocv.swigcxx @@ -1,14 +1,14 @@ -%module gcv_core +%module gocv %include "std_vector.i" %{ #include "opencv2/core/types_c.h" #include "opencv2/core/version.hpp" #include "opencv2/core/core.hpp" -#include "gcv_core.hpp" +#include "gocv.hpp" %} -%include "gcv_core.hpp" +%include "gocv.hpp" /* Classes defined in core.hpp */ namespace cv { diff --git a/opencv2/gcv_core/gcv_core_test.go b/gocv/gocv_test.go similarity index 63% rename from opencv2/gcv_core/gcv_core_test.go rename to gocv/gocv_test.go index c2dfa4f..4a2771c 100644 --- a/opencv2/gcv_core/gcv_core_test.go +++ b/gocv/gocv_test.go @@ -1,4 +1,4 @@ -package gcv_core +package gocv import ( "testing" @@ -46,3 +46,23 @@ func TestGcvInitCameraMatrix2D(t *testing.T) { spew.Dump(camMat.GcvAtd(NewGcvSize2i(1, 2))) spew.Dump(camMat.GcvAtd(NewGcvSize2i(2, 2))) } + +func TestGcvCalibrateCamera(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)) + + imgSize := NewGcvSize2i(1920, 1080) + + camMat := GcvInitCameraMatrix2D(objPts, imgPts) + + GcvCalibrateCamera(objPts, imgPts, imgSize, camMat) +} diff --git a/opencv2/gcv_calib3d/gcv_calib3d.cpp b/opencv2/gcv_calib3d/gcv_calib3d.cpp deleted file mode 100644 index 1b3f997..0000000 --- a/opencv2/gcv_calib3d/gcv_calib3d.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include -#include -#include - -#include "gcv_calib3d.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); - 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_calib3d/gcv_calib3d.go b/opencv2/gcv_calib3d/gcv_calib3d.go deleted file mode 100644 index b8f39df..0000000 --- a/opencv2/gcv_calib3d/gcv_calib3d.go +++ /dev/null @@ -1,5 +0,0 @@ -package gcv_calib3d - -// #cgo CXXFLAGS: -std=c++11 -// #cgo darwin pkg-config: opencv -import "C" diff --git a/opencv2/gcv_calib3d/gcv_calib3d.swigcxx b/opencv2/gcv_calib3d/gcv_calib3d.swigcxx deleted file mode 100644 index 64edfa2..0000000 --- a/opencv2/gcv_calib3d/gcv_calib3d.swigcxx +++ /dev/null @@ -1,7 +0,0 @@ -%module gcv_calib3d - -%{ -#include "gcv_calib3d.hpp" -%} - -%include "gcv_calib3d.hpp" diff --git a/opencv2/gcv_calib3d/gcv_calib3d_test.go b/opencv2/gcv_calib3d/gcv_calib3d_test.go deleted file mode 100644 index 0e9eccd..0000000 --- a/opencv2/gcv_calib3d/gcv_calib3d_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package gcv_calib3d - -import "testing" - -import "github.com/lazywei/go-opencv/opencv2/gcv_core" - -// [[[ 0. 25. 0.] -// [ 0. -25. 0.] -// [-47. 25. 0.] -// [-47. -25. 0.]]] -// [[[ 1136.4140625 1041.89208984] -// [ 1845.33190918 671.39581299] -// [ 302.73373413 634.79998779] -// [ 1051.46154785 352.76107788]]] -// (1920, 1080) -// [[ 4.82812906e+03 0.00000000e+00 9.59500000e+02] -// [ 0.00000000e+00 4.82812906e+03 5.39500000e+02] -// [ 0.00000000e+00 0.00000000e+00 1.00000000e+00]] - -func TestGcvInitCameraMatrix2D(t *testing.T) { - objPts := gcv_core.NewGcvPoint3fVector(int64(4)) - objPts.Set(0, gcv_core.NewGcvPoint3f(0, 25, 0)) - objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) - objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) - objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 0)) - - imgPts := gcv_core.NewGcvPoint2fVector(int64(4)) - imgPts.Set(0, gcv_core.NewGcvPoint2f(1136.4140625, 1041.89208984)) - imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) - imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) - imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) - - GcvInitCameraMatrix2D(objPts, imgPts) -} - -func TestGcvCalibrateCamera(t *testing.T) { - objPts := gcv_core.NewGcvPoint3fVector(int64(4)) - objPts.Set(0, gcv_core.NewGcvPoint3f(0, 25, 0)) - objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) - objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) - objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 0)) - - imgPts := gcv_core.NewGcvPoint2fVector(int64(4)) - imgPts.Set(0, gcv_core.NewGcvPoint2f(1136.4140625, 1041.89208984)) - imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) - imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) - imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) - - GcvInitCameraMatrix2D(objPts, imgPts) - - imgSize := gcv_core.NewGcvSize2i(1920, 1080) - - camMat := GcvInitCameraMatrix2D(objPts, imgPts) - - GcvCalibrateCamera(objPts, imgPts, imgSize, camMat) -} diff --git a/opencv2/gcv_core/gcv_core.hpp b/opencv2/gcv_core/gcv_core.hpp deleted file mode 100644 index 3710c3c..0000000 --- a/opencv2/gcv_core/gcv_core.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#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/opencv2.go b/opencv2/opencv2.go deleted file mode 100644 index 68e5860..0000000 --- a/opencv2/opencv2.go +++ /dev/null @@ -1 +0,0 @@ -package opencv2 From 5b4f28054d6f3298a05866da980fd8b1d0bb3023 Mon Sep 17 00:00:00 2001 From: Chih-Wei Chang Date: Mon, 16 Feb 2015 12:48:16 +0800 Subject: [PATCH 5/5] Update README --- README.md | 29 ++++++++++++++--------------- gocv/gocv_test.go | 1 + 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 77df34a..cec9831 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Go OpenCV binding A Golang binding for [OpenCV](http://opencv.org/). -OpenCV 1.x C API bindings through CGO, and OpenCV 2+ C++ API ([`gocv/`](gocv/)) through SWIG. +OpenCV 1.x C API bindings through CGO, and OpenCV 2+ C++ API ([`GoCV`](gocv/)) through SWIG. [**DISCLAIMER**](https://github.com/lazywei/go-opencv#disclaimer) @@ -34,7 +34,7 @@ go get github.com/lazywei/go-opencv cd ${GoOpenCVRoot}/trunk/samples && go run hellocv.go ``` -## [WIP] OpenCV2 +## [WIP] OpenCV2 (GoCV) After OpenCV 2.x+, the core team no longer develop and maintain C API. Therefore, CGO will not be used in CV2 binding. Instead, we are using SWIG for wrapping. The support for OpenCV2 is currently under development, and whole code will be placed under `gocv` package. @@ -49,23 +49,22 @@ Please also note that the basic data structures in OpenCV (e.g., `cv::Mat`, `cv: ```go package main -import "github.com/lazywei/go-opencv/opencv2/gcv_core" -import "github.com/lazywei/go-opencv/opencv2/gcv_calib3d" +import "github.com/lazywei/go-opencv/gocv" func main() { - objPts := gcv_core.NewGcvPoint3fVector(int64(4)) - objPts.Set(0, gcv_core.NewGcvPoint3f(0, 25, 0)) - objPts.Set(1, gcv_core.NewGcvPoint3f(0, -25, 0)) - objPts.Set(2, gcv_core.NewGcvPoint3f(-47, 25, 0)) - objPts.Set(3, gcv_core.NewGcvPoint3f(-47, -25, 0)) + objPts := gocv.NewGcvPoint3fVector(int64(4)) + objPts.Set(0, gocv.NewGcvPoint3f(0, 25, 0)) + objPts.Set(1, gocv.NewGcvPoint3f(0, -25, 0)) + objPts.Set(2, gocv.NewGcvPoint3f(-47, 25, 0)) + objPts.Set(3, gocv.NewGcvPoint3f(-47, -25, 0)) - imgPts := gcv_core.NewGcvPoint2fVector(int64(4)) - imgPts.Set(0, gcv_core.NewGcvPoint2f(1136.4140625, 1041.89208984)) - imgPts.Set(1, gcv_core.NewGcvPoint2f(1845.33190918, 671.39581299)) - imgPts.Set(2, gcv_core.NewGcvPoint2f(302.73373413, 634.79998779)) - imgPts.Set(3, gcv_core.NewGcvPoint2f(1051.46154785, 352.76107788)) + imgPts := gocv.NewGcvPoint2fVector(int64(4)) + imgPts.Set(0, gocv.NewGcvPoint2f(1136.4140625, 1041.89208984)) + imgPts.Set(1, gocv.NewGcvPoint2f(1845.33190918, 671.39581299)) + imgPts.Set(2, gocv.NewGcvPoint2f(302.73373413, 634.79998779)) + imgPts.Set(3, gocv.NewGcvPoint2f(1051.46154785, 352.76107788)) - GcvInitCameraMatrix2D(objPts, imgPts) + gocv.GcvInitCameraMatrix2D(objPts, imgPts) } ``` diff --git a/gocv/gocv_test.go b/gocv/gocv_test.go index 4a2771c..f1a52e8 100644 --- a/gocv/gocv_test.go +++ b/gocv/gocv_test.go @@ -26,6 +26,7 @@ func TestMat(t *testing.T) { mat2 := NewMat(mat) spew.Dump(mat2) } + func TestGcvInitCameraMatrix2D(t *testing.T) { objPts := NewGcvPoint3fVector(int64(4)) objPts.Set(0, NewGcvPoint3f(0, 25, 0))