Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

this worked for me (with 2.4.9):

(one had to navigate around the BowTrainer constructor and alias the compute method)


class CV_EXPORTS_W BOWTrainer
{
public:
    BOWTrainer();
    virtual ~BOWTrainer();

    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;

    CV_WRAP virtual void clear();

    /*
     * Train visual words vocabulary, that is cluster training descriptors and
     * compute cluster centers.
     * Returns cluster centers.
     *
     * descriptors      Training descriptors computed on images keypoints.
     */
    CV_WRAP virtual Mat cluster() const = 0;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;

protected:
    vector<Mat> descriptors;
    int size;
};

/*
 * This is BOWTrainer using cv::kmeans to get vocabulary.
 */
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
{
public:
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
    virtual ~BOWKMeansTrainer();

    // Returns trained vocabulary (i.e. cluster centers).
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;

protected:

    int clusterCount;
    TermCriteria termcrit;
    int attempts;
    int flags;
};

/*
 * Class to compute image descriptor using bag of visual words.
 */
class CV_EXPORTS_W BOWImgDescriptorExtractor
{
public:
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
                               const Ptr<DescriptorMatcher>& dmatcher );
    virtual ~BOWImgDescriptorExtractor();

    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
                  vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
    // compute() is not constant because DescriptorMatcher::match is not constant

    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor )
    { compute(image,keypoints,imgDescriptor); }

    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;

protected:
    Mat vocabulary;
    Ptr<DescriptorExtractor> dextractor;
    Ptr<DescriptorMatcher> dmatcher;
};

this worked for me (with 2.4.9):

(one had to navigate around the BowTrainer constructor and alias the compute method)


class CV_EXPORTS_W BOWTrainer
{
public:
    BOWTrainer();
    virtual ~BOWTrainer();

    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;

    CV_WRAP virtual void clear();

    /*
     * Train visual words vocabulary, that is cluster training descriptors and
     * compute cluster centers.
     * Returns cluster centers.
     *
     * descriptors      Training descriptors computed on images keypoints.
     */
    CV_WRAP virtual Mat cluster() const = 0;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;

protected:
    vector<Mat> descriptors;
    int size;
};

/*
 * This is BOWTrainer using cv::kmeans to get vocabulary.
 */
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
{
public:
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
    virtual ~BOWKMeansTrainer();

    // Returns trained vocabulary (i.e. cluster centers).
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;

protected:

    int clusterCount;
    TermCriteria termcrit;
    int attempts;
    int flags;
};

/*
 * Class to compute image descriptor using bag of visual words.
 */
class CV_EXPORTS_W BOWImgDescriptorExtractor
{
public:
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
                               const Ptr<DescriptorMatcher>& dmatcher );
    virtual ~BOWImgDescriptorExtractor();

    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
                  vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
    // compute() is not constant because DescriptorMatcher::match is not constant

    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor )
    { compute(image,keypoints,imgDescriptor); }

    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;

protected:
    Mat vocabulary;
    Ptr<DescriptorExtractor> dextractor;
    Ptr<DescriptorMatcher> dmatcher;
};

>>> be = cv2.BOWImgDescriptorExtractor(None,None)
>>> help(be)
class BOWImgDescriptorExtractor(__builtin__.object)
 ...
 |  compute(...)
 |      compute(image, keypoints, imgDescriptor) -> None
 |  descriptorSize(...)
 |      descriptorSize() -> retval
 |  descriptorType(...)
 |      descriptorType() -> retval
 |  getVocabulary(...)
 |      getVocabulary() -> retval
 |  setVocabulary(...)
 |      setVocabulary(vocabulary) -> None

>>> bt = cv2.BOWKMeansTrainer(4)
>>> help(bt)
class BOWKMeansTrainer(BOWTrainer)
 ...
 |  cluster(...)
 |      cluster() -> retval  or  cluster(descriptors) -> retval
 |  ----------------------------------------------------------------------
 |  Methods inherited from BOWTrainer:
 |  add(...)
 |      add(descriptors) -> None
 |  clear(...)
 |      clear() -> None
 |  descripotorsCount(...)
 |      descripotorsCount() -> retval
 |  getDescriptors(...)
 |      getDescriptors() -> retval
 ...

this worked for me (with 2.4.9):

(one had to navigate around the BowTrainer constructor and alias the compute method)


class CV_EXPORTS_W BOWTrainer
{
public:
    BOWTrainer();
    virtual ~BOWTrainer();

    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;

    CV_WRAP virtual void clear();

    /*
     * Train visual words vocabulary, that is cluster training descriptors and
     * compute cluster centers.
     * Returns cluster centers.
     *
     * descriptors      Training descriptors computed on images keypoints.
     */
    CV_WRAP virtual Mat cluster() const = 0;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;

protected:
    vector<Mat> descriptors;
    int size;
};

/*
 * This is BOWTrainer using cv::kmeans to get vocabulary.
 */
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
{
public:
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
    virtual ~BOWKMeansTrainer();

    // Returns trained vocabulary (i.e. cluster centers).
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;

protected:

    int clusterCount;
    TermCriteria termcrit;
    int attempts;
    int flags;
};

