This source file includes following definitions.
- setDistanceFlag
- getDistanceFlag
- setRankProportion
- getRankProportion
- write
- read
- _apply
- computeDistance
- createHausdorffDistanceExtractor
#include "precomp.hpp"
namespace cv
{
class HausdorffDistanceExtractorImpl : public HausdorffDistanceExtractor
{
public:
HausdorffDistanceExtractorImpl(int _distanceFlag = NORM_L1, float _rankProportion=0.6)
{
distanceFlag = _distanceFlag;
rankProportion = _rankProportion;
name_ = "ShapeDistanceExtractor.HAU";
}
~HausdorffDistanceExtractorImpl()
{
}
virtual float computeDistance(InputArray contour1, InputArray contour2);
virtual void setDistanceFlag(int _distanceFlag) {distanceFlag=_distanceFlag;}
virtual int getDistanceFlag() const {return distanceFlag;}
virtual void setRankProportion(float _rankProportion)
{
CV_Assert((_rankProportion>0) && (_rankProportion<=1));
rankProportion=_rankProportion;
}
virtual float getRankProportion() const {return rankProportion;}
virtual void write(FileStorage& fs) const
{
fs << "name" << name_
<< "distance" << distanceFlag
<< "rank" << rankProportion;
}
virtual void read(const FileNode& fn)
{
CV_Assert( (String)fn["name"] == name_ );
distanceFlag = (int)fn["distance"];
rankProportion = (float)fn["rank"];
}
private:
int distanceFlag;
float rankProportion;
protected:
String name_;
};
static float _apply(const Mat &set1, const Mat &set2, int distType, double propRank)
{
Mat disMat(set1.cols, set2.cols, CV_32F);
int K = int(propRank*(disMat.rows-1));
for (int r=0; r<disMat.rows; r++)
{
for (int c=0; c<disMat.cols; c++)
{
Point2f diff = set1.at<Point2f>(0,r)-set2.at<Point2f>(0,c);
disMat.at<float>(r,c) = (float)norm(Mat(diff), distType);
}
}
Mat shortest(disMat.rows,1,CV_32F);
for (int ii=0; ii<disMat.rows; ii++)
{
Mat therow = disMat.row(ii);
double mindis;
minMaxIdx(therow, &mindis);
shortest.at<float>(ii,0) = float(mindis);
}
Mat sorted;
cv::sort(shortest, sorted, SORT_EVERY_ROW | SORT_DESCENDING);
return sorted.at<float>(K,0);
}
float HausdorffDistanceExtractorImpl::computeDistance(InputArray contour1, InputArray contour2)
{
Mat set1=contour1.getMat(), set2=contour2.getMat();
if (set1.type() != CV_32F)
set1.convertTo(set1, CV_32F);
if (set2.type() != CV_32F)
set2.convertTo(set2, CV_32F);
CV_Assert((set1.channels()==2) && (set1.cols>0));
CV_Assert((set2.channels()==2) && (set2.cols>0));
return std::max( _apply(set1, set2, distanceFlag, rankProportion),
_apply(set2, set1, distanceFlag, rankProportion) );
}
Ptr <HausdorffDistanceExtractor> createHausdorffDistanceExtractor(int distanceFlag, float rankProp)
{
return Ptr<HausdorffDistanceExtractor>(new HausdorffDistanceExtractorImpl(distanceFlag, rankProp));
}
}