No Description
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.

img.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef __IMG_H__
  18. #define __IMG_H__
  19. #include <iostream>
  20. #include <list>
  21. #include <string>
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. #include <opencv2/core.hpp>
  25. #ifdef __WITH_ARUCO__
  26. #include <opencv2/aruco/dictionary.hpp>
  27. #include <opencv2/aruco/charuco.hpp>
  28. #include <opencv2/core/mat.hpp>
  29. #endif // __WITH_ARUCO__
  30. #define ARENA_NOT_DETECTED -1
  31. using namespace std;
  32. /**
  33. * Redefinition of cv::Mat type
  34. */
  35. typedef cv::Mat ImageMat;
  36. /**
  37. * Declaration of Jpg type
  38. */
  39. typedef vector<unsigned char> Jpg;
  40. /**
  41. * Position type used for store robot coordinates
  42. *
  43. * @brief Position type used for store robot coordinates
  44. */
  45. typedef struct {
  46. cv::Point2f center;
  47. cv::Point2f direction;
  48. float angle;
  49. int robotId;
  50. } Position;
  51. /**
  52. * Class arena, used for holding outline of arena on image and cropping image to only usefull area
  53. *
  54. * @brief Class arena, used for holding outline of arena on image and cropping image to only usefull area
  55. */
  56. class Arena {
  57. public:
  58. /**
  59. * Constructor of Arena object
  60. */
  61. Arena() {}
  62. /**
  63. * Coordinate of arena, empty if no arena found
  64. */
  65. cv::Rect arena;
  66. /**
  67. * Tell if arena is empty (not found) or not
  68. * @return true if no arena found, false otherwise
  69. */
  70. bool IsEmpty();
  71. };
  72. /**
  73. * Class for image storage and treatment
  74. *
  75. * @brief Class for image storage and treatment
  76. */
  77. class Img {
  78. public:
  79. /**
  80. * Image data
  81. */
  82. ImageMat img;
  83. /**
  84. * Create new Img object based on image data
  85. *
  86. * @param imgMatrice Image data to be stored (raw data)
  87. */
  88. Img(ImageMat imgMatrice);
  89. /**
  90. * Convert object to a string representation
  91. *
  92. * @return String containing information on contained image (size and number of channel)
  93. */
  94. string ToString();
  95. /**
  96. * Create a copy of current object
  97. *
  98. * @return New Img object, copy of current
  99. */
  100. Img* Copy();
  101. /**
  102. * Compress current image to JPEG
  103. * @return Image compressed as JPEG
  104. */
  105. Jpg ToJpg();
  106. /**
  107. * Search arena outline in current image
  108. * @return Arena object with coordinate of outline, empty if no arena found
  109. */
  110. Arena SearchArena();
  111. /**
  112. * Draw an oriented arrow at robot position
  113. * @param robot Position of robot
  114. */
  115. void DrawRobot(Position robot);
  116. /**
  117. * Draw an oriented arrow for each position provided
  118. * @param robots List of robot positions
  119. * @return Number of position drawn
  120. */
  121. int DrawAllRobots(std::list<Position> robots);
  122. /**
  123. * Draw arena outline
  124. * @param arenaToDraw Arena position
  125. */
  126. void DrawArena(Arena arenaToDraw);
  127. /**
  128. * Search available robots in an image
  129. * @param arena Arena position for cropping image
  130. * @return list of position, empty if no robot found
  131. */
  132. std::list<Position> SearchRobot(Arena arena);
  133. #ifdef __WITH_ARUCO__
  134. /**
  135. * Dictionary to be used for aruco recognition
  136. */
  137. cv::Ptr<cv::aruco::Dictionary> dictionary;
  138. #endif // __WITH_ARUCO__
  139. private:
  140. #ifdef __WITH_ARUCO__
  141. /**
  142. * Find center point of given aruco
  143. * @param aruco Aruco coordinates
  144. * @return Center point coordinate
  145. */
  146. cv::Point2f FindArucoCenter(std::vector<cv::Point2f> aruco);
  147. /**
  148. * Find direction of given aruco
  149. * @param aruco Aruco coordinates
  150. * @return Orientation of aruco
  151. */
  152. cv::Point2f FindArucoDirection(std::vector<cv::Point2f> aruco);
  153. #endif // __WITH_ARUCO__
  154. /**
  155. * Function for computing angle
  156. * @param robots Position of robot
  157. * @return Angle
  158. */
  159. float CalculAngle(Position robots);
  160. /**
  161. * Function for computing angle
  162. * @param pt1 ???
  163. * @param pt2 ???
  164. * @return Angle
  165. */
  166. float CalculAngle2(cv::Point2f pt1, cv::Point2f pt2);
  167. /**
  168. * Used for computing distance
  169. * @param p ???
  170. * @param q ???
  171. * @return Distance
  172. */
  173. float EuclideanDistance(cv::Point2f p, cv::Point2f q);
  174. /**
  175. * Crop image around detected arena
  176. * @param arena Coordinate of arena
  177. * @return Reduced image, focused on arena
  178. */
  179. ImageMat CropArena(Arena arena);
  180. };
  181. #endif //__IMG_H__