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) +}