/*
 * Class to compute image descriptor using bag of visual words.
 */
class CV_EXPORTS_W BOWImgDescriptorExtractor
{
public:
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
                               const Ptr<DescriptorMatcher>& dmatcher );
    virtual ~BOWImgDescriptorExtractor();

    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
                  vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
    // compute() is not constant because DescriptorMatcher::match is not constant

    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor )
    { compute(image,keypoints,imgDescriptor); }

    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;

protected:
    Mat vocabulary;
    Ptr<DescriptorExtractor> dextractor;
    Ptr<DescriptorMatcher> dmatcher;
};

>>> be = cv2.BOWImgDescriptorExtractor(None,None)
>>> help(be)
class BOWImgDescriptorExtractor(__builtin__.object)
 ...
 |  compute(...)
 |      compute(image, keypoints, imgDescriptor) -> None
 |  descriptorSize(...)
 |      descriptorSize() -> retval
 |  descriptorType(...)
 |      descriptorType() -> retval
 |  getVocabulary(...)
 |      getVocabulary() -> retval
 |  setVocabulary(...)
 |      setVocabulary(vocabulary) -> None

>>> bt = cv2.BOWKMeansTrainer(4)
>>> help(bt)
class BOWKMeansTrainer(BOWTrainer)
 ...
 |  cluster(...)
 |      cluster() -> retval  or  cluster(descriptors) -> retval
 |  ----------------------------------------------------------------------
 |  Methods inherited from BOWTrainer:
 |  add(...)
 |      add(descriptors) -> None
 |  clear(...)
 |      clear() -> None
 |  descripotorsCount(...)
 |      descripotorsCount() -> retval
 |  getDescriptors(...)
 |      getDescriptors() -> retval
 ...

-----------------------------------------------------------------------------------------------

[EDIT]

tried to get java wrappers running, ended up with having to duplicate dummy declarations in features2d_manual.hpp .

i don't like it. please, a better idea is needed here.

class CV_EXPORTS_AS(BOWKMeansTrainer) javaBOWKMeansTrainer : public BOWKMeansTrainer 
{
public:
#if 0
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(), int attempts=3, int flags=KMEANS_PP_CENTERS );
    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;
    CV_WRAP virtual void clear();
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;
#endif
};

class CV_EXPORTS_AS(BOWImgDescriptorExtractor) javaBOWImgDescriptorExtractor : public BOWImgDescriptorExtractor
{
public:
#if 0
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor, const Ptr<DescriptorMatcher>& dmatcher );
    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;
    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor );
#endif
};

this worked for me (with 2.4.9):

(one had to navigate around the BowTrainer constructor and alias the compute method)


class CV_EXPORTS_W BOWTrainer
{
public:
    BOWTrainer();
    virtual ~BOWTrainer();

    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;

    CV_WRAP virtual void clear();

    /*
     * Train visual words vocabulary, that is cluster training descriptors and
     * compute cluster centers.
     * Returns cluster centers.
     *
     * descriptors      Training descriptors computed on images keypoints.
     */
    CV_WRAP virtual Mat cluster() const = 0;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;

protected:
    vector<Mat> descriptors;
    int size;
};

/*
 * This is BOWTrainer using cv::kmeans to get vocabulary.
 */
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
{
public:
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
    virtual ~BOWKMeansTrainer();

    // Returns trained vocabulary (i.e. cluster centers).
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;

protected:

    int clusterCount;
    TermCriteria termcrit;
    int attempts;
    int flags;
};

/*
 * Class to compute image descriptor using bag of visual words.
 */
class CV_EXPORTS_W BOWImgDescriptorExtractor
{
public:
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
                               const Ptr<DescriptorMatcher>& dmatcher );
    virtual ~BOWImgDescriptorExtractor();

    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
                  vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
    // compute() is not constant because DescriptorMatcher::match is not constant

    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
    { compute(image,keypoints,imgDescriptor); }

    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;

protected:
    Mat vocabulary;
    Ptr<DescriptorExtractor> dextractor;
    Ptr<DescriptorMatcher> dmatcher;
};

>>> be = cv2.BOWImgDescriptorExtractor(None,None)
>>> help(be)
class BOWImgDescriptorExtractor(__builtin__.object)
 ...
 |  compute(...)
 |      compute(image, keypoints, imgDescriptor) -> None
[imgDescriptor]) -> imgDescriptor
 |  descriptorSize(...)
 |      descriptorSize() -> retval
 |  descriptorType(...)
 |      descriptorType() -> retval
 |  getVocabulary(...)
 |      getVocabulary() -> retval
 |  setVocabulary(...)
 |      setVocabulary(vocabulary) -> None

