almost finished Python wrappers

This commit is contained in:
Vadim Pisarevsky
2010-11-02 17:58:22 +00:00
parent eda72a3e8b
commit 5b6a755719
27 changed files with 1292 additions and 634 deletions

View File

@@ -972,7 +972,7 @@ public:
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 1, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -984,7 +984,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -996,7 +996,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1008,7 +1008,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1020,7 +1020,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1032,7 +1032,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1044,7 +1044,7 @@ public:
typedef int work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1056,7 +1056,7 @@ public:
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1068,7 +1068,7 @@ public:
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1080,7 +1080,7 @@ public:
typedef value_type work_type;
typedef value_type channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = 1,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 1,
fmt=DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1092,7 +1092,7 @@ public:
typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
typedef _Tp channel_type;
typedef value_type vec_type;
enum { depth = DataDepth<channel_type>::value, channels = cn,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = cn,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
};
@@ -1103,7 +1103,7 @@ public:
typedef std::complex<_Tp> value_type;
typedef value_type work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 2,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1115,7 +1115,7 @@ public:
typedef Complex<_Tp> value_type;
typedef value_type work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 2,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1127,7 +1127,7 @@ public:
typedef Point_<_Tp> value_type;
typedef Point_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 2,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1139,7 +1139,7 @@ public:
typedef Point3_<_Tp> value_type;
typedef Point3_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 3,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 3,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1151,7 +1151,7 @@ public:
typedef Size_<_Tp> value_type;
typedef Size_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 2,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1163,7 +1163,7 @@ public:
typedef Rect_<_Tp> value_type;
typedef Rect_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 4,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 4,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1175,7 +1175,7 @@ public:
typedef Scalar_<_Tp> value_type;
typedef Scalar_<typename DataType<_Tp>::work_type> work_type;
typedef _Tp channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 4,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 4,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -1187,7 +1187,7 @@ public:
typedef Range value_type;
typedef value_type work_type;
typedef int channel_type;
enum { depth = DataDepth<channel_type>::value, channels = 2,
enum { generic = 0, depth = DataDepth<channel_type>::value, channels = 2,
fmt = ((channels-1)<<8) + DataDepth<channel_type>::fmt,
type = CV_MAKETYPE(depth, channels) };
typedef Vec<channel_type, channels> vec_type;
@@ -2154,27 +2154,27 @@ CV_EXPORTS_W void calcCovarMatrix( const Mat& samples, CV_OUT Mat& covar, CV_OUT
}
\endcode
*/
class CV_EXPORTS_W PCA
class CV_EXPORTS PCA
{
public:
//! default constructor
CV_WRAP PCA();
PCA();
//! the constructor that performs PCA
CV_WRAP PCA(const Mat& data, const Mat& mean, int flags, int maxComponents=0);
PCA(const Mat& data, const Mat& mean, int flags, int maxComponents=0);
//! operator that performs PCA. The previously stored data, if any, is released
CV_WRAP_AS(compute) PCA& operator()(const Mat& data, const Mat& mean, int flags, int maxComponents=0);
PCA& operator()(const Mat& data, const Mat& mean, int flags, int maxComponents=0);
//! projects vector from the original space to the principal components subspace
Mat project(const Mat& vec) const;
//! projects vector from the original space to the principal components subspace
CV_WRAP void project(const Mat& vec, CV_OUT Mat& result) const;
void project(const Mat& vec, CV_OUT Mat& result) const;
//! reconstructs the original vector from the projection
Mat backProject(const Mat& vec) const;
//! reconstructs the original vector from the projection
CV_WRAP void backProject(const Mat& vec, CV_OUT Mat& result) const;
void backProject(const Mat& vec, CV_OUT Mat& result) const;
CV_PROP Mat eigenvectors; //!< eigenvectors of the covariation matrix
CV_PROP Mat eigenvalues; //!< eigenvalues of the covariation matrix
CV_PROP Mat mean; //!< mean value subtracted before the projection and added after the back projection
Mat eigenvectors; //!< eigenvectors of the covariation matrix
Mat eigenvalues; //!< eigenvalues of the covariation matrix
Mat mean; //!< mean value subtracted before the projection and added after the back projection
};
/*!
@@ -2273,7 +2273,7 @@ CV_WRAP static inline void randn(CV_OUT Mat& dst, const Scalar& mean, const Scal
{ theRNG().fill(dst, RNG::NORMAL, mean, stddev); }
//! shuffles the input array elements
CV_EXPORTS_W void randShuffle(Mat& dst, double iterFactor=1., RNG* rng=0);
CV_EXPORTS void randShuffle(Mat& dst, double iterFactor=1., RNG* rng=0);
//! draws the line segment (pt1, pt2) in the image
CV_EXPORTS_W void line(Mat& img, Point pt1, Point pt2, const Scalar& color,
@@ -3576,30 +3576,46 @@ public:
CV_WRAP KDTree();
//! the full constructor that builds the search tree
CV_WRAP KDTree(const Mat& _points, bool copyAndReorderPoints=false);
//! the full constructor that builds the search tree
CV_WRAP KDTree(const Mat& _points, const Mat& _labels, bool copyAndReorderPoints=false);
//! builds the search tree
CV_WRAP void build(const Mat& _points, bool copyAndReorderPoints=false);
//! builds the search tree
CV_WRAP void build(const Mat& _points, const Mat& _labels, bool copyAndReorderPoints=false);
//! finds the K nearest neighbors of "vec" while looking at Emax (at most) leaves
int findNearest(const float* vec,
int K, int Emax, int* neighborsIdx,
Mat* neighbors=0, float* dist=0) const;
Mat* neighbors=0, float* dist=0, int* labels=0) const;
//! finds the K nearest neighbors while looking at Emax (at most) leaves
int findNearest(const float* vec, int K, int Emax,
vector<int>* neighborsIdx,
Mat* neighbors=0, vector<float>* dist=0) const;
Mat* neighbors=0,
vector<float>* dist=0,
vector<int>* labels=0) const;
CV_WRAP int findNearest(const vector<float>& vec, int K, int Emax,
CV_OUT vector<int>* neighborsIdx,
CV_OUT Mat* neighbors=0,
CV_OUT vector<float>* dist=0,
CV_OUT vector<int>* labels=0) const;
//! finds all the points from the initial set that belong to the specified box
void findOrthoRange(const float* minBounds, const float* maxBounds,
vector<int>* neighborsIdx, Mat* neighbors=0) const;
vector<int>* neighborsIdx, Mat* neighbors=0,
vector<int>* labels=0) const;
CV_WRAP void findOrthoRange(const vector<float>& minBounds, const vector<float>& maxBounds,
CV_OUT vector<int>* neighborsIdx, CV_OUT Mat* neighbors=0,
CV_OUT vector<int>* labels=0) const;
//! returns vectors with the specified indices
void getPoints(const int* idx, size_t nidx, Mat& pts) const;
void getPoints(const int* idx, size_t nidx, Mat& pts, vector<int>* labels=0) const;
//! returns vectors with the specified indices
void getPoints(const Mat& idxs, Mat& pts) const;
CV_WRAP void getPoints(const vector<int>& idxs, Mat& pts, CV_OUT vector<int>* labels=0) const;
//! return a vector with the specified index
const float* getPoint(int ptidx) const;
const float* getPoint(int ptidx, int* label=0) const;
//! returns the search space dimensionality
CV_WRAP int dims() const;
vector<Node> nodes; //!< all the tree nodes
CV_PROP Mat points; //!< all the points. It can be a reordered copy of the input vector set or the original vector set.
CV_PROP vector<int> labels; //!< the parallel array of labels.
CV_PROP int maxDepth; //!< maximum depth of the search tree. Do not modify it
CV_PROP_RW int normType; //!< type of the distance (cv::NORM_L1 or cv::NORM_L2) used for search. Initially set to cv::NORM_L2, but you can modify it
};
@@ -3743,7 +3759,7 @@ public:
//! returns the top-level mapping. YAML supports multiple streams
CV_WRAP FileNode root(int streamidx=0) const;
//! returns the specified element of the top-level mapping
CV_WRAP FileNode operator[](const string& nodename) const;
FileNode operator[](const string& nodename) const;
//! returns the specified element of the top-level mapping
CV_WRAP FileNode operator[](const char* nodename) const;
@@ -3777,7 +3793,7 @@ class CV_EXPORTS FileNodeIterator;
Note that file nodes are only used for navigating file storages opened for reading.
When a file storage is opened for writing, no data is stored in memory after it is written.
*/
class CV_EXPORTS_W FileNode
class CV_EXPORTS_W_SIMPLE FileNode
{
public:
//! type of the file storage node
@@ -3813,7 +3829,6 @@ public:
//! returns type of the node
CV_WRAP int type() const;
CV_WRAP int rawDataSize(const string& fmt) const;
//! returns true if the node is empty
CV_WRAP bool empty() const;
//! returns true if the node is a "none" object
@@ -3835,13 +3850,13 @@ public:
//! returns the number of elements in the node, if it is a sequence or mapping, or 1 otherwise.
CV_WRAP size_t size() const;
//! returns the node content as an integer. If the node stores floating-point number, it is rounded.
CV_WRAP operator int() const;
operator int() const;
//! returns the node content as float
CV_WRAP operator float() const;
operator float() const;
//! returns the node content as double
CV_WRAP operator double() const;
operator double() const;
//! returns the node content as text string
CV_WRAP operator string() const;
operator string() const;
//! returns pointer to the underlying file node
CvFileNode* operator *();

View File

@@ -2990,8 +2990,7 @@ template<typename _Tp, class _LT> void sort( vector<_Tp>& vec, _LT LT=_LT() )
{
_Tp *lb;
_Tp *ub;
}
stack[48];
} stack[48];
size_t total = vec.size();

View File

@@ -161,8 +161,9 @@ typedef signed char schar;
#define CV_CARRAY(counter)
#define CV_CUSTOM_CARRAY(args)
#define CV_EXPORTS_W CV_EXPORTS
#define CV_EXPORTS_W_SIMPLE CV_EXPORTS
#define CV_EXPORTS_AS(synonym) CV_EXPORTS
#define CV_EXPORTS_AS_MAP CV_EXPORTS
#define CV_EXPORTS_W_MAP CV_EXPORTS
#define CV_IN_OUT
#define CV_OUT
#define CV_PROP

View File

@@ -3555,6 +3555,13 @@ KDTree::KDTree(const Mat& _points, bool _copyData)
build(_points, _copyData);
}
KDTree::KDTree(const Mat& _points, const Mat& _labels, bool _copyData)
{
maxDepth = -1;
normType = NORM_L2;
build(_points, _labels, _copyData);
}
struct SubTree
{
SubTree() : first(0), last(0), nodeIdx(0), depth(0) {}
@@ -3630,10 +3637,16 @@ computeSums( const Mat& points, const size_t* ofs, int a, int b, double* sums )
}
}
void KDTree::build(const Mat& _points, bool _copyData)
{
CV_Assert(_points.type() == CV_32F);
build(_points, Mat(), _copyData);
}
void KDTree::build(const Mat& _points, const Mat& _labels, bool _copyData)
{
CV_Assert(_points.type() == CV_32F && !_points.empty());
vector<KDTree::Node>().swap(nodes);
if( !_copyData )
@@ -3643,13 +3656,22 @@ void KDTree::build(const Mat& _points, bool _copyData)
points.release();
points.create(_points.size(), _points.type());
}
int i, j, n = _points.rows, dims = _points.cols, top = 0;
const float* data = _points.ptr<float>(0);
float* dstdata = points.ptr<float>(0);
size_t step = _points.step1();
size_t dstep = points.step1();
int ptpos = 0;
labels.resize(n);
const int* _labels_data = 0;
if( !_labels.empty() )
{
int nlabels = _labels.checkVector(1, CV_32S, true);
CV_Assert(nlabels == n);
_labels_data = (const int*)_labels.data;
}
Mat sumstack(MAX_TREE_DEPTH*2, dims*2, CV_64F);
SubTree stack[MAX_TREE_DEPTH*2];
@@ -3675,7 +3697,8 @@ void KDTree::build(const Mat& _points, bool _copyData)
if( count == 1 )
{
int idx = _copyData ? ptpos++ : (int)(ptofs[first]/step);
int idx0 = (int)(ptofs[first]/step);
int idx = _copyData ? ptpos++ : idx0;
nodes[nidx].idx = ~idx;
if( _copyData )
{
@@ -3684,6 +3707,7 @@ void KDTree::build(const Mat& _points, bool _copyData)
for( j = 0; j < dims; j++ )
dst[j] = src[j];
}
labels[idx] = _labels_data ? _labels_data[idx0] : idx0;
_maxDepth = std::max(_maxDepth, depth);
continue;
}
@@ -3723,7 +3747,8 @@ void KDTree::build(const Mat& _points, bool _copyData)
int KDTree::findNearest(const float* vec, int K, int emax,
vector<int>* neighborsIdx,
Mat* neighbors,
vector<float>* dist) const
vector<float>* dist,
vector<int>* labels) const
{
K = std::min(K, points.rows);
CV_Assert(K > 0);
@@ -3731,15 +3756,44 @@ int KDTree::findNearest(const float* vec, int K, int emax,
neighborsIdx->resize(K);
if(dist)
dist->resize(K);
if(labels)
labels->resize(K);
K = findNearest(vec, K, emax, neighborsIdx ? &(*neighborsIdx)[0] : 0,
neighbors, dist ? &(*dist)[0] : 0);
neighbors, dist ? &(*dist)[0] : 0, labels ? &(*labels)[0] : 0);
if(neighborsIdx)
neighborsIdx->resize(K);
if(dist)
dist->resize(K);
if(labels)
labels->resize(K);
return K;
}
int KDTree::findNearest(const vector<float>& vec, int K, int emax,
vector<int>* neighborsIdx,
Mat* neighbors,
vector<float>* dist,
vector<int>* labels) const
{
CV_Assert((int)vec.size() == points.cols);
K = std::min(K, points.rows);
CV_Assert(K > 0);
if(neighborsIdx)
neighborsIdx->resize(K);
if(dist)
dist->resize(K);
if(labels)
labels->resize(K);
K = findNearest(&vec[0], K, emax, neighborsIdx ? &(*neighborsIdx)[0] : 0,
neighbors, dist ? &(*dist)[0] : 0, labels ? &(*labels)[0] : 0);
if(neighborsIdx)
neighborsIdx->resize(K);
if(dist)
dist->resize(K);
if(labels)
labels->resize(K);
return K;
}
struct PQueueElem
{
@@ -3752,7 +3806,7 @@ struct PQueueElem
int KDTree::findNearest(const float* vec, int K, int emax,
int* _neighborsIdx, Mat* _neighbors,
float* _dist) const
float* _dist, int* _labels) const
{
K = std::min(K, points.rows);
@@ -3885,6 +3939,11 @@ int KDTree::findNearest(const float* vec, int K, int emax,
for( i = 0; i < K; i++ )
_dist[i] = std::sqrt(dist[i]);
}
if( _labels )
{
for( i = 0; i < K; i++ )
_labels[i] = labels[idx[i]];
}
if( _neighbors )
getPoints(idx, K, *_neighbors);
@@ -3893,7 +3952,8 @@ int KDTree::findNearest(const float* vec, int K, int emax,
void KDTree::findOrthoRange(const float* L, const float* R,
vector<int>* neighborsIdx, Mat* neighbors) const
vector<int>* neighborsIdx,
Mat* neighbors, vector<int>* _labels) const
{
int dims = points.cols;
@@ -3931,48 +3991,73 @@ void KDTree::findOrthoRange(const float* L, const float* R,
}
if( neighbors )
getPoints( &(*idx)[0], idx->size(), *neighbors );
getPoints( &(*idx)[0], idx->size(), *neighbors, _labels );
}
void KDTree::getPoints(const int* idx, size_t nidx, Mat& pts) const
void KDTree::findOrthoRange(const vector<float>& L, const vector<float>& R,
vector<int>* neighborsIdx, Mat* neighbors, vector<int>* _labels) const
{
size_t dims = points.cols;
CV_Assert(L.size() == dims && R.size() == dims);
findOrthoRange(&L[0], &R[0], neighborsIdx, neighbors, _labels);
}
void KDTree::getPoints(const int* idx, size_t nidx, Mat& pts, vector<int>* _labels) const
{
int dims = points.cols, n = (int)nidx;
pts.create( n, dims, points.type());
if(_labels)
_labels->resize(nidx);
for( int i = 0; i < n; i++ )
{
int k = idx[i];
CV_Assert( (unsigned)k < (unsigned)points.rows );
const float* src = points.ptr<float>(k);
std::copy(src, src + dims, pts.ptr<float>(i));
if(_labels)
(*_labels)[i] = labels[k];
}
}
void KDTree::getPoints(const Mat& idx, Mat& pts) const
void KDTree::getPoints(const vector<int>& idx, Mat& pts, vector<int>* _labels) const
{
CV_Assert(idx.type() == CV_32S && idx.isContinuous() &&
(idx.cols == 1 || idx.rows == 1));
int dims = points.cols;
int i, nidx = idx.cols + idx.rows - 1;
int i, nidx = (int)idx.size();
pts.create( nidx, dims, points.type());
const int* _idx = idx.ptr<int>();
if(_labels)
_labels->resize(nidx);
for( i = 0; i < nidx; i++ )
{
int k = _idx[i];
int k = idx[i];
CV_Assert( (unsigned)k < (unsigned)points.rows );
const float* src = points.ptr<float>(k);
std::copy(src, src + dims, pts.ptr<float>(i));
if(_labels) (*_labels)[i] = labels[k];
}
}
const float* KDTree::getPoint(int ptidx) const
const float* KDTree::getPoint(int ptidx, int* label) const
{
CV_Assert( (unsigned)ptidx < (unsigned)points.rows);
if(label)
*label = label[ptidx];
return points.ptr<float>(ptidx);
}
int KDTree::dims() const
{
return !points.empty() ? points.cols : 0;
}
////////////////////////////////////////////////////////////////////////////////
schar* seqPush( CvSeq* seq, const void* element )
{

View File

@@ -5130,7 +5130,6 @@ void* FileNode::readObj() const
return cvRead( (CvFileStorage*)fs, (CvFileNode*)node );
}
FileNodeIterator::FileNodeIterator()
{
fs = 0;