motempl.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #ifndef __OPENCV_OPTFLOW_MOTEMPL_HPP__
  32. #define __OPENCV_OPTFLOW_MOTEMPL_HPP__
  33. #include "opencv2/core.hpp"
  34. namespace cv
  35. {
  36. namespace motempl
  37. {
  38. //! @addtogroup optflow
  39. //! @{
  40. /** @brief Updates the motion history image by a moving silhouette.
  41. @param silhouette Silhouette mask that has non-zero pixels where the motion occurs.
  42. @param mhi Motion history image that is updated by the function (single-channel, 32-bit
  43. floating-point).
  44. @param timestamp Current time in milliseconds or other units.
  45. @param duration Maximal duration of the motion track in the same units as timestamp .
  46. The function updates the motion history image as follows:
  47. \f[\texttt{mhi} (x,y)= \forkthree{\texttt{timestamp}}{if \(\texttt{silhouette}(x,y) \ne 0\)}{0}{if \(\texttt{silhouette}(x,y) = 0\) and \(\texttt{mhi} < (\texttt{timestamp} - \texttt{duration})\)}{\texttt{mhi}(x,y)}{otherwise}\f]
  48. That is, MHI pixels where the motion occurs are set to the current timestamp , while the pixels
  49. where the motion happened last time a long time ago are cleared.
  50. The function, together with calcMotionGradient and calcGlobalOrientation , implements a motion
  51. templates technique described in @cite Davis97 and @cite Bradski00 .
  52. */
  53. CV_EXPORTS_W void updateMotionHistory( InputArray silhouette, InputOutputArray mhi,
  54. double timestamp, double duration );
  55. /** @brief Calculates a gradient orientation of a motion history image.
  56. @param mhi Motion history single-channel floating-point image.
  57. @param mask Output mask image that has the type CV_8UC1 and the same size as mhi . Its non-zero
  58. elements mark pixels where the motion gradient data is correct.
  59. @param orientation Output motion gradient orientation image that has the same type and the same
  60. size as mhi . Each pixel of the image is a motion orientation, from 0 to 360 degrees.
  61. @param delta1 Minimal (or maximal) allowed difference between mhi values within a pixel
  62. neighborhood.
  63. @param delta2 Maximal (or minimal) allowed difference between mhi values within a pixel
  64. neighborhood. That is, the function finds the minimum ( \f$m(x,y)\f$ ) and maximum ( \f$M(x,y)\f$ ) mhi
  65. values over \f$3 \times 3\f$ neighborhood of each pixel and marks the motion orientation at \f$(x, y)\f$
  66. as valid only if
  67. \f[\min ( \texttt{delta1} , \texttt{delta2} ) \le M(x,y)-m(x,y) \le \max ( \texttt{delta1} , \texttt{delta2} ).\f]
  68. @param apertureSize Aperture size of the Sobel operator.
  69. The function calculates a gradient orientation at each pixel \f$(x, y)\f$ as:
  70. \f[\texttt{orientation} (x,y)= \arctan{\frac{d\texttt{mhi}/dy}{d\texttt{mhi}/dx}}\f]
  71. In fact, fastAtan2 and phase are used so that the computed angle is measured in degrees and covers
  72. the full range 0..360. Also, the mask is filled to indicate pixels where the computed angle is
  73. valid.
  74. @note
  75. - (Python) An example on how to perform a motion template technique can be found at
  76. opencv_source_code/samples/python2/motempl.py
  77. */
  78. CV_EXPORTS_W void calcMotionGradient( InputArray mhi, OutputArray mask, OutputArray orientation,
  79. double delta1, double delta2, int apertureSize = 3 );
  80. /** @brief Calculates a global motion orientation in a selected region.
  81. @param orientation Motion gradient orientation image calculated by the function calcMotionGradient
  82. @param mask Mask image. It may be a conjunction of a valid gradient mask, also calculated by
  83. calcMotionGradient , and the mask of a region whose direction needs to be calculated.
  84. @param mhi Motion history image calculated by updateMotionHistory .
  85. @param timestamp Timestamp passed to updateMotionHistory .
  86. @param duration Maximum duration of a motion track in milliseconds, passed to updateMotionHistory
  87. The function calculates an average motion direction in the selected region and returns the angle
  88. between 0 degrees and 360 degrees. The average direction is computed from the weighted orientation
  89. histogram, where a recent motion has a larger weight and the motion occurred in the past has a
  90. smaller weight, as recorded in mhi .
  91. */
  92. CV_EXPORTS_W double calcGlobalOrientation( InputArray orientation, InputArray mask, InputArray mhi,
  93. double timestamp, double duration );
  94. /** @brief Splits a motion history image into a few parts corresponding to separate independent motions (for
  95. example, left hand, right hand).
  96. @param mhi Motion history image.
  97. @param segmask Image where the found mask should be stored, single-channel, 32-bit floating-point.
  98. @param boundingRects Vector containing ROIs of motion connected components.
  99. @param timestamp Current time in milliseconds or other units.
  100. @param segThresh Segmentation threshold that is recommended to be equal to the interval between
  101. motion history "steps" or greater.
  102. The function finds all of the motion segments and marks them in segmask with individual values
  103. (1,2,...). It also computes a vector with ROIs of motion connected components. After that the motion
  104. direction for every component can be calculated with calcGlobalOrientation using the extracted mask
  105. of the particular component.
  106. */
  107. CV_EXPORTS_W void segmentMotion( InputArray mhi, OutputArray segmask,
  108. CV_OUT std::vector<Rect>& boundingRects,
  109. double timestamp, double segThresh );
  110. //! @}
  111. }
  112. }
  113. #endif