/*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// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include <stdlib.h>43#include <math.h>44#include <vector>4546namespace cv47{48/*49* ShapeContextDescriptor class50*/51class SCD52{53public:54//! the full constructor taking all the necessary parameters55explicit SCD(int _nAngularBins=12, int _nRadialBins=5,56double _innerRadius=0.1, double _outerRadius=1, bool _rotationInvariant=false)57{58setAngularBins(_nAngularBins);59setRadialBins(_nRadialBins);60setInnerRadius(_innerRadius);61setOuterRadius(_outerRadius);62setRotationInvariant(_rotationInvariant);63meanDistance = 0;64}6566void extractSCD(cv::Mat& contour, cv::Mat& descriptors,67const std::vector<int>& queryInliers=std::vector<int>(),68const float _meanDistance=-1);6970int descriptorSize() {return nAngularBins*nRadialBins;}71void setAngularBins(int angularBins) { nAngularBins=angularBins; }72void setRadialBins(int radialBins) { nRadialBins=radialBins; }73void setInnerRadius(double _innerRadius) { innerRadius=_innerRadius; }74void setOuterRadius(double _outerRadius) { outerRadius=_outerRadius; }75void setRotationInvariant(bool _rotationInvariant) { rotationInvariant=_rotationInvariant; }76int getAngularBins() const { return nAngularBins; }77int getRadialBins() const { return nRadialBins; }78double getInnerRadius() const { return innerRadius; }79double getOuterRadius() const { return outerRadius; }80bool getRotationInvariant() const { return rotationInvariant; }81float getMeanDistance() const { return meanDistance; }8283private:84int nAngularBins;85int nRadialBins;86double innerRadius;87double outerRadius;88bool rotationInvariant;89float meanDistance;9091protected:92void logarithmicSpaces(std::vector<double>& vecSpaces) const;93void angularSpaces(std::vector<double>& vecSpaces) const;9495void buildNormalizedDistanceMatrix(cv::Mat& contour,96cv::Mat& disMatrix, const std::vector<int> &queryInliers,97const float _meanDistance=-1);9899void buildAngleMatrix(cv::Mat& contour,100cv::Mat& angleMatrix) const;101};102103/*104* Matcher105*/106class SCDMatcher107{108public:109// the full constructor110SCDMatcher() : minMatchCost(0)111{112}113114// the matcher function using Hungarian method115void matchDescriptors(cv::Mat& descriptors1, cv::Mat& descriptors2, std::vector<cv::DMatch>& matches, cv::Ptr<cv::HistogramCostExtractor>& comparer,116std::vector<int>& inliers1, std::vector<int> &inliers2);117118// matching cost119float getMatchingCost() const {return minMatchCost;}120121private:122float minMatchCost;123protected:124void buildCostMatrix(const cv::Mat& descriptors1, const cv::Mat& descriptors2,125cv::Mat& costMatrix, cv::Ptr<cv::HistogramCostExtractor>& comparer) const;126void hungarian(cv::Mat& costMatrix, std::vector<cv::DMatch>& outMatches, std::vector<int> &inliers1,127std::vector<int> &inliers2, int sizeScd1=0, int sizeScd2=0);128129};130131}132133134