fourier_descriptors.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef __OPENCV_FOURIERDESCRIPTORS_HPP__
  5. #define __OPENCV_FOURIERDESCRIPTORS_HPP__
  6. #include <opencv2/core.hpp>
  7. namespace cv {
  8. namespace ximgproc {
  9. //! @addtogroup ximgproc_fourier
  10. //! @{
  11. /** @brief Class for ContourFitting algorithms.
  12. ContourFitting match two contours \f$ z_a \f$ and \f$ z_b \f$ minimizing distance
  13. \f[ d(z_a,z_b)=\sum (a_n - s b_n e^{j(n \alpha +\phi )})^2 \f] where \f$ a_n \f$ and \f$ b_n \f$ are Fourier descriptors of \f$ z_a \f$ and \f$ z_b \f$ and s is a scaling factor and \f$ \phi \f$ is angle rotation and \f$ \alpha \f$ is starting point factor adjustement
  14. */
  15. class CV_EXPORTS_W ContourFitting : public Algorithm
  16. {
  17. int ctrSize;
  18. int fdSize;
  19. std::vector<std::complex<double> > b;
  20. std::vector<std::complex<double> > a;
  21. std::vector<double> frequence;
  22. std::vector<double> rho, psi;
  23. void frequencyInit();
  24. void fAlpha(double x, double &fn, double &df);
  25. double distance(std::complex<double> r, double alpha);
  26. double newtonRaphson(double x1, double x2);
  27. public:
  28. /** @brief Fit two closed curves using fourier descriptors. More details in @cite PersoonFu1977 and @cite BergerRaghunathan1998
  29. * @param ctr number of Fourier descriptors equal to number of contour points after resampling.
  30. * @param fd Contour defining second shape (Target).
  31. */
  32. ContourFitting(int ctr=1024,int fd=16):ctrSize(ctr),fdSize(fd){};
  33. /** @brief Fit two closed curves using fourier descriptors. More details in @cite PersoonFu1977 and @cite BergerRaghunathan1998
  34. @param src Contour defining first shape.
  35. @param dst Contour defining second shape (Target).
  36. @param alphaPhiST : \f$ \alpha \f$=alphaPhiST(0,0), \f$ \phi \f$=alphaPhiST(0,1) (in radian), s=alphaPhiST(0,2), Tx=alphaPhiST(0,3), Ty=alphaPhiST(0,4) rotation center
  37. @param dist distance between src and dst after matching.
  38. @param fdContour false then src and dst are contours and true src and dst are fourier descriptors.
  39. */
  40. void estimateTransformation(InputArray src, InputArray dst, OutputArray alphaPhiST, double *dist = 0, bool fdContour = false);
  41. /** @brief Fit two closed curves using fourier descriptors. More details in @cite PersoonFu1977 and @cite BergerRaghunathan1998
  42. @param src Contour defining first shape.
  43. @param dst Contour defining second shape (Target).
  44. @param alphaPhiST : \f$ \alpha \f$=alphaPhiST(0,0), \f$ \phi \f$=alphaPhiST(0,1) (in radian), s=alphaPhiST(0,2), Tx=alphaPhiST(0,3), Ty=alphaPhiST(0,4) rotation center
  45. @param dist distance between src and dst after matching.
  46. @param fdContour false then src and dst are contours and true src and dst are fourier descriptors.
  47. */
  48. CV_WRAP void estimateTransformation(InputArray src, InputArray dst, OutputArray alphaPhiST, CV_OUT double &dist , bool fdContour = false);
  49. /** @brief set number of Fourier descriptors used in estimateTransformation
  50. @param n number of Fourier descriptors equal to number of contour points after resampling.
  51. */
  52. CV_WRAP void setCtrSize(int n);
  53. /** @brief set number of Fourier descriptors when estimateTransformation used vector<Point>
  54. @param n number of fourier descriptors used for optimal curve matching.
  55. */
  56. CV_WRAP void setFDSize(int n);
  57. /**
  58. @returns number of fourier descriptors
  59. */
  60. CV_WRAP int getCtrSize() { return ctrSize; };
  61. /**
  62. @returns number of fourier descriptors used for optimal curve matching
  63. */
  64. CV_WRAP int getFDSize() { return fdSize; };
  65. };
  66. /**
  67. * @brief Fourier descriptors for planed closed curves
  68. *
  69. * For more details about this implementation, please see @cite PersoonFu1977
  70. *
  71. * @param src contour type vector<Point> , vector<Point2f> or vector<Point2d>
  72. * @param dst Mat of type CV_64FC2 and nbElt rows A VERIFIER
  73. * @param nbElt number of rows in dst or getOptimalDFTSize rows if nbElt=-1
  74. * @param nbFD number of FD return in dst dst = [FD(1...nbFD/2) FD(nbFD/2-nbElt+1...:nbElt)]
  75. *
  76. */
  77. CV_EXPORTS_W void fourierDescriptor(InputArray src, OutputArray dst, int nbElt=-1,int nbFD=-1);
  78. /**
  79. * @brief transform a contour
  80. *
  81. * @param src contour or Fourier Descriptors if fd is true
  82. * @param t transform Mat given by estimateTransformation
  83. * @param dst Mat of type CV_64FC2 and nbElt rows
  84. * @param fdContour true src are Fourier Descriptors. fdContour false src is a contour
  85. *
  86. */
  87. CV_EXPORTS_W void transformFD(InputArray src, InputArray t,OutputArray dst, bool fdContour=true);
  88. /**
  89. * @brief Contour sampling .
  90. *
  91. * @param src contour type vector<Point> , vector<Point2f> or vector<Point2d>
  92. * @param out Mat of type CV_64FC2 and nbElt rows
  93. * @param nbElt number of points in out contour
  94. *
  95. */
  96. CV_EXPORTS_W void contourSampling(InputArray src, OutputArray out, int nbElt);
  97. /**
  98. * @brief create ContourFitting algorithm object
  99. *
  100. * @param ctr number of Fourier descriptors equal to number of contour points after resampling.
  101. * @param fd Contour defining second shape (Target).
  102. */
  103. CV_EXPORTS_W Ptr<ContourFitting> createContourFitting(int ctr = 1024, int fd = 16);
  104. //! @} ximgproc_fourier
  105. }
  106. }
  107. #endif