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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. * Class position, used for store robot coordinates
  42. *
  43. * @brief Class position, used for store robot coordinates
  44. */
  45. class Position {
  46. public:
  47. int robotId;
  48. float angle;
  49. cv::Point2f center;
  50. cv::Point2f direction;
  51. /**
  52. * Constructor of Position object
  53. */
  54. Position() {
  55. robotId = -1;
  56. angle = 0.0;
  57. center=cv::Point2f(-1.0,-1.0); //Remettre 0.0 si jamais
  58. direction=cv::Point2f(0.0,0.0);
  59. }
  60. /**
  61. * Build a string representation of the object
  62. * @return String containing object information
  63. */
  64. string ToString() {
  65. return "Id=" + to_string(this->robotId) + ", Angle=" + to_string(this->angle) + ", center=(" + to_string(this->center.x) + ";" + to_string(this->center.y) + ")";
  66. }
  67. };
  68. /**
  69. * Class arena, used for holding outline of arena on image and cropping image to only usefull area
  70. *
  71. * @brief Class arena, used for holding outline of arena on image and cropping image to only usefull area
  72. */
  73. class Arena {
  74. public:
  75. /**
  76. * Coordinate of arena, empty if no arena found
  77. */
  78. cv::Rect arena;
  79. /**
  80. * Constructor of Arena object
  81. */
  82. Arena() {}
  83. /**
  84. * Tell if arena is empty (not found) or not
  85. * @return true if no arena found, false otherwise
  86. */
  87. bool IsEmpty();
  88. /**
  89. * Build a string representation of the object
  90. * @return String containing object information
  91. */
  92. string ToString() {
  93. if (IsEmpty()) return "Arena empty";
  94. else
  95. return "Arena: (x;y)=(" + to_string(this->arena.x) + ";" + to_string(this->arena.x) + " (w;h)=(" + to_string(this->arena.width) + ";" + to_string(this->arena.height) + ")";
  96. }
  97. };
  98. /**
  99. * Class for image storage and treatment
  100. *
  101. * @brief Class for image storage and treatment
  102. */
  103. class Img {
  104. public:
  105. /**
  106. * Image data
  107. */
  108. ImageMat img;
  109. /**
  110. * Create new Img object based on image data
  111. *
  112. * @param imgMatrice Image data to be stored (raw data)
  113. */
  114. Img(ImageMat imgMatrice);
  115. /**
  116. * Convert object to a string representation
  117. *
  118. * @return String containing information on contained image (size and number of channel)
  119. */
  120. string ToString();
  121. /**
  122. * Create a copy of current object
  123. *
  124. * @return New Img object, copy of current
  125. */
  126. Img* Copy();
  127. /**
  128. * Compress current image to JPEG
  129. * @return Image compressed as JPEG
  130. */
  131. Jpg ToJpg();
  132. /**
  133. * Search arena outline in current image
  134. * @return Arena object with coordinate of outline, empty if no arena found
  135. */
  136. Arena SearchArena();
  137. /**
  138. * Draw an oriented arrow at robot position
  139. * @param robot Position of robot
  140. */
  141. void DrawRobot(Position robot);
  142. /**
  143. * Draw an oriented arrow for each position provided
  144. * @param robots List of robot positions
  145. * @return Number of position drawn
  146. */
  147. int DrawAllRobots(std::list<Position> robots);
  148. /**
  149. * Draw arena outline
  150. * @param arenaToDraw Arena position
  151. */
  152. void DrawArena(Arena arenaToDraw);
  153. /**
  154. * Search available robots in an image
  155. * @param arena Arena position for cropping image
  156. * @return list of position, empty if no robot found
  157. */
  158. std::list<Position> SearchRobot(Arena arena);
  159. #ifdef __WITH_ARUCO__
  160. /**
  161. * Dictionary to be used for aruco recognition
  162. */
  163. cv::Ptr<cv::aruco::Dictionary> dictionary;
  164. #endif // __WITH_ARUCO__
  165. private:
  166. #ifdef __WITH_ARUCO__
  167. /**
  168. * Find center point of given aruco
  169. * @param aruco Aruco coordinates
  170. * @return Center point coordinate
  171. */
  172. cv::Point2f FindArucoCenter(std::vector<cv::Point2f> aruco);
  173. /**
  174. * Find direction of given aruco
  175. * @param aruco Aruco coordinates
  176. * @return Orientation of aruco
  177. */
  178. cv::Point2f FindArucoDirection(std::vector<cv::Point2f> aruco);
  179. #endif // __WITH_ARUCO__
  180. /**
  181. * Function for computing angle
  182. * @param robots Position of robot
  183. * @return Angle
  184. */
  185. float CalculAngle(Position robots);
  186. /**
  187. * Function for computing angle
  188. * @param pt1 ???
  189. * @param pt2 ???
  190. * @return Angle
  191. */
  192. float CalculAngle2(cv::Point2f pt1, cv::Point2f pt2);
  193. /**
  194. * Used for computing distance
  195. * @param p ???
  196. * @param q ???
  197. * @return Distance
  198. */
  199. float EuclideanDistance(cv::Point2f p, cv::Point2f q);
  200. /**
  201. * Crop image around detected arena
  202. * @param arena Coordinate of arena
  203. * @return Reduced image, focused on arena
  204. */
  205. ImageMat CropArena(Arena arena);
  206. };
  207. #endif //__IMG_H__