Commit f4788b36 authored by Ilya Lysenkov's avatar Ilya Lysenkov

Added read/write methods in detectors and some descriptors for XML/YAML persistence

parent bb235220
......@@ -316,6 +316,10 @@ public:
vector<KeyPoint>& keypoints,
Mat& descriptors,
bool useProvidedKeypoints=false) const;
CommonParams getCommonParams () const { return commParams; }
DetectorParams getDetectorParams () const { return detectorParams; }
DescriptorParams getDescriptorParams () const { return descriptorParams; }
protected:
CommonParams commParams;
DetectorParams detectorParams;
......@@ -969,6 +973,12 @@ public:
// - return value: 1 if succeeded, 0 otherwise
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
// - fs: file storage
// - name: node name
......@@ -1110,17 +1120,29 @@ public:
void InitializeDescriptors(IplImage* train_image, const vector<cv::KeyPoint>& features,
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
// - filename: input 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
// - filename: output filename
void SavePCADescriptors(const char* filename);
// SavePCADescriptors: saves PCA descriptors to a file storage
// - fs: output file storage
void SavePCADescriptors(CvFileStorage* fs);
void SavePCADescriptors(CvFileStorage* fs) const;
// GeneratePCA: calculate and save PCA components and descriptors
// - img_path: path to training PCA images directory
......@@ -1254,6 +1276,9 @@ public:
detectImpl( image, mask, keypoints );
}
virtual void read (const FileNode& fn) {};
virtual void write (FileStorage& fs) const {};
protected:
/*
* Detect keypoints; detect() calls this. Must be implemented by the subclass.
......@@ -1272,7 +1297,10 @@ protected:
class CV_EXPORTS FastFeatureDetector : public FeatureDetector
{
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:
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
......@@ -1287,6 +1315,10 @@ class CV_EXPORTS GoodFeaturesToTrackDetector : public FeatureDetector
public:
GoodFeaturesToTrackDetector( int _maxCorners, double _qualityLevel, double _minDistance,
int _blockSize=3, bool _useHarrisDetector=false, double _k=0.04 );
virtual void read (const FileNode& fn);
virtual void write (FileStorage& fs) const;
protected:
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
......@@ -1301,8 +1333,13 @@ protected:
class CV_EXPORTS MserFeatureDetector : public FeatureDetector
{
public:
MserFeatureDetector( CvMSERParams params = cvMSERParams () );
MserFeatureDetector( int delta, int minArea, int maxArea, float maxVariation, float minDiversity,
int maxEvolution, double areaThreshold, double minMargin, int edgeBlurSize );
virtual void read (const FileNode& fn);
virtual void write (FileStorage& fs) const;
protected:
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,
int lineThresholdBinarized=8, int suppressNonmaxSize=5 );
virtual void read (const FileNode& fn);
virtual void write (FileStorage& fs) const;
protected:
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 firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
virtual void read (const FileNode& fn);
virtual void write (FileStorage& fs) const;
protected:
virtual void detectImpl( const Mat& image, const Mat& mask, vector<KeyPoint>& keypoints ) const;
......@@ -1341,6 +1385,9 @@ class CV_EXPORTS SurfFeatureDetector : public FeatureDetector
public:
SurfFeatureDetector( double hessianThreshold = 400., int octaves = 3, int octaveLayers = 4 );
virtual void read (const FileNode& fn);
virtual void write (FileStorage& fs) const;
protected:
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 read (const FileNode &fn) {};
virtual void write (FileStorage &fs) const {};
protected:
/*
* Remove keypoints within border_pixels of an image edge.
......@@ -1394,6 +1444,8 @@ public:
int angleMode=SIFT::CommonParams::FIRST_ANGLE );
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:
SIFT sift;
......@@ -1406,6 +1458,8 @@ public:
int nOctaveLayers=2, bool extended=false );
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:
SURF surf;
......@@ -1693,6 +1747,10 @@ public:
// Clears keypoints storing in collection
virtual void clear();
virtual void read( const FileNode& fn ) {};
virtual void write( FileStorage& fs ) const {};
protected:
KeyPointCollection collection;
};
......@@ -1740,7 +1798,7 @@ public:
virtual ~OneWayDescriptorMatch();
// 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
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
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 ();
protected:
void readParams (const FileNode &fn);
void writeParams (FileStorage& fs) const;
Ptr<OneWayDescriptorBase> base;
Params params;
};
......@@ -1927,6 +1991,17 @@ public:
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:
Extractor extractor;
Matcher matcher;
......
......@@ -91,6 +91,34 @@ void SiftDescriptorExtractor::compute( const Mat& image,
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 *
\****************************************************************************************/
......@@ -114,6 +142,24 @@ void SurfDescriptorExtractor::compute( const Mat& image,
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 *
\****************************************************************************************/
......@@ -194,18 +240,18 @@ OneWayDescriptorMatch::OneWayDescriptorMatch( const Params& _params)
OneWayDescriptorMatch::~OneWayDescriptorMatch()
{}
void OneWayDescriptorMatch::initialize( const Params& _params)
void OneWayDescriptorMatch::initialize( const Params& _params, OneWayDescriptorBase *_base)
{
base.release();
if (_base != 0)
{
base = _base;
}
params = _params;
}
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();
base->Allocate( trainFeatureCount );
......@@ -223,10 +269,6 @@ void OneWayDescriptorMatch::add( const Mat& image, vector<KeyPoint>& 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();
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 )
{
IplImage _image = image;
......
......@@ -75,6 +75,18 @@ FastFeatureDetector::FastFeatureDetector( int _threshold, bool _nonmaxSuppressio
: 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
{
FAST( image, keypoints, threshold, nonmaxSuppression );
......@@ -91,6 +103,26 @@ GoodFeaturesToTrackDetector::GoodFeaturesToTrackDetector( int _maxCorners, doubl
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,
vector<KeyPoint>& keypoints ) const
{
......@@ -117,6 +149,43 @@ MserFeatureDetector::MserFeatureDetector( int delta, int minArea, int maxArea,
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
{
vector<vector<Point> > msers;
......@@ -144,6 +213,29 @@ StarFeatureDetector::StarFeatureDetector(int maxSize, int responseThreshold,
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
{
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,
vector<KeyPoint>& keypoints) const
{
......@@ -172,6 +291,24 @@ SurfFeatureDetector::SurfFeatureDetector( double hessianThreshold, int octaves,
: 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,
vector<KeyPoint>& keypoints) const
{
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment