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.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "img.h"
  18. /**
  19. * Tell if arena is empty (not found) or not
  20. * @return true if no arena found, false otherwise
  21. */
  22. bool Arena::IsEmpty() {
  23. if ((this->arena.height == 0) || (this->arena.width == 0)) return true;
  24. else return false;
  25. }
  26. /**
  27. * Create new Img object based on image data
  28. *
  29. * @param imgMatrice Image data to be stored (raw data)
  30. */
  31. Img::Img(ImageMat imgMatrice) {
  32. this->img = imgMatrice.clone();
  33. }
  34. /**
  35. * Convert object to a string representation
  36. *
  37. * @return String containing information on contained image (size and number of channel)
  38. */
  39. string Img::ToString() {
  40. return "Image size: " + to_string(this->img.cols) + "x" + to_string(this->img.rows) + " (dim=" + to_string(this->img.dims) + ")";
  41. }
  42. /**
  43. * Create a copy of current object
  44. *
  45. * @return New Img object, copy of current
  46. */
  47. Img* Img::Copy() {
  48. return new Img(this->img);
  49. }
  50. /**
  51. * Function for computing angle
  52. * @param robots Position of robot
  53. * @return Angle
  54. */
  55. float Img::CalculAngle(Position robot) {
  56. float a = robot.direction.x - robot.center.x;
  57. float b = robot.direction.y - robot.center.y;
  58. float angle = atan2(b, a);
  59. return angle * 180.f / M_PI;
  60. }
  61. /**
  62. * Function for computing angle
  63. * @param pt1 ???
  64. * @param pt2 ???
  65. * @return Angle
  66. */
  67. float Img::CalculAngle2(cv::Point2f pt1, cv::Point2f pt2) {
  68. float a = pt1.x - pt2.x;
  69. float b = pt1.y - pt2.y;
  70. float angle = atan2(b, a);
  71. return angle * 180.f / M_PI;
  72. }
  73. #ifdef __WITH_ARUCO__
  74. /**
  75. * Find center point of given aruco
  76. * @param aruco Aruco coordinates
  77. * @return Center point coordinate
  78. */
  79. cv::Point2f Img::FindArucoCenter(std::vector<cv::Point2f> aruco) {
  80. return ((aruco[0] + aruco[2]) / 2);
  81. }
  82. /**
  83. * Find direction of given aruco
  84. * @param aruco Aruco coordinates
  85. * @return Orientation of aruco
  86. */
  87. cv::Point2f Img::FindArucoDirection(std::vector<cv::Point2f> aruco) {
  88. return ((aruco[0] + aruco[1]) / 2);
  89. }
  90. #endif // __WITH_ARUCO__
  91. /**
  92. * Used for computing distance
  93. * @param p ???
  94. * @param q ???
  95. * @return Distance
  96. */
  97. float Img::EuclideanDistance(cv::Point2f p, cv::Point2f q) {
  98. cv::Point diff = p - q;
  99. return cv::sqrt(diff.x * diff.x + diff.y * diff.y);
  100. }
  101. /**
  102. * Compress current image to JPEG
  103. * @return Image compressed as JPEG
  104. */
  105. Jpg Img::ToJpg() {
  106. Jpg imgJpg;
  107. cv::imencode(".jpg", this->img, imgJpg);
  108. return imgJpg;
  109. }
  110. /**
  111. * Search available robots in an image
  112. * @param arena Arena position for cropping image
  113. * @return list of position, empty if no robot found
  114. */
  115. std::list<Position> Img::SearchRobot(Arena arena) {
  116. #ifdef __WITH_ARUCO__
  117. ImageMat imgTraitment;
  118. std::list<Position> positionList;
  119. cv::Point2f areneCoor;
  120. std::vector<int> ids;
  121. std::vector<std::vector<cv::Point2f> > corners;
  122. if (arena.IsEmpty())
  123. imgTraitment = this->img.clone();
  124. else {
  125. imgTraitment = CropArena(arena);
  126. areneCoor.x = arena.arena.x;
  127. areneCoor.y = arena.arena.y;
  128. }
  129. cv::aruco::detectMarkers(imgTraitment, dictionary, corners, ids);
  130. if (ids.size() > 0) {
  131. for (int i = 0; i < ids.size(); i++) {
  132. Position newPos;
  133. newPos.center = FindArucoCenter(corners[i]);
  134. newPos.direction = FindArucoDirection(corners[i]);
  135. newPos.robotId = ids[i];
  136. if (!arena.IsEmpty()) {
  137. newPos.direction += areneCoor;
  138. newPos.center += areneCoor;
  139. }
  140. newPos.angle = CalculAngle2(newPos.center, newPos.direction);
  141. positionList.push_back(newPos);
  142. }
  143. }
  144. return positionList;
  145. #else
  146. std::list<Position> robotsFind;
  147. std::vector<std::vector<cv::Point> > contours;
  148. std::vector<cv::Point> approx;
  149. std::vector<cv::Vec4i> hierarchy;
  150. ImageMat imgTraitment;
  151. if (arena.IsEmpty()) {
  152. imgTraitment = this->img.clone();
  153. } else {
  154. imgTraitment = this->img(arena.arena);
  155. }
  156. cvtColor(imgTraitment, imgTraitment, CV_RGB2GRAY);
  157. threshold(imgTraitment, imgTraitment, 128, 255, CV_THRESH_BINARY);
  158. findContours(imgTraitment, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
  159. for (unsigned int i = 0; i < contours.size(); i++) {
  160. ImageMat m(contours[i]);
  161. cv::approxPolyDP(m, approx, cv::arcLength(ImageMat(contours[i]), true)*0.17, true);
  162. if (approx.size() == 3 && fabs(cv::contourArea(contours[i])) > 200 && fabs(cv::contourArea(contours[i])) < 700) {
  163. cv::Point a, b, c;
  164. cv::Point center;
  165. a = approx[0];
  166. b = approx[1];
  167. c = approx[2];
  168. if (!arena.IsEmpty()) { // ajout de l'offset de l'arène
  169. a.x += arena.arena.x;
  170. a.y += arena.arena.y;
  171. b.x += arena.arena.x;
  172. b.y += arena.arena.y;
  173. c.x += arena.arena.x;
  174. c.y += arena.arena.y;
  175. }
  176. center.x = (a.x + b.x + c.x) / 3;
  177. center.y = (a.y + b.y + c.y) / 3;
  178. Position newPos;
  179. newPos.center = center;
  180. if (EuclideanDistance(center, b) > EuclideanDistance(center, a) && EuclideanDistance(center, b) > EuclideanDistance(center, c)) {
  181. newPos.direction = b;
  182. //line(img,center,b,Scalar(0,125,0),2,8,0);
  183. } else if (EuclideanDistance(center, a) > EuclideanDistance(center, c)) {
  184. newPos.direction = a;
  185. //line(img,center,a,Scalar(0,125,0),2,8,0);
  186. } else {
  187. newPos.direction = c;
  188. //line(img,center,c,Scalar(0,125,0),2,8,0);
  189. }
  190. newPos.angle = CalculAngle(newPos);
  191. newPos.robotId = -1; // dumb identifier
  192. robotsFind.push_back(newPos);
  193. }
  194. }
  195. return robotsFind;
  196. #endif // __WITH_ARUCO__
  197. }
  198. /**
  199. * Search arena outline in current image
  200. * @return Arena object with coordinate of outline, empty if no arena found
  201. */
  202. Arena Img::SearchArena() {
  203. std::vector<std::vector<cv::Point> > contours;
  204. std::vector<cv::Point> approx;
  205. std::vector<cv::Vec4i> hierarchy;
  206. ImageMat imageTrt;
  207. cv::cvtColor(this->img, imageTrt, CV_RGB2GRAY); // conversion en niveau de gris
  208. cv::threshold(imageTrt, imageTrt, 128, 255, CV_THRESH_BINARY); // Threshold les éléments les plus clair
  209. cv::Canny(imageTrt, imageTrt, 100, 200, 3); // detection d'angle
  210. findContours(imageTrt, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
  211. for (unsigned int i = 0; i < contours.size(); i++) {
  212. approxPolyDP(ImageMat(contours[i]), approx, cv::arcLength(ImageMat(contours[i]), true)*0.1, true);
  213. if (approx.size() == 4 && fabs(cv::contourArea(contours[i])) > 100000) {
  214. Arena rectangle;
  215. rectangle.arena = cv::boundingRect(ImageMat(contours[i]));
  216. return rectangle;
  217. }
  218. }
  219. return Arena();
  220. }
  221. /**
  222. * Draw an oriented arrow at robot position
  223. * @param robot Position of robot
  224. */
  225. void Img::DrawRobot(Position robot) {
  226. cv::arrowedLine(this->img, (cv::Point2f)robot.center, (cv::Point2f)robot.direction, cv::Scalar(0, 0, 255), 3, 8, 0);
  227. }
  228. /**
  229. * Draw an oriented arrow for each position provided
  230. * @param robots List of robot positions
  231. * @return Number of position drawn
  232. */
  233. int Img::DrawAllRobots(std::list<Position> robots) {
  234. for (Position robot : robots) {
  235. cv::arrowedLine(this->img, (cv::Point2f)robot.center, (cv::Point2f)robot.direction, cv::Scalar(0, 0, 255), 3, 8, 0);
  236. }
  237. return robots.size();
  238. }
  239. /**
  240. * Draw arena outline
  241. * @param arenaToDraw Arena position
  242. */
  243. void Img::DrawArena(Arena arenaToDraw) {
  244. cv::rectangle(this->img, arenaToDraw.arena.tl(), arenaToDraw.arena.br(), cv::Scalar(0, 0, 125), 2, 8, 0);
  245. }
  246. /**
  247. * Crop image around detected arena
  248. * @param arena Coordinate of arena
  249. * @return Reduced image, focused on arena
  250. */
  251. ImageMat Img::CropArena(Arena arena) {
  252. return this->img(arena.arena);
  253. }