diff --git a/AUTHORS b/AUTHORS index fcb511b..810f970 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,12 +7,12 @@ ChaiShushan # Forked version authors: Chih-Wei Chang MohamedHelala -Adrian Zankich <> -vanillahsu <> -Alexander Kirillov <> -Aditya Bhargava <> -Bradley Griffiths <> -Ashkan Kiani <> -Peter Bui <> -satojkovic <> -TEE JIA HEN <> +Adrian Zankich +vanillahsu +Alexander Kirillov +Aditya Bhargava +Bradley Griffiths +Ashkan Kiani +Peter Bui +satojkovic +TEE JIA HEN diff --git a/README.md b/README.md index c8ef54f..1018df8 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ 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. + [**DISCLAIMER**](https://github.com/lazywei/go-opencv#disclaimer) ## Install @@ -31,14 +33,42 @@ go get code.google.com/p/go-opencv/trunk/opencv cd ${GoOpenCVRoot}/trunk/samples && go run hellocv.go ``` -## TODOs +## [WIP] OpenCV2 -- [ ] Better documents -- [ ] Split the big package into sub-packages corresponding to the modules described in [OpenCV API Reference](http://docs.opencv.org/modules/core/doc/intro.html) -- [ ] Clean up the codes +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. + +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. ## Example +### OpenCV2's initCameraMatrix2D + +```go +package main + +import "github.com/lazywei/go-opencv/opencv2/gcv_utils" +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)) + + 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)) + + cameraMatrix := GcvInitCameraMatrix2D(objPts, imgPts) +} +``` + + ### Resizing ```go @@ -106,5 +136,5 @@ You can find more samples at: https://github.com/lazywei/go-opencv/tree/master/s ## Disclaimer -This is a fork of [chai's go-opencv](https://github.com/chai2010/opencv). At the time of the fork (Dec 9, 2013) the original project was inactive, and hence I decide to host a fork on Github so people can contribute to this project easily. However, now it seems to be active again starting from Aug 25, 2014. Efforts to merge the two projects are very welcome. +This is a fork of [chai's go-opencv](https://github.com/chai2010/opencv), which has only OpenCV1 support through CGO. At the time of the fork (Dec 9, 2013) the original project was inactive, and hence I decide to host a fork on Github so people can contribute to this project easily. However, now it seems to be active again starting from Aug 25, 2014. Efforts to merge the two projects are very welcome. diff --git a/opencv/imgproc.go b/opencv/imgproc.go index 90ebb5c..93b8070 100644 --- a/opencv/imgproc.go +++ b/opencv/imgproc.go @@ -1,4 +1,4 @@ -// Copyright 2013 jrweizhang AT gmail.com. All rights reserved. +// Copyright 2013 . All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. diff --git a/opencv2/gcv_calib3d/gcv_calib3d.cpp b/opencv2/gcv_calib3d/gcv_calib3d.cpp new file mode 100644 index 0000000..19fbafb --- /dev/null +++ b/opencv2/gcv_calib3d/gcv_calib3d.cpp @@ -0,0 +1,44 @@ +#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, + std::vector 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, + cv::Size2i(imgSize[0], imgSize[1]), + 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 new file mode 100644 index 0000000..b8f39df --- /dev/null +++ b/opencv2/gcv_calib3d/gcv_calib3d.go @@ -0,0 +1,5 @@ +package gcv_calib3d + +// #cgo CXXFLAGS: -std=c++11 +// #cgo darwin pkg-config: opencv +import "C" diff --git a/opencv2/gcv_calib3d/gcv_calib3d.hpp b/opencv2/gcv_calib3d/gcv_calib3d.hpp new file mode 100644 index 0000000..15d2295 --- /dev/null +++ b/opencv2/gcv_calib3d/gcv_calib3d.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, + std::vector imgSize, cv::Mat cameraMatrix); diff --git a/opencv2/gcv_calib3d/gcv_calib3d.swigcxx b/opencv2/gcv_calib3d/gcv_calib3d.swigcxx new file mode 100644 index 0000000..64edfa2 --- /dev/null +++ b/opencv2/gcv_calib3d/gcv_calib3d.swigcxx @@ -0,0 +1,7 @@ +%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 new file mode 100644 index 0000000..abd3e94 --- /dev/null +++ b/opencv2/gcv_calib3d/gcv_calib3d_test.go @@ -0,0 +1,56 @@ +package gcv_calib3d + +import "testing" + +import "github.com/lazywei/go-opencv/opencv2/gcv_utils" + +// [[[ 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_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)) + + 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)) + + 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)) + + 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)) + + imgSize := gcv_utils.NewGcvIntVector(int64(2)) + imgSize.Set(0, 1920) + imgSize.Set(1, 1080) + + camMat := GcvInitCameraMatrix2D(objPts, imgPts) + + GcvCalibrateCamera(objPts, imgPts, imgSize, camMat) +} diff --git a/opencv2/gcv_utils/gcv_utils.go b/opencv2/gcv_utils/gcv_utils.go new file mode 100644 index 0000000..45b5f12 --- /dev/null +++ b/opencv2/gcv_utils/gcv_utils.go @@ -0,0 +1,5 @@ +package gcv_utils + +// #cgo CXXFLAGS: -std=c++11 +// #cgo darwin pkg-config: opencv +import "C" diff --git a/opencv2/gcv_utils/gcv_utils.hpp b/opencv2/gcv_utils/gcv_utils.hpp new file mode 100644 index 0000000..b7814d5 --- /dev/null +++ b/opencv2/gcv_utils/gcv_utils.hpp @@ -0,0 +1,12 @@ +#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 new file mode 100644 index 0000000..abfbf7f --- /dev/null +++ b/opencv2/gcv_utils/gcv_utils.swigcxx @@ -0,0 +1,16 @@ +%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; +}; diff --git a/opencv2/opencv2.go b/opencv2/opencv2.go new file mode 100644 index 0000000..68e5860 --- /dev/null +++ b/opencv2/opencv2.go @@ -0,0 +1 @@ +package opencv2