/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "precomp.hpp"4243namespace cv44{4546class HausdorffDistanceExtractorImpl CV_FINAL : public HausdorffDistanceExtractor47{48public:49/* Constructor */50HausdorffDistanceExtractorImpl(int _distanceFlag = NORM_L1, float _rankProportion=0.6)51{52distanceFlag = _distanceFlag;53rankProportion = _rankProportion;54name_ = "ShapeDistanceExtractor.HAU";55}5657/* Destructor */58~HausdorffDistanceExtractorImpl()59{60}6162//! the main operator63virtual float computeDistance(InputArray contour1, InputArray contour2) CV_OVERRIDE;6465//! Setters/Getters66virtual void setDistanceFlag(int _distanceFlag) CV_OVERRIDE {distanceFlag=_distanceFlag;}67virtual int getDistanceFlag() const CV_OVERRIDE {return distanceFlag;}6869virtual void setRankProportion(float _rankProportion) CV_OVERRIDE70{71CV_Assert((_rankProportion>0) && (_rankProportion<=1));72rankProportion=_rankProportion;73}74virtual float getRankProportion() const CV_OVERRIDE {return rankProportion;}7576//! write/read77virtual void write(FileStorage& fs) const CV_OVERRIDE78{79writeFormat(fs);80fs << "name" << name_81<< "distance" << distanceFlag82<< "rank" << rankProportion;83}8485virtual void read(const FileNode& fn) CV_OVERRIDE86{87CV_Assert( (String)fn["name"] == name_ );88distanceFlag = (int)fn["distance"];89rankProportion = (float)fn["rank"];90}9192private:93int distanceFlag;94float rankProportion;9596protected:97String name_;98};99100//! Hausdorff distance for a pair of set of points101static float _apply(const Mat &set1, const Mat &set2, int distType, double propRank)102{103// Building distance matrix //104Mat disMat(set1.cols, set2.cols, CV_32F);105int K = int(propRank*(disMat.rows-1));106107for (int r=0; r<disMat.rows; r++)108{109for (int c=0; c<disMat.cols; c++)110{111Point2f diff = set1.at<Point2f>(0,r)-set2.at<Point2f>(0,c);112disMat.at<float>(r,c) = (float)norm(Mat(diff), distType);113}114}115116Mat shortest(disMat.rows,1,CV_32F);117for (int ii=0; ii<disMat.rows; ii++)118{119Mat therow = disMat.row(ii);120double mindis;121minMaxIdx(therow, &mindis);122shortest.at<float>(ii,0) = float(mindis);123}124Mat sorted;125cv::sort(shortest, sorted, SORT_EVERY_ROW | SORT_DESCENDING);126return sorted.at<float>(K,0);127}128129float HausdorffDistanceExtractorImpl::computeDistance(InputArray contour1, InputArray contour2)130{131CV_INSTRUMENT_REGION();132133Mat set1=contour1.getMat(), set2=contour2.getMat();134if (set1.type() != CV_32F)135set1.convertTo(set1, CV_32F);136if (set2.type() != CV_32F)137set2.convertTo(set2, CV_32F);138CV_Assert((set1.channels()==2) && (set1.cols>0));139CV_Assert((set2.channels()==2) && (set2.cols>0));140141// Force vectors column-based142if (set1.dims > 1)143set1 = set1.reshape(2, 1);144if (set2.dims > 1)145set2 = set2.reshape(2, 1);146147return std::max( _apply(set1, set2, distanceFlag, rankProportion),148_apply(set2, set1, distanceFlag, rankProportion) );149}150151Ptr <HausdorffDistanceExtractor> createHausdorffDistanceExtractor(int distanceFlag, float rankProp)152{153return Ptr<HausdorffDistanceExtractor>(new HausdorffDistanceExtractorImpl(distanceFlag, rankProp));154}155156} // cv157158159