Merge pull request #44 from lazywei/cv2

Wrap OpenCV2's Calib3d module with SWIG
This commit is contained in:
Chih-Wei Chang 2015-02-15 14:20:40 +08:00
commit 119a8baae1
12 changed files with 202 additions and 15 deletions

18
AUTHORS
View file

@ -7,12 +7,12 @@ ChaiShushan <chaishushan@gmail.com>
# Forked version authors:
Chih-Wei Chang <me@cwchang.me>
MohamedHelala <mohamed.helala@gmail.com>
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

View file

@ -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.

View file

@ -1,4 +1,4 @@
// Copyright 2013 jrweizhang AT gmail.com. All rights reserved.
// Copyright 2013 <me@cwchang.me>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

View file

@ -0,0 +1,44 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <vector>
#include "gcv_calib3d.hpp"
cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts) {
cv::Mat cameraMatrix;
std::vector<VecPoint3f> objPtsArr;
std::vector<VecPoint2f> 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<int> imgSize, cv::Mat cameraMatrix) {
std::vector<VecPoint3f> objPtsArr;
std::vector<VecPoint2f> imgPtsArr;
std::vector<cv::Mat> 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;
}

View file

@ -0,0 +1,5 @@
package gcv_calib3d
// #cgo CXXFLAGS: -std=c++11
// #cgo darwin pkg-config: opencv
import "C"

View file

@ -0,0 +1,11 @@
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
typedef std::vector<cv::Point3f> VecPoint3f;
typedef std::vector<cv::Point2f> VecPoint2f;
cv::Mat GcvInitCameraMatrix2D(VecPoint3f objPts, VecPoint2f imgPts);
double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts,
std::vector<int> imgSize, cv::Mat cameraMatrix);

View file

@ -0,0 +1,7 @@
%module gcv_calib3d
%{
#include "gcv_calib3d.hpp"
%}
%include "gcv_calib3d.hpp"

View file

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

View file

@ -0,0 +1,5 @@
package gcv_utils
// #cgo CXXFLAGS: -std=c++11
// #cgo darwin pkg-config: opencv
import "C"

View file

@ -0,0 +1,12 @@
#include <opencv2/opencv.hpp>
#include <vector>
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);
}

View file

@ -0,0 +1,16 @@
%module gcv_utils
%include "std_vector.i"
%{
#include "gcv_utils.hpp"
%}
%include "gcv_utils.hpp"
namespace std {
%template(GcvPoint3fVector) vector<cv::Point3f>;
%template(GcvPoint2fVector) vector<cv::Point2f>;
%template(GcvIntVector) vector<int>;
%template(GcvFloatVector) vector<float>;
};

1
opencv2/opencv2.go Normal file
View file

@ -0,0 +1 @@
package opencv2