Added read/write methods in detectors and some descriptors for XML/YAML persistence
This commit is contained in:
parent
bb235220e7
commit
f4788b3645
@ -316,6 +316,10 @@ public:
|
|||||||
vector<KeyPoint>& keypoints,
|
vector<KeyPoint>& keypoints,
|
||||||
Mat& descriptors,
|
Mat& descriptors,
|
||||||
bool useProvidedKeypoints=false) const;
|
bool useProvidedKeypoints=false) const;
|
||||||
|
|
||||||
|
CommonParams getCommonParams () const { return commParams; }
|
||||||
|
DetectorParams getDetectorParams () const { return detectorParams; }
|
||||||
|
DescriptorParams getDescriptorParams () const { return descriptorParams; }
|
||||||
protected:
|
protected:
|
||||||
CommonParams commParams;
|
CommonParams commParams;
|
||||||
DetectorParams detectorParams;
|
DetectorParams detectorParams;
|
||||||
@ -969,6 +973,12 @@ public:
|
|||||||
// - return value: 1 if succeeded, 0 otherwise
|
// - return value: 1 if succeeded, 0 otherwise
|
||||||
int ReadByName(CvFileStorage* fs, CvFileNode* parent, const char* name);
|
int ReadByName(CvFileStorage* fs, CvFileNode* parent, const char* name);
|
||||||
|
|
||||||
|
// ReadByName: reads a descriptor from a file node
|
||||||
|
// - parent: parent node
|
||||||
|
// - name: node name
|
||||||
|
// - return value: 1 if succeeded, 0 otherwise
|
||||||
|
int ReadByName(const FileNode &parent, const char* name);
|
||||||
|
|
||||||
// Write: writes a descriptor into a file storage
|
// Write: writes a descriptor into a file storage
|
||||||
// - fs: file storage
|
// - fs: file storage
|
||||||
// - name: node name
|
// - name: node name
|
||||||
@ -1110,17 +1120,29 @@ public:
|
|||||||
void InitializeDescriptors(IplImage* train_image, const vector<cv::KeyPoint>& features,
|
void InitializeDescriptors(IplImage* train_image, const vector<cv::KeyPoint>& features,
|
||||||
const char* feature_label = "", int desc_start_idx = 0);
|
const char* feature_label = "", int desc_start_idx = 0);
|
||||||
|
|
||||||
|
// SavePCAall: saves PCA components and descriptors to a file storage
|
||||||
|
// - fs: output file storage
|
||||||
|
void SavePCAall (FileStorage &fs) const;
|
||||||
|
|
||||||
|
// LoadPCAall: loads PCA components and descriptors from a file node
|
||||||
|
// - fn: input file node
|
||||||
|
void LoadPCAall (const FileNode &fn);
|
||||||
|
|
||||||
// LoadPCADescriptors: loads PCA descriptors from a file
|
// LoadPCADescriptors: loads PCA descriptors from a file
|
||||||
// - filename: input filename
|
// - filename: input filename
|
||||||
int LoadPCADescriptors(const char* filename);
|
int LoadPCADescriptors(const char* filename);
|
||||||
|
|
||||||
|
// LoadPCADescriptors: loads PCA descriptors from a file node
|
||||||
|
// - fn: input file node
|
||||||
|
int LoadPCADescriptors(const FileNode &fn);
|
||||||
|
|
||||||
// SavePCADescriptors: saves PCA descriptors to a file
|
// SavePCADescriptors: saves PCA descriptors to a file
|
||||||
// - filename: output filename
|
// - filename: output filename
|
||||||
void SavePCADescriptors(const char* filename);
|
void SavePCADescriptors(const char* filename);
|
||||||
|
|
||||||
// SavePCADescriptors: saves PCA descriptors to a file storage
|
// SavePCADescriptors: saves PCA descriptors to a file storage
|
||||||
// - fs: output file storage
|
// - fs: output file storage
|
||||||
void SavePCADescriptors(CvFileStorage* fs);
|
void SavePCADescriptors(CvFileStorage* fs) const;
|
||||||
|
|
||||||
// GeneratePCA: calculate and save PCA components and descriptors
|
// GeneratePCA: calculate and save PCA components and descriptors
|
||||||
// - img_path: path to training PCA images directory
|
// - img_path: path to training PCA images directory
|
||||||
@ -1254,6 +1276,9 @@ public:
|
|||||||
detectImpl( image, mask, keypoints );
|
detectImpl( image, mask, keypoints );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn) {};
|
||||||
|
virtual void write (FileStorage& fs) const {};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*
|
/*
|
||||||
* Detect keypoints; detect() calls this. Must be implemented by the subclass.
|
* Detect keypoints; detect() calls this. Must be implemented by the subclass.
|
||||||
@ -1272,7 +1297,10 @@ protected:
|
|||||||
class CV_EXPORTS FastFeatureDetector : public FeatureDetector
|
class CV_EXPORTS FastFeatureDetector : public FeatureDetector
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FastFeatureDetector( int _threshold, bool _nonmaxSuppression = true );
|
FastFeatureDetector( int _threshold = 1, bool _nonmaxSuppression = true );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
@ -1287,6 +1315,10 @@ class CV_EXPORTS GoodFeaturesToTrackDetector : public FeatureDetector
|
|||||||
public:
|
public:
|
||||||
GoodFeaturesToTrackDetector( int _maxCorners, double _qualityLevel, double _minDistance,
|
GoodFeaturesToTrackDetector( int _maxCorners, double _qualityLevel, double _minDistance,
|
||||||
int _blockSize=3, bool _useHarrisDetector=false, double _k=0.04 );
|
int _blockSize=3, bool _useHarrisDetector=false, double _k=0.04 );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
|
|
||||||
@ -1301,8 +1333,13 @@ protected:
|
|||||||
class CV_EXPORTS MserFeatureDetector : public FeatureDetector
|
class CV_EXPORTS MserFeatureDetector : public FeatureDetector
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
MserFeatureDetector( CvMSERParams params = cvMSERParams () );
|
||||||
MserFeatureDetector( int delta, int minArea, int maxArea, float maxVariation, float minDiversity,
|
MserFeatureDetector( int delta, int minArea, int maxArea, float maxVariation, float minDiversity,
|
||||||
int maxEvolution, double areaThreshold, double minMargin, int edgeBlurSize );
|
int maxEvolution, double areaThreshold, double minMargin, int edgeBlurSize );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
|
|
||||||
@ -1315,6 +1352,9 @@ public:
|
|||||||
StarFeatureDetector( int maxSize=16, int responseThreshold=30, int lineThresholdProjected = 10,
|
StarFeatureDetector( int maxSize=16, int responseThreshold=30, int lineThresholdProjected = 10,
|
||||||
int lineThresholdBinarized=8, int suppressNonmaxSize=5 );
|
int lineThresholdBinarized=8, int suppressNonmaxSize=5 );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
|
|
||||||
@ -1330,6 +1370,10 @@ public:
|
|||||||
int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
|
int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
|
||||||
int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
|
int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
|
||||||
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
|
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
|
|
||||||
@ -1341,6 +1385,9 @@ class CV_EXPORTS SurfFeatureDetector : public FeatureDetector
|
|||||||
public:
|
public:
|
||||||
SurfFeatureDetector( double hessianThreshold = 400., int octaves = 3, int octaveLayers = 4 );
|
SurfFeatureDetector( double hessianThreshold = 400., int octaves = 3, int octaveLayers = 4 );
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
|
||||||
|
|
||||||
@ -1375,6 +1422,9 @@ public:
|
|||||||
*/
|
*/
|
||||||
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const = 0;
|
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const = 0;
|
||||||
|
|
||||||
|
virtual void read (const FileNode &fn) {};
|
||||||
|
virtual void write (FileStorage &fs) const {};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*
|
/*
|
||||||
* Remove keypoints within border_pixels of an image edge.
|
* Remove keypoints within border_pixels of an image edge.
|
||||||
@ -1394,6 +1444,8 @@ public:
|
|||||||
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
|
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
|
||||||
|
|
||||||
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;
|
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;
|
||||||
|
virtual void read (const FileNode &fn);
|
||||||
|
virtual void write (FileStorage &fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SIFT sift;
|
SIFT sift;
|
||||||
@ -1406,6 +1458,8 @@ public:
|
|||||||
int nOctaveLayers=2, bool extended=false );
|
int nOctaveLayers=2, bool extended=false );
|
||||||
|
|
||||||
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;
|
virtual void compute( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors) const;
|
||||||
|
virtual void read (const FileNode &fn);
|
||||||
|
virtual void write (FileStorage &fs) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SURF surf;
|
SURF surf;
|
||||||
@ -1693,6 +1747,10 @@ public:
|
|||||||
|
|
||||||
// Clears keypoints storing in collection
|
// Clears keypoints storing in collection
|
||||||
virtual void clear();
|
virtual void clear();
|
||||||
|
|
||||||
|
|
||||||
|
virtual void read( const FileNode& fn ) {};
|
||||||
|
virtual void write( FileStorage& fs ) const {};
|
||||||
protected:
|
protected:
|
||||||
KeyPointCollection collection;
|
KeyPointCollection collection;
|
||||||
};
|
};
|
||||||
@ -1740,7 +1798,7 @@ public:
|
|||||||
virtual ~OneWayDescriptorMatch();
|
virtual ~OneWayDescriptorMatch();
|
||||||
|
|
||||||
// Sets one way descriptor parameters
|
// Sets one way descriptor parameters
|
||||||
void initialize( const Params& _params );
|
void initialize( const Params& _params, OneWayDescriptorBase *_base = 0 );
|
||||||
|
|
||||||
// Calculates one way descriptors for a set of keypoints
|
// Calculates one way descriptors for a set of keypoints
|
||||||
virtual void add( const Mat& image, vector<KeyPoint>& keypoints );
|
virtual void add( const Mat& image, vector<KeyPoint>& keypoints );
|
||||||
@ -1759,9 +1817,15 @@ public:
|
|||||||
// Classify a set of keypoints. The same as match, but returns point classes rather than indices
|
// Classify a set of keypoints. The same as match, but returns point classes rather than indices
|
||||||
virtual void classify( const Mat& image, vector<KeyPoint>& points );
|
virtual void classify( const Mat& image, vector<KeyPoint>& points );
|
||||||
|
|
||||||
|
virtual void read (const FileNode &fn);
|
||||||
|
virtual void write (FileStorage& fs) const;
|
||||||
|
Params getParams () const {return params;}
|
||||||
|
|
||||||
virtual void clear ();
|
virtual void clear ();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void readParams (const FileNode &fn);
|
||||||
|
void writeParams (FileStorage& fs) const;
|
||||||
Ptr<OneWayDescriptorBase> base;
|
Ptr<OneWayDescriptorBase> base;
|
||||||
Params params;
|
Params params;
|
||||||
};
|
};
|
||||||
@ -1927,6 +1991,17 @@ public:
|
|||||||
matcher.clear();
|
matcher.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
GenericDescriptorMatch::read(fn);
|
||||||
|
extractor.read (fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
GenericDescriptorMatch::write(fs);
|
||||||
|
extractor.write (fs);
|
||||||
|
}
|
||||||
protected:
|
protected:
|
||||||
Extractor extractor;
|
Extractor extractor;
|
||||||
Matcher matcher;
|
Matcher matcher;
|
||||||
|
@ -91,6 +91,34 @@ void SiftDescriptorExtractor::compute( const Mat& image,
|
|||||||
sift(image, Mat(), keypoints, descriptors, useProvidedKeypoints);
|
sift(image, Mat(), keypoints, descriptors, useProvidedKeypoints);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SiftDescriptorExtractor::read (const FileNode &fn)
|
||||||
|
{
|
||||||
|
double magnification = fn["magnification"];
|
||||||
|
bool isNormalize = (int)fn["isNormalize"] != 0;
|
||||||
|
bool recalculateAngles = (int)fn["recalculateAngles"] != 0;
|
||||||
|
int nOctaves = fn["nOctaves"];
|
||||||
|
int nOctaveLayers = fn["nOctaveLayers"];
|
||||||
|
int firstOctave = fn["firstOctave"];
|
||||||
|
int angleMode = fn["angleMode"];
|
||||||
|
|
||||||
|
sift = SIFT( magnification, isNormalize, recalculateAngles, nOctaves, nOctaveLayers, firstOctave, angleMode );
|
||||||
|
}
|
||||||
|
|
||||||
|
void SiftDescriptorExtractor::write (FileStorage &fs) const
|
||||||
|
{
|
||||||
|
// fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
SIFT::CommonParams commParams = sift.getCommonParams ();
|
||||||
|
SIFT::DescriptorParams descriptorParams = sift.getDescriptorParams ();
|
||||||
|
fs << "magnification" << descriptorParams.magnification;
|
||||||
|
fs << "isNormalize" << descriptorParams.isNormalize;
|
||||||
|
fs << "recalculateAngles" << descriptorParams.recalculateAngles;
|
||||||
|
fs << "nOctaves" << commParams.nOctaves;
|
||||||
|
fs << "nOctaveLayers" << commParams.nOctaveLayers;
|
||||||
|
fs << "firstOctave" << commParams.firstOctave;
|
||||||
|
fs << "angleMode" << commParams.angleMode;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************************\
|
/****************************************************************************************\
|
||||||
* SurfDescriptorExtractor *
|
* SurfDescriptorExtractor *
|
||||||
\****************************************************************************************/
|
\****************************************************************************************/
|
||||||
@ -114,6 +142,24 @@ void SurfDescriptorExtractor::compute( const Mat& image,
|
|||||||
std::copy(_descriptors.begin(), _descriptors.end(), descriptors.begin<float>());
|
std::copy(_descriptors.begin(), _descriptors.end(), descriptors.begin<float>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SurfDescriptorExtractor::read( const FileNode &fn )
|
||||||
|
{
|
||||||
|
int nOctaves = fn["nOctaves"];
|
||||||
|
int nOctaveLayers = fn["nOctaveLayers"];
|
||||||
|
bool extended = (int)fn["extended"] != 0;
|
||||||
|
|
||||||
|
surf = SURF( 0.0, nOctaves, nOctaveLayers, extended );
|
||||||
|
}
|
||||||
|
|
||||||
|
void SurfDescriptorExtractor::write( FileStorage &fs ) const
|
||||||
|
{
|
||||||
|
// fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
fs << "nOctaves" << surf.nOctaves;
|
||||||
|
fs << "nOctaveLayers" << surf.nOctaveLayers;
|
||||||
|
fs << "extended" << surf.extended;
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************************\
|
/****************************************************************************************\
|
||||||
* GenericDescriptorMatch *
|
* GenericDescriptorMatch *
|
||||||
\****************************************************************************************/
|
\****************************************************************************************/
|
||||||
@ -194,18 +240,18 @@ OneWayDescriptorMatch::OneWayDescriptorMatch( const Params& _params)
|
|||||||
OneWayDescriptorMatch::~OneWayDescriptorMatch()
|
OneWayDescriptorMatch::~OneWayDescriptorMatch()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void OneWayDescriptorMatch::initialize( const Params& _params)
|
void OneWayDescriptorMatch::initialize( const Params& _params, OneWayDescriptorBase *_base)
|
||||||
{
|
{
|
||||||
base.release();
|
base.release();
|
||||||
|
if (_base != 0)
|
||||||
|
{
|
||||||
|
base = _base;
|
||||||
|
}
|
||||||
params = _params;
|
params = _params;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OneWayDescriptorMatch::add( const Mat& image, vector<KeyPoint>& keypoints )
|
void OneWayDescriptorMatch::add( const Mat& image, vector<KeyPoint>& keypoints )
|
||||||
{
|
{
|
||||||
if( base.empty() )
|
|
||||||
base = new OneWayDescriptorObject( params.patchSize, params.poseCount, params.pcaFilename,
|
|
||||||
params.trainPath, params.trainImagesList, params.minScale, params.maxScale, params.stepScale);
|
|
||||||
|
|
||||||
size_t trainFeatureCount = keypoints.size();
|
size_t trainFeatureCount = keypoints.size();
|
||||||
|
|
||||||
base->Allocate( trainFeatureCount );
|
base->Allocate( trainFeatureCount );
|
||||||
@ -223,10 +269,6 @@ void OneWayDescriptorMatch::add( const Mat& image, vector<KeyPoint>& keypoints )
|
|||||||
|
|
||||||
void OneWayDescriptorMatch::add( KeyPointCollection& keypoints )
|
void OneWayDescriptorMatch::add( KeyPointCollection& keypoints )
|
||||||
{
|
{
|
||||||
if( base.empty() )
|
|
||||||
base = new OneWayDescriptorObject( params.patchSize, params.poseCount, params.pcaFilename,
|
|
||||||
params.trainPath, params.trainImagesList, params.minScale, params.maxScale, params.stepScale);
|
|
||||||
|
|
||||||
size_t trainFeatureCount = keypoints.calcKeypointCount();
|
size_t trainFeatureCount = keypoints.calcKeypointCount();
|
||||||
|
|
||||||
base->Allocate( trainFeatureCount );
|
base->Allocate( trainFeatureCount );
|
||||||
@ -262,6 +304,43 @@ void OneWayDescriptorMatch::match( const Mat& image, vector<KeyPoint>& points, v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorMatch::read( const FileNode &fn )
|
||||||
|
{
|
||||||
|
readParams (fn);
|
||||||
|
|
||||||
|
base = new OneWayDescriptorObject( params.patchSize, params.poseCount, string (), string (), string (),
|
||||||
|
params.minScale, params.maxScale, params.stepScale );
|
||||||
|
base->LoadPCAall (fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorMatch::readParams ( const FileNode &fn )
|
||||||
|
{
|
||||||
|
params.poseCount = fn["poseCount"];
|
||||||
|
int patchWidth = fn["patchWidth"];
|
||||||
|
int patchHeight = fn["patchHeight"];
|
||||||
|
params.patchSize = Size(patchWidth, patchHeight);
|
||||||
|
params.minScale = fn["minScale"];
|
||||||
|
params.maxScale = fn["maxScale"];
|
||||||
|
params.stepScale = fn["stepScale"];
|
||||||
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorMatch::write( FileStorage& fs ) const
|
||||||
|
{
|
||||||
|
// fs << "algorithm" << getAlgorithmName ();
|
||||||
|
writeParams (fs);
|
||||||
|
base->SavePCAall (fs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorMatch::writeParams( FileStorage& fs ) const
|
||||||
|
{
|
||||||
|
fs << "poseCount" << params.poseCount;
|
||||||
|
fs << "patchWidth" << params.patchSize.width;
|
||||||
|
fs << "patchHeight" << params.patchSize.height;
|
||||||
|
fs << "minScale" << params.minScale;
|
||||||
|
fs << "maxScale" << params.maxScale;
|
||||||
|
fs << "stepScale" << params.stepScale;
|
||||||
|
}
|
||||||
|
|
||||||
void OneWayDescriptorMatch::classify( const Mat& image, vector<KeyPoint>& points )
|
void OneWayDescriptorMatch::classify( const Mat& image, vector<KeyPoint>& points )
|
||||||
{
|
{
|
||||||
IplImage _image = image;
|
IplImage _image = image;
|
||||||
|
@ -75,6 +75,18 @@ FastFeatureDetector::FastFeatureDetector( int _threshold, bool _nonmaxSuppressio
|
|||||||
: threshold(_threshold), nonmaxSuppression(_nonmaxSuppression)
|
: threshold(_threshold), nonmaxSuppression(_nonmaxSuppression)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
void FastFeatureDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
threshold = fn["threshold"];
|
||||||
|
nonmaxSuppression = (int)fn["nonmaxSuppression"] ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FastFeatureDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
fs << "threshold" << threshold;
|
||||||
|
fs << "nonmaxSuppression" << nonmaxSuppression;
|
||||||
|
}
|
||||||
|
|
||||||
void FastFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints) const
|
void FastFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints) const
|
||||||
{
|
{
|
||||||
FAST( image, keypoints, threshold, nonmaxSuppression );
|
FAST( image, keypoints, threshold, nonmaxSuppression );
|
||||||
@ -91,6 +103,26 @@ GoodFeaturesToTrackDetector::GoodFeaturesToTrackDetector( int _maxCorners, doubl
|
|||||||
blockSize(_blockSize), useHarrisDetector(_useHarrisDetector), k(_k)
|
blockSize(_blockSize), useHarrisDetector(_useHarrisDetector), k(_k)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
void GoodFeaturesToTrackDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
maxCorners = fn["maxCorners"];
|
||||||
|
qualityLevel = fn["qualityLevel"];
|
||||||
|
minDistance = fn["minDistance"];
|
||||||
|
blockSize = fn["blockSize"];
|
||||||
|
useHarrisDetector = (int) fn["useHarrisDetector"];
|
||||||
|
k = fn["k"];
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoodFeaturesToTrackDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
fs << "maxCorners" << maxCorners;
|
||||||
|
fs << "qualityLevel" << qualityLevel;
|
||||||
|
fs << "minDistance" << minDistance;
|
||||||
|
fs << "blockSize" << blockSize;
|
||||||
|
fs << "useHarrisDetector" << useHarrisDetector;
|
||||||
|
fs << "k" << k;
|
||||||
|
}
|
||||||
|
|
||||||
void GoodFeaturesToTrackDetector::detectImpl( const Mat& image, const Mat& mask,
|
void GoodFeaturesToTrackDetector::detectImpl( const Mat& image, const Mat& mask,
|
||||||
vector<KeyPoint>& keypoints ) const
|
vector<KeyPoint>& keypoints ) const
|
||||||
{
|
{
|
||||||
@ -117,6 +149,43 @@ MserFeatureDetector::MserFeatureDetector( int delta, int minArea, int maxArea,
|
|||||||
maxEvolution, areaThreshold, minMargin, edgeBlurSize )
|
maxEvolution, areaThreshold, minMargin, edgeBlurSize )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
MserFeatureDetector::MserFeatureDetector( CvMSERParams params )
|
||||||
|
: mser( params.delta, params.minArea, params.maxArea, params.maxVariation, params.minDiversity,
|
||||||
|
params.maxEvolution, params.areaThreshold, params.minMargin, params.edgeBlurSize )
|
||||||
|
{}
|
||||||
|
|
||||||
|
void MserFeatureDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
int delta = fn["delta"];
|
||||||
|
int minArea = fn["minArea"];
|
||||||
|
int maxArea = fn["maxArea"];
|
||||||
|
float maxVariation = fn["maxVariation"];
|
||||||
|
float minDiversity = fn["minDiversity"];
|
||||||
|
int maxEvolution = fn["maxEvolution"];
|
||||||
|
double areaThreshold = fn["areaThreshold"];
|
||||||
|
double minMargin = fn["minMargin"];
|
||||||
|
int edgeBlurSize = fn["edgeBlurSize"];
|
||||||
|
|
||||||
|
mser = MSER( delta, minArea, maxArea, maxVariation, minDiversity,
|
||||||
|
maxEvolution, areaThreshold, minMargin, edgeBlurSize );
|
||||||
|
}
|
||||||
|
|
||||||
|
void MserFeatureDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
//fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
fs << "delta" << mser.delta;
|
||||||
|
fs << "minArea" << mser.minArea;
|
||||||
|
fs << "maxArea" << mser.maxArea;
|
||||||
|
fs << "maxVariation" << mser.maxVariation;
|
||||||
|
fs << "minDiversity" << mser.minDiversity;
|
||||||
|
fs << "maxEvolution" << mser.maxEvolution;
|
||||||
|
fs << "areaThreshold" << mser.areaThreshold;
|
||||||
|
fs << "minMargin" << mser.minMargin;
|
||||||
|
fs << "edgeBlurSize" << mser.edgeBlurSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void MserFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const
|
void MserFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const
|
||||||
{
|
{
|
||||||
vector<vector<Point> > msers;
|
vector<vector<Point> > msers;
|
||||||
@ -144,6 +213,29 @@ StarFeatureDetector::StarFeatureDetector(int maxSize, int responseThreshold,
|
|||||||
lineThresholdBinarized, suppressNonmaxSize)
|
lineThresholdBinarized, suppressNonmaxSize)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
void StarFeatureDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
int maxSize = fn["maxSize"];
|
||||||
|
int responseThreshold = fn["responseThreshold"];
|
||||||
|
int lineThresholdProjected = fn["lineThresholdProjected"];
|
||||||
|
int lineThresholdBinarized = fn["lineThresholdBinarized"];
|
||||||
|
int suppressNonmaxSize = fn["suppressNonmaxSize"];
|
||||||
|
|
||||||
|
star = StarDetector( maxSize, responseThreshold, lineThresholdProjected,
|
||||||
|
lineThresholdBinarized, suppressNonmaxSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StarFeatureDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
//fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
fs << "maxSize" << star.maxSize;
|
||||||
|
fs << "responseThreshold" << star.responseThreshold;
|
||||||
|
fs << "lineThresholdProjected" << star.lineThresholdProjected;
|
||||||
|
fs << "lineThresholdBinarized" << star.lineThresholdBinarized;
|
||||||
|
fs << "suppressNonmaxSize" << star.suppressNonmaxSize;
|
||||||
|
}
|
||||||
|
|
||||||
void StarFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints) const
|
void StarFeatureDetector::detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints) const
|
||||||
{
|
{
|
||||||
star(image, keypoints);
|
star(image, keypoints);
|
||||||
@ -159,6 +251,33 @@ SiftFeatureDetector::SiftFeatureDetector(double threshold, double edgeThreshold,
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SiftFeatureDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
double threshold = fn["threshold"];
|
||||||
|
double edgeThreshold = fn["edgeThreshold"];
|
||||||
|
int nOctaves = fn["nOctaves"];
|
||||||
|
int nOctaveLayers = fn["nOctaveLayers"];
|
||||||
|
int firstOctave = fn["firstOctave"];
|
||||||
|
int angleMode = fn["angleMode"];
|
||||||
|
|
||||||
|
sift = SIFT(threshold, edgeThreshold, nOctaves, nOctaveLayers, firstOctave, angleMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SiftFeatureDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
//fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
SIFT::CommonParams commParams = sift.getCommonParams ();
|
||||||
|
SIFT::DetectorParams detectorParams = sift.getDetectorParams ();
|
||||||
|
fs << "threshold" << detectorParams.threshold;
|
||||||
|
fs << "edgeThreshold" << detectorParams.edgeThreshold;
|
||||||
|
fs << "nOctaves" << commParams.nOctaves;
|
||||||
|
fs << "nOctaveLayers" << commParams.nOctaveLayers;
|
||||||
|
fs << "firstOctave" << commParams.firstOctave;
|
||||||
|
fs << "angleMode" << commParams.angleMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SiftFeatureDetector::detectImpl( const Mat& image, const Mat& mask,
|
void SiftFeatureDetector::detectImpl( const Mat& image, const Mat& mask,
|
||||||
vector<KeyPoint>& keypoints) const
|
vector<KeyPoint>& keypoints) const
|
||||||
{
|
{
|
||||||
@ -172,6 +291,24 @@ SurfFeatureDetector::SurfFeatureDetector( double hessianThreshold, int octaves,
|
|||||||
: surf(hessianThreshold, octaves, octaveLayers)
|
: surf(hessianThreshold, octaves, octaveLayers)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
void SurfFeatureDetector::read (const FileNode& fn)
|
||||||
|
{
|
||||||
|
double hessianThreshold = fn["hessianThreshold"];
|
||||||
|
int octaves = fn["octaves"];
|
||||||
|
int octaveLayers = fn["octaveLayers"];
|
||||||
|
|
||||||
|
surf = SURF( hessianThreshold, octaves, octaveLayers );
|
||||||
|
}
|
||||||
|
|
||||||
|
void SurfFeatureDetector::write (FileStorage& fs) const
|
||||||
|
{
|
||||||
|
//fs << "algorithm" << getAlgorithmName ();
|
||||||
|
|
||||||
|
fs << "hessianThreshold" << surf.hessianThreshold;
|
||||||
|
fs << "octaves" << surf.nOctaves;
|
||||||
|
fs << "octaveLayers" << surf.nOctaveLayers;
|
||||||
|
}
|
||||||
|
|
||||||
void SurfFeatureDetector::detectImpl( const Mat& image, const Mat& mask,
|
void SurfFeatureDetector::detectImpl( const Mat& image, const Mat& mask,
|
||||||
vector<KeyPoint>& keypoints) const
|
vector<KeyPoint>& keypoints) const
|
||||||
{
|
{
|
||||||
|
@ -140,6 +140,7 @@ namespace cv{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void readPCAFeatures(const char *filename, CvMat** avg, CvMat** eigenvectors, const char *postfix = "");
|
void readPCAFeatures(const char *filename, CvMat** avg, CvMat** eigenvectors, const char *postfix = "");
|
||||||
|
void readPCAFeatures(const FileNode &fn, CvMat** avg, CvMat** eigenvectors, const char* postfix = "");
|
||||||
void savePCAFeatures(FileStorage &fs, const char* postfix, CvMat* avg, CvMat* eigenvectors);
|
void savePCAFeatures(FileStorage &fs, const char* postfix, CvMat* avg, CvMat* eigenvectors);
|
||||||
void calcPCAFeatures(vector<IplImage*>& patches, FileStorage &fs, const char* postfix, CvMat** avg,
|
void calcPCAFeatures(vector<IplImage*>& patches, FileStorage &fs, const char* postfix, CvMat** avg,
|
||||||
CvMat** eigenvectors);
|
CvMat** eigenvectors);
|
||||||
@ -692,9 +693,9 @@ namespace cv{
|
|||||||
cvReleaseMat(&mat);
|
cvReleaseMat(&mat);
|
||||||
}
|
}
|
||||||
|
|
||||||
int OneWayDescriptor::ReadByName(CvFileStorage* fs, CvFileNode* parent, const char* name)
|
int OneWayDescriptor::ReadByName(const FileNode &parent, const char* name)
|
||||||
{
|
{
|
||||||
CvMat* mat = (CvMat*)cvReadByName(fs, parent, name);
|
CvMat* mat = reinterpret_cast<CvMat*> (parent[name].readObj ());
|
||||||
if(!mat)
|
if(!mat)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
@ -717,6 +718,11 @@ namespace cv{
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int OneWayDescriptor::ReadByName(CvFileStorage* fs, CvFileNode* parent, const char* name)
|
||||||
|
{
|
||||||
|
return ReadByName (FileNode (fs, parent), name);
|
||||||
|
}
|
||||||
|
|
||||||
IplImage* OneWayDescriptor::GetPatch(int index)
|
IplImage* OneWayDescriptor::GetPatch(int index)
|
||||||
{
|
{
|
||||||
return m_samples[index];
|
return m_samples[index];
|
||||||
@ -1216,6 +1222,10 @@ namespace cv{
|
|||||||
int pca_dim_high, int pca_dim_low)
|
int pca_dim_high, int pca_dim_low)
|
||||||
: m_pca_dim_high(pca_dim_high), m_pca_dim_low(pca_dim_low), scale_min (0.7f), scale_max(1.5f), scale_step (1.2f)
|
: m_pca_dim_high(pca_dim_high), m_pca_dim_low(pca_dim_low), scale_min (0.7f), scale_max(1.5f), scale_step (1.2f)
|
||||||
{
|
{
|
||||||
|
#if defined(_KDTREE)
|
||||||
|
m_pca_descriptors_matrix = 0;
|
||||||
|
m_pca_descriptors_tree = 0;
|
||||||
|
#endif
|
||||||
// m_pca_descriptors_matrix = 0;
|
// m_pca_descriptors_matrix = 0;
|
||||||
m_patch_size = patch_size;
|
m_patch_size = patch_size;
|
||||||
m_pose_count = pose_count;
|
m_pose_count = pose_count;
|
||||||
@ -1276,7 +1286,10 @@ namespace cv{
|
|||||||
int pca_dim_high, int pca_dim_low)
|
int pca_dim_high, int pca_dim_low)
|
||||||
: m_pca_dim_high(pca_dim_high), m_pca_dim_low(pca_dim_low), scale_min(_scale_min), scale_max(_scale_max), scale_step(_scale_step)
|
: m_pca_dim_high(pca_dim_high), m_pca_dim_low(pca_dim_low), scale_min(_scale_min), scale_max(_scale_max), scale_step(_scale_step)
|
||||||
{
|
{
|
||||||
// m_pca_descriptors_matrix = 0;
|
#if defined(_KDTREE)
|
||||||
|
m_pca_descriptors_matrix = 0;
|
||||||
|
m_pca_descriptors_tree = 0;
|
||||||
|
#endif
|
||||||
m_patch_size = patch_size;
|
m_patch_size = patch_size;
|
||||||
m_pose_count = pose_count;
|
m_pose_count = pose_count;
|
||||||
m_pyr_levels = pyr_levels;
|
m_pyr_levels = pyr_levels;
|
||||||
@ -1291,6 +1304,12 @@ namespace cv{
|
|||||||
|
|
||||||
m_descriptors = 0;
|
m_descriptors = 0;
|
||||||
|
|
||||||
|
|
||||||
|
if (pca_filename.length() == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
CvFileStorage* fs = cvOpenFileStorage(pca_filename.c_str(), NULL, CV_STORAGE_READ);
|
CvFileStorage* fs = cvOpenFileStorage(pca_filename.c_str(), NULL, CV_STORAGE_READ);
|
||||||
if (fs != 0)
|
if (fs != 0)
|
||||||
{
|
{
|
||||||
@ -1313,6 +1332,26 @@ namespace cv{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorBase::LoadPCAall (const FileNode &fn)
|
||||||
|
{
|
||||||
|
// m_pose_count = fn["pose_count"];
|
||||||
|
// int patch_width = fn["patch_width"];
|
||||||
|
// int patch_height = fn["patch_height"];
|
||||||
|
// m_patch_size = cvSize (patch_width, patch_height);
|
||||||
|
// m_pyr_levels = fn["pyr_levels"];
|
||||||
|
// m_pca_dim_high = fn["pca_dim_high"];
|
||||||
|
// m_pca_dim_low = fn["pca_dim_low"];
|
||||||
|
// scale_min = fn["scale_min"];
|
||||||
|
// scale_max = fn["scale_max"];
|
||||||
|
// scale_step = fn["scale_step"];
|
||||||
|
|
||||||
|
readPCAFeatures(fn, &m_pca_avg, &m_pca_eigenvectors, "_lr");
|
||||||
|
readPCAFeatures(fn, &m_pca_hr_avg, &m_pca_hr_eigenvectors, "_hr");
|
||||||
|
m_pca_descriptors = new OneWayDescriptor[m_pca_dim_high + 1];
|
||||||
|
#if !defined(_GH_REGIONS)
|
||||||
|
LoadPCADescriptors(fn);
|
||||||
|
#endif //_GH_REGIONS
|
||||||
|
}
|
||||||
|
|
||||||
OneWayDescriptorBase::~OneWayDescriptorBase()
|
OneWayDescriptorBase::~OneWayDescriptorBase()
|
||||||
{
|
{
|
||||||
@ -1327,34 +1366,50 @@ namespace cv{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(m_descriptors)
|
||||||
delete []m_descriptors;
|
delete []m_descriptors;
|
||||||
|
|
||||||
|
if(m_poses)
|
||||||
delete []m_poses;
|
delete []m_poses;
|
||||||
|
|
||||||
|
if (m_transforms)
|
||||||
|
{
|
||||||
for(int i = 0; i < m_pose_count; i++)
|
for(int i = 0; i < m_pose_count; i++)
|
||||||
{
|
{
|
||||||
cvReleaseMat(&m_transforms[i]);
|
cvReleaseMat(&m_transforms[i]);
|
||||||
}
|
}
|
||||||
delete []m_transforms;
|
delete []m_transforms;
|
||||||
|
}
|
||||||
#if defined(_KDTREE)
|
#if defined(_KDTREE)
|
||||||
// if (m_pca_descriptors_matrix)
|
if (m_pca_descriptors_matrix)
|
||||||
// delete m_pca_descriptors_matrix;
|
{
|
||||||
cvReleaseMat(&m_pca_descriptors_matrix);
|
cvReleaseMat(&m_pca_descriptors_matrix);
|
||||||
|
}
|
||||||
|
if (m_pca_descriptors_tree)
|
||||||
|
{
|
||||||
delete m_pca_descriptors_tree;
|
delete m_pca_descriptors_tree;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void OneWayDescriptorBase::clear(){
|
void OneWayDescriptorBase::clear(){
|
||||||
|
if (m_descriptors)
|
||||||
|
{
|
||||||
delete []m_descriptors;
|
delete []m_descriptors;
|
||||||
m_descriptors = 0;
|
m_descriptors = 0;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(_KDTREE)
|
#if defined(_KDTREE)
|
||||||
// if (m_pca_descriptors_matrix)
|
if (m_pca_descriptors_matrix)
|
||||||
// delete m_pca_descriptors_matrix;
|
{
|
||||||
cvReleaseMat(&m_pca_descriptors_matrix);
|
cvReleaseMat(&m_pca_descriptors_matrix);
|
||||||
m_pca_descriptors_matrix = 0;
|
m_pca_descriptors_matrix = 0;
|
||||||
|
}
|
||||||
|
if (m_pca_descriptors_tree)
|
||||||
|
{
|
||||||
delete m_pca_descriptors_tree;
|
delete m_pca_descriptors_tree;
|
||||||
m_pca_descriptors_tree = 0;
|
m_pca_descriptors_tree = 0;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1545,27 +1600,33 @@ namespace cv{
|
|||||||
|
|
||||||
int OneWayDescriptorBase::LoadPCADescriptors(const char* filename)
|
int OneWayDescriptorBase::LoadPCADescriptors(const char* filename)
|
||||||
{
|
{
|
||||||
CvMemStorage* storage = cvCreateMemStorage();
|
FileStorage fs = FileStorage (filename, FileStorage::READ);
|
||||||
CvFileStorage* fs = cvOpenFileStorage(filename, storage, CV_STORAGE_READ);
|
if(!fs.isOpened ())
|
||||||
if(!fs)
|
|
||||||
{
|
{
|
||||||
cvReleaseMemStorage(&storage);
|
|
||||||
printf("File %s not found...\n", filename);
|
printf("File %s not found...\n", filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read affine poses
|
LoadPCADescriptors (fs.root ());
|
||||||
CvFileNode* node = cvGetFileNodeByName(fs, 0, "affine poses");
|
|
||||||
if(node != 0)
|
printf("Successfully read %d pca components\n", m_pca_dim_high);
|
||||||
|
fs.release ();
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OneWayDescriptorBase::LoadPCADescriptors(const FileNode &fn)
|
||||||
{
|
{
|
||||||
CvMat* poses = (CvMat*)cvRead(fs, node);
|
// read affine poses
|
||||||
//if(poses->rows != m_pose_count)
|
// FileNode* node = cvGetFileNodeByName(fs, 0, "affine poses");
|
||||||
//{
|
CvMat* poses = reinterpret_cast<CvMat*> (fn["affine_poses"].readObj ());
|
||||||
// printf("Inconsistency in the number of poses between the class instance and the file! Exiting...\n");
|
if (poses == 0)
|
||||||
// cvReleaseMat(&poses);
|
{
|
||||||
// cvReleaseFileStorage(&fs);
|
poses = reinterpret_cast<CvMat*> (fn["affine poses"].readObj ());
|
||||||
// cvReleaseMemStorage(&storage);
|
if (poses == 0)
|
||||||
//}
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if(m_poses)
|
if(m_poses)
|
||||||
{
|
{
|
||||||
@ -1583,17 +1644,12 @@ namespace cv{
|
|||||||
|
|
||||||
// now initialize pose transforms
|
// now initialize pose transforms
|
||||||
InitializeTransformsFromPoses();
|
InitializeTransformsFromPoses();
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Node \"affine poses\" not found...\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
node = cvGetFileNodeByName(fs, 0, "pca components number");
|
m_pca_dim_high = (int) fn["pca_components_number"];
|
||||||
if(node != 0)
|
if (m_pca_dim_high == 0)
|
||||||
{
|
{
|
||||||
|
m_pca_dim_high = (int) fn["pca components number"];
|
||||||
m_pca_dim_high = cvReadInt(node);
|
}
|
||||||
if(m_pca_descriptors)
|
if(m_pca_descriptors)
|
||||||
{
|
{
|
||||||
delete []m_pca_descriptors;
|
delete []m_pca_descriptors;
|
||||||
@ -1604,20 +1660,15 @@ namespace cv{
|
|||||||
m_pca_descriptors[i].Allocate(m_pose_count, m_patch_size, 1);
|
m_pca_descriptors[i].Allocate(m_pose_count, m_patch_size, 1);
|
||||||
m_pca_descriptors[i].SetTransforms(m_poses, m_transforms);
|
m_pca_descriptors[i].SetTransforms(m_poses, m_transforms);
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
sprintf(buf, "descriptor for pca component %d", i);
|
sprintf(buf, "descriptor_for_pca_component_%d", i);
|
||||||
m_pca_descriptors[i].ReadByName(fs, 0, buf);
|
|
||||||
}
|
if (! m_pca_descriptors[i].ReadByName(fn, buf))
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
printf("Node \"pca components number\" not found...\n");
|
char buf[1024];
|
||||||
|
sprintf(buf, "descriptor for pca component %d", i);
|
||||||
|
m_pca_descriptors[i].ReadByName(fn, buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cvReleaseFileStorage(&fs);
|
|
||||||
cvReleaseMemStorage(&storage);
|
|
||||||
|
|
||||||
printf("Successfully read %d pca components\n", m_pca_dim_high);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1668,31 +1719,8 @@ namespace cv{
|
|||||||
cvReleaseMat(&eigenvalues);
|
cvReleaseMat(&eigenvalues);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadPCAFeatures(const char* path, const char* images_list, vector<IplImage*>& patches, CvSize patch_size)
|
void extractPatches (IplImage *img, vector<IplImage*>& patches, CvSize patch_size)
|
||||||
{
|
{
|
||||||
char images_filename[1024];
|
|
||||||
sprintf(images_filename, "%s/%s", path, images_list);
|
|
||||||
FILE *pFile = fopen(images_filename, "r");
|
|
||||||
if (pFile == 0)
|
|
||||||
{
|
|
||||||
printf("Cannot open images list file %s\n", images_filename);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while (!feof(pFile))
|
|
||||||
{
|
|
||||||
char imagename[1024];
|
|
||||||
if (fscanf(pFile, "%s", imagename) <= 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
char filename[1024];
|
|
||||||
sprintf(filename, "%s/%s", path, imagename);
|
|
||||||
|
|
||||||
//printf("Reading image %s...", filename);
|
|
||||||
IplImage* img = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
|
|
||||||
//printf("done\n");
|
|
||||||
|
|
||||||
vector<KeyPoint> features;
|
vector<KeyPoint> features;
|
||||||
SURF surf_extractor(1.0f);
|
SURF surf_extractor(1.0f);
|
||||||
//printf("Extracting SURF features...");
|
//printf("Extracting SURF features...");
|
||||||
@ -1718,10 +1746,49 @@ namespace cv{
|
|||||||
cvCopy(img, patch);
|
cvCopy(img, patch);
|
||||||
patches.push_back(patch);
|
patches.push_back(patch);
|
||||||
cvResetImageROI(img);
|
cvResetImageROI(img);
|
||||||
|
}
|
||||||
|
//printf("Completed file, extracted %d features\n", (int)features.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("Completed file, extracted %d features\n", (int)features.size());
|
/*
|
||||||
|
void loadPCAFeatures(const FileNode &fn, vector<IplImage*>& patches, CvSize patch_size)
|
||||||
|
{
|
||||||
|
FileNodeIterator begin = fn.begin();
|
||||||
|
for (FileNodeIterator i = fn.begin(); i != fn.end(); i++)
|
||||||
|
{
|
||||||
|
IplImage *img = reinterpret_cast<IplImage*> ((*i).readObj());
|
||||||
|
extractPatches (img, patches, patch_size);
|
||||||
|
cvReleaseImage(&img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
void loadPCAFeatures(const char* path, const char* images_list, vector<IplImage*>& patches, CvSize patch_size)
|
||||||
|
{
|
||||||
|
char images_filename[1024];
|
||||||
|
sprintf(images_filename, "%s/%s", path, images_list);
|
||||||
|
FILE *pFile = fopen(images_filename, "r");
|
||||||
|
if (pFile == 0)
|
||||||
|
{
|
||||||
|
printf("Cannot open images list file %s\n", images_filename);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (!feof(pFile))
|
||||||
|
{
|
||||||
|
char imagename[1024];
|
||||||
|
if (fscanf(pFile, "%s", imagename) <= 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
char filename[1024];
|
||||||
|
sprintf(filename, "%s/%s", path, imagename);
|
||||||
|
|
||||||
|
//printf("Reading image %s...", filename);
|
||||||
|
IplImage* img = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
|
||||||
|
//printf("done\n");
|
||||||
|
|
||||||
|
extractPatches (img, patches, patch_size);
|
||||||
|
|
||||||
cvReleaseImage(&img);
|
cvReleaseImage(&img);
|
||||||
}
|
}
|
||||||
@ -1736,6 +1803,35 @@ namespace cv{
|
|||||||
calcPCAFeatures(patches, fs, postfix, avg, eigenvectors);
|
calcPCAFeatures(patches, fs, postfix, avg, eigenvectors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void generatePCAFeatures(const FileNode &fn, const char* postfix,
|
||||||
|
CvSize patch_size, CvMat** avg, CvMat** eigenvectors)
|
||||||
|
{
|
||||||
|
vector<IplImage*> patches;
|
||||||
|
loadPCAFeatures(fn, patches, patch_size);
|
||||||
|
calcPCAFeatures(patches, fs, postfix, avg, eigenvectors);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void OneWayDescriptorBase::GeneratePCA(const FileNode &fn, int pose_count)
|
||||||
|
{
|
||||||
|
generatePCAFeatures(fn, "hr", m_patch_size, &m_pca_hr_avg, &m_pca_hr_eigenvectors);
|
||||||
|
generatePCAFeatures(fn, "lr", cvSize(m_patch_size.width / 2, m_patch_size.height / 2),
|
||||||
|
&m_pca_avg, &m_pca_eigenvectors);
|
||||||
|
|
||||||
|
|
||||||
|
OneWayDescriptorBase descriptors(m_patch_size, pose_count);
|
||||||
|
descriptors.SetPCAHigh(m_pca_hr_avg, m_pca_hr_eigenvectors);
|
||||||
|
descriptors.SetPCALow(m_pca_avg, m_pca_eigenvectors);
|
||||||
|
|
||||||
|
printf("Calculating %d PCA descriptors (you can grab a coffee, this will take a while)...\n",
|
||||||
|
descriptors.GetPCADimHigh());
|
||||||
|
descriptors.InitializePoseTransforms();
|
||||||
|
descriptors.CreatePCADescriptors();
|
||||||
|
descriptors.SavePCADescriptors(*fs);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
void OneWayDescriptorBase::GeneratePCA(const char* img_path, const char* images_list, int pose_count)
|
void OneWayDescriptorBase::GeneratePCA(const char* img_path, const char* images_list, int pose_count)
|
||||||
{
|
{
|
||||||
char pca_filename[1024];
|
char pca_filename[1024];
|
||||||
@ -1759,6 +1855,22 @@ namespace cv{
|
|||||||
fs.release();
|
fs.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OneWayDescriptorBase::SavePCAall (FileStorage &fs) const
|
||||||
|
{
|
||||||
|
// fs << "pose_count" << m_pose_count;
|
||||||
|
// fs << "patch_width" << m_patch_size.width;
|
||||||
|
// fs << "patch_height" << m_patch_size.height;
|
||||||
|
// fs << "scale_min" << scale_min;
|
||||||
|
// fs << "scale_max" << scale_max;
|
||||||
|
// fs << "scale_step" << scale_step;
|
||||||
|
// fs << "pyr_levels" << m_pyr_levels;
|
||||||
|
// fs << "pca_dim_low" << m_pca_dim_low;
|
||||||
|
|
||||||
|
savePCAFeatures(fs, "hr", m_pca_hr_avg, m_pca_hr_eigenvectors);
|
||||||
|
savePCAFeatures(fs, "lr", m_pca_avg, m_pca_eigenvectors);
|
||||||
|
SavePCADescriptors(*fs);
|
||||||
|
}
|
||||||
|
|
||||||
void OneWayDescriptorBase::SavePCADescriptors(const char* filename)
|
void OneWayDescriptorBase::SavePCADescriptors(const char* filename)
|
||||||
{
|
{
|
||||||
CvMemStorage* storage = cvCreateMemStorage();
|
CvMemStorage* storage = cvCreateMemStorage();
|
||||||
@ -1770,15 +1882,15 @@ namespace cv{
|
|||||||
cvReleaseFileStorage(&fs);
|
cvReleaseFileStorage(&fs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OneWayDescriptorBase::SavePCADescriptors(CvFileStorage *fs)
|
void OneWayDescriptorBase::SavePCADescriptors(CvFileStorage *fs) const
|
||||||
{
|
{
|
||||||
cvWriteInt(fs, "pca components number", m_pca_dim_high);
|
cvWriteInt(fs, "pca_components_number", m_pca_dim_high);
|
||||||
cvWriteComment(
|
cvWriteComment(
|
||||||
fs,
|
fs,
|
||||||
"The first component is the average Vector, so the total number of components is <pca components number> + 1",
|
"The first component is the average Vector, so the total number of components is <pca components number> + 1",
|
||||||
0);
|
0);
|
||||||
cvWriteInt(fs, "patch width", m_patch_size.width);
|
cvWriteInt(fs, "patch_width", m_patch_size.width);
|
||||||
cvWriteInt(fs, "patch height", m_patch_size.height);
|
cvWriteInt(fs, "patch_height", m_patch_size.height);
|
||||||
|
|
||||||
// pack the affine transforms into a single CvMat and write them
|
// pack the affine transforms into a single CvMat and write them
|
||||||
CvMat* poses = cvCreateMat(m_pose_count, 4, CV_32FC1);
|
CvMat* poses = cvCreateMat(m_pose_count, 4, CV_32FC1);
|
||||||
@ -1789,13 +1901,13 @@ namespace cv{
|
|||||||
cvmSet(poses, i, 2, m_poses[i].lambda1);
|
cvmSet(poses, i, 2, m_poses[i].lambda1);
|
||||||
cvmSet(poses, i, 3, m_poses[i].lambda2);
|
cvmSet(poses, i, 3, m_poses[i].lambda2);
|
||||||
}
|
}
|
||||||
cvWrite(fs, "affine poses", poses);
|
cvWrite(fs, "affine_poses", poses);
|
||||||
cvReleaseMat(&poses);
|
cvReleaseMat(&poses);
|
||||||
|
|
||||||
for (int i = 0; i < m_pca_dim_high + 1; i++)
|
for (int i = 0; i < m_pca_dim_high + 1; i++)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
sprintf(buf, "descriptor for pca component %d", i);
|
sprintf(buf, "descriptor_for_pca_component_%d", i);
|
||||||
m_pca_descriptors[i].Write(fs, buf);
|
m_pca_descriptors[i].Write(fs, buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1950,6 +2062,7 @@ namespace cv{
|
|||||||
|
|
||||||
OneWayDescriptorObject::~OneWayDescriptorObject()
|
OneWayDescriptorObject::~OneWayDescriptorObject()
|
||||||
{
|
{
|
||||||
|
if (m_part_id)
|
||||||
delete []m_part_id;
|
delete []m_part_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1993,28 +2106,32 @@ namespace cv{
|
|||||||
|
|
||||||
void readPCAFeatures(const char* filename, CvMat** avg, CvMat** eigenvectors, const char* postfix)
|
void readPCAFeatures(const char* filename, CvMat** avg, CvMat** eigenvectors, const char* postfix)
|
||||||
{
|
{
|
||||||
CvMemStorage* storage = cvCreateMemStorage();
|
FileStorage fs = FileStorage(filename, FileStorage::READ);
|
||||||
CvFileStorage* fs = cvOpenFileStorage(filename, storage, CV_STORAGE_READ);
|
if (!fs.isOpened ())
|
||||||
if (!fs)
|
|
||||||
{
|
{
|
||||||
printf("Cannot open file %s! Exiting!", filename);
|
printf("Cannot open file %s! Exiting!", filename);
|
||||||
cvReleaseMemStorage(&storage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[1024];
|
readPCAFeatures (fs.root (), avg, eigenvectors, postfix);
|
||||||
sprintf(buf, "avg%s", postfix);
|
fs.release ();
|
||||||
CvFileNode* node = cvGetFileNodeByName(fs, 0, buf);
|
}
|
||||||
CvMat* _avg = (CvMat*)cvRead(fs, node);
|
|
||||||
sprintf(buf, "eigenvectors%s", postfix);
|
|
||||||
node = cvGetFileNodeByName(fs, 0, buf);
|
|
||||||
CvMat* _eigenvectors = (CvMat*)cvRead(fs, node);
|
|
||||||
|
|
||||||
|
void readPCAFeatures(const FileNode &fn, CvMat** avg, CvMat** eigenvectors, const char* postfix)
|
||||||
|
{
|
||||||
|
std::string str = std::string ("avg") + postfix;
|
||||||
|
CvMat* _avg = reinterpret_cast<CvMat*> (fn[str].readObj());
|
||||||
|
if (_avg != 0)
|
||||||
|
{
|
||||||
*avg = cvCloneMat(_avg);
|
*avg = cvCloneMat(_avg);
|
||||||
*eigenvectors = cvCloneMat(_eigenvectors);
|
|
||||||
|
|
||||||
cvReleaseMat(&_avg);
|
cvReleaseMat(&_avg);
|
||||||
|
}
|
||||||
|
|
||||||
|
str = std::string ("eigenvectors") + postfix;
|
||||||
|
CvMat* _eigenvectors = reinterpret_cast<CvMat*> (fn[str].readObj());
|
||||||
|
if (_eigenvectors != 0)
|
||||||
|
{
|
||||||
|
*eigenvectors = cvCloneMat(_eigenvectors);
|
||||||
cvReleaseMat(&_eigenvectors);
|
cvReleaseMat(&_eigenvectors);
|
||||||
cvReleaseFileStorage(&fs);
|
}
|
||||||
cvReleaseMemStorage(&storage);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user