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.
This commit is contained in:
Chih-Wei Chang 2015-02-16 00:35:37 +08:00
parent b7a619ff90
commit c9330d1782
4 changed files with 157 additions and 0 deletions

View file

@ -0,0 +1,44 @@
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <vector>
#include "gcv_core.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);
std::cout << cameraMatrix.type() << std::endl;
return cameraMatrix;
}
double GcvCalibrateCamera(VecPoint3f objPts, VecPoint2f imgPts,
cv::Size 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, 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;
}

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,
cv::Size2i imgSize, cv::Mat cameraMatrix);

View file

@ -5,8 +5,11 @@
#include "opencv2/core/types_c.h" #include "opencv2/core/types_c.h"
#include "opencv2/core/version.hpp" #include "opencv2/core/version.hpp"
#include "opencv2/core/core.hpp" #include "opencv2/core/core.hpp"
#include "gcv_core.hpp"
%} %}
%include "gcv_core.hpp"
/* Classes defined in core.hpp */ /* Classes defined in core.hpp */
namespace cv { namespace cv {
@ -190,6 +193,79 @@ namespace cv {
%template(GcvPoint3i) Point3_<int>; %template(GcvPoint3i) Point3_<int>;
%template(GcvPoint3f_) Point3_<float>; %template(GcvPoint3f_) Point3_<float>;
%template(GcvPoint3d_) Point3_<double>; %template(GcvPoint3d_) Point3_<double>;
/* ----------------- 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<typename _Tp> explicit Mat(const vector<_Tp>& vec, bool copyData=false);
//! builds matrix from cv::Vec; the data is copied by default
template<typename _Tp, int n> explicit Mat(const Vec<_Tp, n>& vec, bool copyData=true);
//! builds matrix from cv::Matx; the data is copied by default
template<typename _Tp, int m, int n> explicit Mat(const Matx<_Tp, m, n>& mtx, bool copyData=true);
//! builds matrix from a 2D point
template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
//! builds matrix from a 3D point
template<typename _Tp> 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<typename _Tp> void push_back(const _Tp& elem);
template<typename _Tp> 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<typename _Tp> _Tp& at(cv::Point pt);
template<typename _Tp> const _Tp& at(cv::Point pt) const;
%template(gcvAtd) at<double>;
%template(gcvAtf) at<float>;
};
} }
/* Additional STL types */ /* Additional STL types */

View file

@ -20,3 +20,29 @@ func TestNewGcvSize2d(t *testing.T) {
size := NewGcvSize2d(3, 1) size := NewGcvSize2d(3, 1)
spew.Dump(size) 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)))
}