You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.7 KiB

1 year ago
  1. /**
  2. * @file IWarper.h
  3. * @brief Image and landmarks warping
  4. * @copyright VisionLabs LLC
  5. * @date 04.10.2017
  6. * */
  7. #pragma once
  8. #include "IDescriptor.h"
  9. #include <fsdk/Types/Span.h>
  10. #include <fsdk/vlc/future.h>
  11. namespace fsdk {
  12. struct EyesEstimation;
  13. struct GazeEstimation;
  14. }
  15. namespace fsdk {
  16. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  17. DECLARE_SMARTPTR(IWarper);
  18. #endif
  19. /**
  20. * @brief Transformation data structure, used for warping.
  21. * @details Use this structure to perform image and landmarks transformations.
  22. * @note In no circumstances should you create OR edit this structure by yourself
  23. * */
  24. struct Transformation {
  25. Vector2<float> centerP = {0, 0}; //From 0, scaled
  26. float angleDeg = 0;
  27. float scale = 0;
  28. Vector2<int> detectionTopLeft = {0, 0}; //From 0, original size
  29. bool isValid = false;
  30. };
  31. /**
  32. * @brief Face detection area warper interface.
  33. * @details Perform affine transformations on an image to properly align depicted face for descriptor extraction.
  34. * */
  35. struct IWarper : IRefCounted {
  36. /**
  37. * @brief Create transformation data struct.
  38. * @param [in] detection detection coordinates in image space.
  39. * @param [in] landmarks vector of landmarks to calculate warping angles.
  40. * @return Transformation object.
  41. * @see Transformation, Detection and Landmarks5 for details.
  42. * */
  43. virtual Transformation
  44. createTransformation(
  45. const Detection& detection,
  46. const Landmarks5& landmarks) const noexcept = 0;
  47. /**
  48. * @brief Warp image.
  49. * @param [in] image source image.
  50. * @param [in] transformation transformation data.
  51. * @param [out] transformedImage output transformed image.
  52. * @return Result with error code.
  53. * @see Transformation, Image, Result and FSDKError for details.
  54. * @note image format must be R8G8B8, @see Format.
  55. * */
  56. virtual Result<FSDKError>
  57. warp(
  58. const Image& image,
  59. const Transformation& transformation,
  60. Image& transformedImage) const noexcept = 0;
  61. /**
  62. * @brief Warp faces on multiple images.
  63. * @param [in] images span of source images.
  64. * @param [in] transformations span of transformation structures.
  65. * @param [in/out] transformedImages span of output warped images.
  66. * @note: all input images must have the same memory residence.
  67. * @note: Each entry index in transformations span corresponds to the same index in images span
  68. * to make warps from(i.e. transformations[i] <--> images[i]), so if there are multiple warps on
  69. * the same image then this image must be duplicated in images span in order for the first statement
  70. * to hold true.
  71. * @note: if images in transformedImages span has the same parameters as the standard
  72. * warped image has then no memory will be allocated and output data will be just copied
  73. * to the backing memory of the output images. New images will be allocated otherwise.
  74. * @return Result with error code.
  75. * @see Span, Transformation, Image, Result and FSDKError for details.
  76. * @note images format must be R8G8B8, @see Format.
  77. * @note all spans should be based on user owned continuous collections.
  78. * @note all spans should be equal size.
  79. * */
  80. virtual Result<FSDKError>
  81. warp(
  82. Span<const Image> images,
  83. Span<const Transformation> transformations,
  84. Span<Image> transformedImages) const noexcept = 0;
  85. /**
  86. * @brief Warp landmarks of size 5.
  87. * @param [in] landmarks landmarks array of size 5
  88. * @param [in] transformation transformation data.
  89. * @param [out] transformedLandmarks output transformed landmarks.
  90. * If transformation is created with detection, it's in the coordinates of transformed warped image,
  91. * otherwise in the coordinates of detection.
  92. * @return Result with error code.
  93. * @see Transformation, Landmarks5, Result and FSDKError for details.
  94. * */
  95. virtual Result<FSDKError>
  96. warp(
  97. const Landmarks5& landmarks,
  98. const Transformation& transformation,
  99. Landmarks5& transformedLandmarks) const noexcept = 0;
  100. /**
  101. * @brief Warp landmarks of size 68.
  102. * @param [in] landmarks68 landmarks to warp.
  103. * @param [in] transformation transformation data.
  104. * @param [out] transformedLandmarks68 output transformed landmarks of size 68.
  105. * If transformation is created with detection, it's in the coordinates of transformed warped image,
  106. * otherwise in the coordinates of detection.
  107. * @return Result with error code.
  108. * @see Transformation, Landmarks68, Result and FSDKError for details.
  109. * */
  110. virtual Result<FSDKError>
  111. warp(
  112. const Landmarks68& landmarks68,
  113. const Transformation& transformation,
  114. Landmarks68& transformedLandmarks68) const noexcept = 0;
  115. /**
  116. * @brief Warp irisLandmarks in EyesEstimation struct for both eyes.
  117. * @details Warps from warped image coord. space to source image coordinates space,
  118. * the one that was used to create transformation.
  119. * @param [in] eyesEstimationInWarpCoordinates EyesEstimation straight out of EyeEstimator.
  120. * Should be create from warpedImage, that was created with the same transformation object as the one passed.
  121. * @param [in] transformation transformation data.
  122. * @param [out] eyesEstimation eyes estimation with iris landmarks warped to source image coordinates space.
  123. * @return Result with error code.
  124. * @see Transformation, EyesEstimation, Result and FSDKError for details.
  125. * */
  126. virtual Result<FSDKError>
  127. unwarp(
  128. const EyesEstimation& eyesEstimationInWarpCoordinates,
  129. const Transformation& transformation,
  130. EyesEstimation& eyesEstimation) const noexcept = 0;
  131. /**
  132. * @brief Warp landmarks of size 5 back to source image coordinates.
  133. * @param [in] warpedLandmarks5 warped landmarks array of size 5.
  134. * @param [in] transformation transformation data.
  135. * @param [out] landmarks5 landmarks of size 5 warped back to source image coordinates.
  136. * @return Result with error code.
  137. * @see Transformation, Landmarks5, Result and FSDKError for details.
  138. * */
  139. virtual Result<FSDKError>
  140. unwarp(
  141. const Landmarks5& warpedLandmarks5,
  142. const Transformation& transformation,
  143. Landmarks5& landmarks5) const noexcept = 0;
  144. /**
  145. * @brief Warp landmarks of size 68 back to source image coordinates.
  146. * @param [in] warpedLandmarks5 warped landmarks array of size 68.
  147. * @param [in] transformation transformation data.
  148. * @param [out] landmarks5 landmarks of size 68 warped back to source image coordinates.
  149. * @return Result with error code.
  150. * @see Transformation, Landmarks68, Result and FSDKError for details.
  151. * */
  152. virtual Result<FSDKError>
  153. unwarp(
  154. const Landmarks68& warpedLandmarks68,
  155. const Transformation& transformation,
  156. Landmarks68& landmarks68) const noexcept = 0;
  157. virtual Result<FSDKError>
  158. unwarp(
  159. const GazeEstimation& warpedAngles,
  160. const Transformation& transformation,
  161. GazeEstimation& angles
  162. ) const noexcept = 0;
  163. /**
  164. * @brief Common aliases for IWarper asynchronous interface.
  165. * */
  166. using ImageBatch = std::vector<Image>;
  167. using ImageBatchFuture = vlc::future<ImageBatch>;
  168. /**
  169. * @brief Asynchronously warp faces on multiple images.
  170. * @param [in] images span of source images.
  171. * @param [in] transformations span of transformation structures.
  172. * @return Future result object with error code and warped image batch.
  173. * @see Span, Transformation, Image, Result and FSDKError for details.
  174. * @note images format must be R8G8B8, @see Format.
  175. * @note all spans should be based on user owned continuous collections.
  176. * @note all spans should be equal size.
  177. * @note this method is experimental and interface may be changed in the future.
  178. * @note this method is not marked as noexcept and may throw an exception.
  179. * */
  180. virtual ImageBatchFuture warpAsync(
  181. Span<const Image> images,
  182. Span<const Transformation> transformations) const = 0;
  183. };
  184. }