>>> bt = cv2.BOWKMeansTrainer(4)
>>> help(bt)
class BOWKMeansTrainer(BOWTrainer)
 ...
 |  cluster(...)
 |      cluster() -> retval  or  cluster(descriptors) -> retval
 |  ----------------------------------------------------------------------
 |  Methods inherited from BOWTrainer:
 |  add(...)
 |      add(descriptors) -> None
 |  clear(...)
 |      clear() -> None
 |  descripotorsCount(...)
 |      descripotorsCount() -> retval
 |  getDescriptors(...)
 |      getDescriptors() -> retval
 ...

-----------------------------------------------------------------------------------------------

[EDIT]

tried to get java wrappers running, ended up with having to duplicate dummy declarations in features2d_manual.hpp .

i don't like it. please, a better idea is needed here.

class CV_EXPORTS_AS(BOWKMeansTrainer) javaBOWKMeansTrainer : public BOWKMeansTrainer 
{
public:
#if 0
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(), int attempts=3, int flags=KMEANS_PP_CENTERS );
    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;
    CV_WRAP virtual void clear();
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;
#endif
};

class CV_EXPORTS_AS(BOWImgDescriptorExtractor) javaBOWImgDescriptorExtractor : public BOWImgDescriptorExtractor
{
public:
#if 0
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor, const Ptr<DescriptorMatcher>& dmatcher );
    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;
    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor );
#endif
};

[edit]: none of this is nessecary any more, BowTrainer and friends are wrapped to python by default now.

this worked for me (with 2.4.9):

(one had to navigate around the BowTrainer constructor and alias the compute method)


class CV_EXPORTS_W BOWTrainer
{
public:
    BOWTrainer();
    virtual ~BOWTrainer();

    CV_WRAP void add( const Mat& descriptors );
    CV_WRAP const vector<Mat>& getDescriptors() const;
    CV_WRAP int descripotorsCount() const;

    CV_WRAP virtual void clear();

    /*
     * Train visual words vocabulary, that is cluster training descriptors and
     * compute cluster centers.
     * Returns cluster centers.
     *
     * descriptors      Training descriptors computed on images keypoints.
     */
    CV_WRAP virtual Mat cluster() const = 0;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const = 0;

protected:
    vector<Mat> descriptors;
    int size;
};

/*
 * This is BOWTrainer using cv::kmeans to get vocabulary.
 */
class CV_EXPORTS_W BOWKMeansTrainer : public BOWTrainer
{
public:
    CV_WRAP BOWKMeansTrainer( int clusterCount, const TermCriteria& termcrit=TermCriteria(),
                      int attempts=3, int flags=KMEANS_PP_CENTERS );
    virtual ~BOWKMeansTrainer();

    // Returns trained vocabulary (i.e. cluster centers).
    CV_WRAP virtual Mat cluster() const;
    CV_WRAP virtual Mat cluster( const Mat& descriptors ) const;

protected:

    int clusterCount;
    TermCriteria termcrit;
    int attempts;
    int flags;
};

/*
 * Class to compute image descriptor using bag of visual words.
 */
class CV_EXPORTS_W BOWImgDescriptorExtractor
{
public:
    CV_WRAP BOWImgDescriptorExtractor( const Ptr<DescriptorExtractor>& dextractor,
                               const Ptr<DescriptorMatcher>& dmatcher );
    virtual ~BOWImgDescriptorExtractor();

    CV_WRAP void setVocabulary( const Mat& vocabulary );
    CV_WRAP const Mat& getVocabulary() const;
    void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& imgDescriptor,
                  vector<vector<int> >* pointIdxsOfClusters=0, Mat* descriptors=0 );
    // compute() is not constant because DescriptorMatcher::match is not constant

    CV_WRAP_AS(compute) void compute2( const Mat& image, vector<KeyPoint>& keypoints, CV_OUT Mat& imgDescriptor )
    { compute(image,keypoints,imgDescriptor); }

    CV_WRAP int descriptorSize() const;
    CV_WRAP int descriptorType() const;

protected:
    Mat vocabulary;
    Ptr<DescriptorExtractor> dextractor;
    Ptr<DescriptorMatcher> dmatcher;
};

>>> be = cv2.BOWImgDescriptorExtractor(None,None)
>>> help(be)
class BOWImgDescriptorExtractor(__builtin__.object)
 ...
 |  compute(...)
 |      compute(image, keypoints, [imgDescriptor]) -> imgDescriptor
 |  descriptorSize(...)
 |      descriptorSize() -> retval
 |  descriptorType(...)
 |      descriptorType() -> retval
 |  getVocabulary(...)
 |      getVocabulary() -> retval
 |  setVocabulary(...)
 |      setVocabulary(vocabulary) -> None

>>> bt = cv2.BOWKMeansTrainer(4)
>>> help(bt)
class BOWKMeansTrainer(BOWTrainer)
 ...
 |  cluster(...)
 |      cluster() -> retval  or  cluster(descriptors) -> retval
 |  ----------------------------------------------------------------------
 |  Methods inherited from BOWTrainer:
 |  add(...)
 |      add(descriptors) -> None
 |  clear(...)
 |      clear() -> None
 |  descripotorsCount(...)
 |      descripotorsCount() -> retval
 |  getDescriptors(...)
 |      getDescriptors() -> retval
 ...