shape_distance.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_SHAPE_SHAPE_DISTANCE_HPP
  44. #define OPENCV_SHAPE_SHAPE_DISTANCE_HPP
  45. #include "opencv2/core.hpp"
  46. #include "opencv2/shape/hist_cost.hpp"
  47. #include "opencv2/shape/shape_transformer.hpp"
  48. namespace cv
  49. {
  50. //! @addtogroup shape
  51. //! @{
  52. /** @example samples/cpp/shape_example.cpp
  53. An example using shape distance algorithm
  54. */
  55. /** @brief Abstract base class for shape distance algorithms.
  56. */
  57. class CV_EXPORTS_W ShapeDistanceExtractor : public Algorithm
  58. {
  59. public:
  60. /** @brief Compute the shape distance between two shapes defined by its contours.
  61. @param contour1 Contour defining first shape.
  62. @param contour2 Contour defining second shape.
  63. */
  64. CV_WRAP virtual float computeDistance(InputArray contour1, InputArray contour2) = 0;
  65. };
  66. /***********************************************************************************/
  67. /***********************************************************************************/
  68. /***********************************************************************************/
  69. /** @brief Implementation of the Shape Context descriptor and matching algorithm
  70. proposed by Belongie et al. in "Shape Matching and Object Recognition Using Shape Contexts" (PAMI
  71. 2002). This implementation is packaged in a generic scheme, in order to allow you the
  72. implementation of the common variations of the original pipeline.
  73. */
  74. class CV_EXPORTS_W ShapeContextDistanceExtractor : public ShapeDistanceExtractor
  75. {
  76. public:
  77. /** @brief Establish the number of angular bins for the Shape Context Descriptor used in the shape matching
  78. pipeline.
  79. @param nAngularBins The number of angular bins in the shape context descriptor.
  80. */
  81. CV_WRAP virtual void setAngularBins(int nAngularBins) = 0;
  82. CV_WRAP virtual int getAngularBins() const = 0;
  83. /** @brief Establish the number of radial bins for the Shape Context Descriptor used in the shape matching
  84. pipeline.
  85. @param nRadialBins The number of radial bins in the shape context descriptor.
  86. */
  87. CV_WRAP virtual void setRadialBins(int nRadialBins) = 0;
  88. CV_WRAP virtual int getRadialBins() const = 0;
  89. /** @brief Set the inner radius of the shape context descriptor.
  90. @param innerRadius The value of the inner radius.
  91. */
  92. CV_WRAP virtual void setInnerRadius(float innerRadius) = 0;
  93. CV_WRAP virtual float getInnerRadius() const = 0;
  94. /** @brief Set the outer radius of the shape context descriptor.
  95. @param outerRadius The value of the outer radius.
  96. */
  97. CV_WRAP virtual void setOuterRadius(float outerRadius) = 0;
  98. CV_WRAP virtual float getOuterRadius() const = 0;
  99. CV_WRAP virtual void setRotationInvariant(bool rotationInvariant) = 0;
  100. CV_WRAP virtual bool getRotationInvariant() const = 0;
  101. /** @brief Set the weight of the shape context distance in the final value of the shape distance. The shape
  102. context distance between two shapes is defined as the symmetric sum of shape context matching costs
  103. over best matching points. The final value of the shape distance is a user-defined linear
  104. combination of the shape context distance, an image appearance distance, and a bending energy.
  105. @param shapeContextWeight The weight of the shape context distance in the final distance value.
  106. */
  107. CV_WRAP virtual void setShapeContextWeight(float shapeContextWeight) = 0;
  108. CV_WRAP virtual float getShapeContextWeight() const = 0;
  109. /** @brief Set the weight of the Image Appearance cost in the final value of the shape distance. The image
  110. appearance cost is defined as the sum of squared brightness differences in Gaussian windows around
  111. corresponding image points. The final value of the shape distance is a user-defined linear
  112. combination of the shape context distance, an image appearance distance, and a bending energy. If
  113. this value is set to a number different from 0, is mandatory to set the images that correspond to
  114. each shape.
  115. @param imageAppearanceWeight The weight of the appearance cost in the final distance value.
  116. */
  117. CV_WRAP virtual void setImageAppearanceWeight(float imageAppearanceWeight) = 0;
  118. CV_WRAP virtual float getImageAppearanceWeight() const = 0;
  119. /** @brief Set the weight of the Bending Energy in the final value of the shape distance. The bending energy
  120. definition depends on what transformation is being used to align the shapes. The final value of the
  121. shape distance is a user-defined linear combination of the shape context distance, an image
  122. appearance distance, and a bending energy.
  123. @param bendingEnergyWeight The weight of the Bending Energy in the final distance value.
  124. */
  125. CV_WRAP virtual void setBendingEnergyWeight(float bendingEnergyWeight) = 0;
  126. CV_WRAP virtual float getBendingEnergyWeight() const = 0;
  127. /** @brief Set the images that correspond to each shape. This images are used in the calculation of the Image
  128. Appearance cost.
  129. @param image1 Image corresponding to the shape defined by contours1.
  130. @param image2 Image corresponding to the shape defined by contours2.
  131. */
  132. CV_WRAP virtual void setImages(InputArray image1, InputArray image2) = 0;
  133. CV_WRAP virtual void getImages(OutputArray image1, OutputArray image2) const = 0;
  134. CV_WRAP virtual void setIterations(int iterations) = 0;
  135. CV_WRAP virtual int getIterations() const = 0;
  136. /** @brief Set the algorithm used for building the shape context descriptor cost matrix.
  137. @param comparer Smart pointer to a HistogramCostExtractor, an algorithm that defines the cost
  138. matrix between descriptors.
  139. */
  140. CV_WRAP virtual void setCostExtractor(Ptr<HistogramCostExtractor> comparer) = 0;
  141. CV_WRAP virtual Ptr<HistogramCostExtractor> getCostExtractor() const = 0;
  142. /** @brief Set the value of the standard deviation for the Gaussian window for the image appearance cost.
  143. @param sigma Standard Deviation.
  144. */
  145. CV_WRAP virtual void setStdDev(float sigma) = 0;
  146. CV_WRAP virtual float getStdDev() const = 0;
  147. /** @brief Set the algorithm used for aligning the shapes.
  148. @param transformer Smart pointer to a ShapeTransformer, an algorithm that defines the aligning
  149. transformation.
  150. */
  151. CV_WRAP virtual void setTransformAlgorithm(Ptr<ShapeTransformer> transformer) = 0;
  152. CV_WRAP virtual Ptr<ShapeTransformer> getTransformAlgorithm() const = 0;
  153. };
  154. /* Complete constructor */
  155. CV_EXPORTS_W Ptr<ShapeContextDistanceExtractor>
  156. createShapeContextDistanceExtractor(int nAngularBins=12, int nRadialBins=4,
  157. float innerRadius=0.2f, float outerRadius=2, int iterations=3,
  158. const Ptr<HistogramCostExtractor> &comparer = createChiHistogramCostExtractor(),
  159. const Ptr<ShapeTransformer> &transformer = createThinPlateSplineShapeTransformer());
  160. /***********************************************************************************/
  161. /***********************************************************************************/
  162. /***********************************************************************************/
  163. /** @brief A simple Hausdorff distance measure between shapes defined by contours
  164. according to the paper "Comparing Images using the Hausdorff distance." by D.P. Huttenlocher, G.A.
  165. Klanderman, and W.J. Rucklidge. (PAMI 1993). :
  166. */
  167. class CV_EXPORTS_W HausdorffDistanceExtractor : public ShapeDistanceExtractor
  168. {
  169. public:
  170. /** @brief Set the norm used to compute the Hausdorff value between two shapes. It can be L1 or L2 norm.
  171. @param distanceFlag Flag indicating which norm is used to compute the Hausdorff distance
  172. (NORM_L1, NORM_L2).
  173. */
  174. CV_WRAP virtual void setDistanceFlag(int distanceFlag) = 0;
  175. CV_WRAP virtual int getDistanceFlag() const = 0;
  176. /** @brief This method sets the rank proportion (or fractional value) that establish the Kth ranked value of
  177. the partial Hausdorff distance. Experimentally had been shown that 0.6 is a good value to compare
  178. shapes.
  179. @param rankProportion fractional value (between 0 and 1).
  180. */
  181. CV_WRAP virtual void setRankProportion(float rankProportion) = 0;
  182. CV_WRAP virtual float getRankProportion() const = 0;
  183. };
  184. /* Constructor */
  185. CV_EXPORTS_W Ptr<HausdorffDistanceExtractor> createHausdorffDistanceExtractor(int distanceFlag=cv::NORM_L2, float rankProp=0.6f);
  186. //! @}
  187. } // cv
  188. #endif