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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. bool Arene::empty() {
  19. if ((this->arene.height==0) || (this->arene.width==0)) return true;
  20. else return false;
  21. }
  22. Img::Img(ImageMat imgMatrice) {
  23. this->img = imgMatrice.clone();
  24. }
  25. string Img::ToString() {
  26. return "Image size: "+this->img.cols+"x"this->img.rows+" (dim="+this->img.dims+")";
  27. }
  28. Img* Img::Copy() {
  29. return new Img(this->img);
  30. }
  31. float Img::calculAngle(Position robot) {
  32. float a = robot.direction.x - robot.center.x;
  33. float b = robot.direction.y - robot.center.y ;
  34. float angle = atan2(b,a);
  35. return angle * 180.f/M_PI;
  36. }
  37. float Img::calculAngle2(cv::Point2f pt1, cv::Point2f pt2) {
  38. float a = pt1.x - pt2.x;
  39. float b = pt1.y - pt2.y ;
  40. float angle = atan2(b,a);
  41. return angle * 180.f/M_PI;
  42. }
  43. #ifdef __WITH_ARUCO__
  44. cv::Point2f Img::find_aruco_center(std::vector<cv::Point2f> aruco) {
  45. return ((aruco[0] + aruco[2])/2);
  46. }
  47. cv::Point2f Img::find_aruco_direction(std::vector<cv::Point2f> aruco) {
  48. return ((aruco[0]+aruco[1])/2);;
  49. }
  50. std::list<Position> Img::search_aruco(Arene monArene) {
  51. ImageMat imgTraitment;
  52. std::list<Position> positionList;
  53. cv::Point2f areneCoor;
  54. std::vector<int> ids;
  55. std::vector<std::vector<cv::Point2f> > corners;
  56. if(monArene.empty())
  57. imgTraitment=this->img.clone();
  58. else{
  59. imgTraitment = cropArena(monArene);
  60. areneCoor.x = monArene.x;
  61. areneCoor.y = monArene.y;
  62. }
  63. cv::detectMarkers(imgTraitment,dictionary, corners, ids);
  64. if(ids.size()>0){
  65. for(int i = 0 ; i < ids.size() ; i++){
  66. Position newPos;
  67. newPos.center = find_aruco_center(corners[i]);
  68. newPos.direction = find_aruco_direction(corners[i]);
  69. newPos.robotId = ids[i];
  70. if(!monArene.empty()){
  71. newPos.direction += areneCoor;
  72. newPos.center += areneCoor;
  73. }
  74. newPos.angle = calculAngle2(newPos.center, newPos.direction);
  75. positionList.push_back(newPos);
  76. }
  77. }
  78. return positionList;
  79. }
  80. #endif // __WITH_ARUCO__
  81. float Img::euclideanDist(cv::Point2f p, cv::Point2f q) {
  82. cv::Point diff = p - q;
  83. return cv::sqrt(diff.x*diff.x + diff.y*diff.y);
  84. }
  85. Jpg Img::toJpg() {
  86. Jpg imgJpg;
  87. cv::imencode(".jpg",this->img,imgJpg);
  88. return imgJpg;
  89. }
  90. string Img::ToBase64() {
  91. string imgBase64;
  92. Jpg imgJpg = toJpg();
  93. /* faire la convertion Jpg vers base 64 */
  94. return imgBase64;
  95. }
  96. std::list<Position> Img::search_robot(Arene monArene) {
  97. std::list<Position> robotsFind;
  98. std::vector<std::vector<cv::Point2f> > contours;
  99. std::vector<cv::Point2f> approx;
  100. std::vector<cv::Vec4i> hierarchy;
  101. ImageMat imgTraitment;
  102. if(monArene.empty())
  103. imgTraitment=this->img.clone();
  104. else
  105. imgTraitment = cropArena(monArene);
  106. cvtColor(imgTraitment,imgTraitment,CV_RGB2GRAY);
  107. threshold(imgTraitment,imgTraitment,128,255,CV_THRESH_BINARY);
  108. findContours(imgTraitment, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point2f(0, 0));
  109. for(unsigned int i = 0;i < contours.size();i++)
  110. {
  111. cv::approxPolyDP(ImageMat(contours[i]), approx, cv::arcLength(ImageMat(contours[i]), true)*0.17, true);
  112. if(approx.size() == 3 && fabs(cv::contourArea(contours[i])) > 200 && fabs(cv::contourArea(contours[i])) < 700)
  113. {
  114. cv::Point2f a,b,c;
  115. cv::Point2f center;
  116. a = approx[0];
  117. b = approx[1];
  118. c = approx[2];
  119. if(!monArene.empty()) // ajout de l'offset de l'arène
  120. {
  121. a.x += monArene.arene.x;
  122. a.y += monArene.arene.y;
  123. b.x += monArene.arene.x;
  124. b.y += monArene.arene.y;
  125. c.x += monArene.arene.x;
  126. c.y += monArene.arene.y;
  127. }
  128. center.x = (a.x + b.x + c.x)/3;
  129. center.y = (a.y + b.y + c.y)/3;
  130. Position newPos;
  131. newPos.center=center;
  132. if(euclideanDist(center,b) > euclideanDist(center,a) && euclideanDist(center,b) > euclideanDist(center,c) )
  133. {
  134. newPos.direction=b;
  135. //line(img,center,b,Scalar(0,125,0),2,8,0);
  136. }
  137. else if(euclideanDist(center,a) > euclideanDist(center,c))
  138. {
  139. newPos.direction=a;
  140. //line(img,center,a,Scalar(0,125,0),2,8,0);
  141. }
  142. else
  143. {
  144. newPos.direction=c;
  145. //line(img,center,c,Scalar(0,125,0),2,8,0);
  146. }
  147. newPos.angle=calculAngle(newPos);
  148. robotsFind.push_back(newPos);
  149. }
  150. }
  151. return robotsFind;
  152. }
  153. Arene Img::search_arena() {
  154. std::vector<std::vector<cv::Point> > contours;
  155. std::vector<cv::Point> approx;
  156. std::vector<cv::Vec4i> hierarchy;
  157. ImageMat imageTrt;
  158. cv::cvtColor(this->img,imageTrt,CV_RGB2GRAY); // conversion en niveau de gris
  159. cv::threshold(imageTrt,imageTrt,128,255,CV_THRESH_BINARY); // Threshold les éléments les plus clair
  160. cv::Canny(imageTrt, imageTrt, 100,200,3); // detection d'angle
  161. findContours(imageTrt, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0));
  162. for(unsigned int i = 0; i < contours.size();i++)
  163. {
  164. approxPolyDP(ImageMat(contours[i]), approx, cv::arcLength(ImageMat(contours[i]), true)*0.1, true);
  165. if(approx.size()==4 && fabs(cv::contourArea(contours[i])) > 100000)
  166. {
  167. Arene rectangle;
  168. rectangle.arene = cv::boundingRect(ImageMat(contours[i]));
  169. return rectangle;
  170. }
  171. }
  172. return Arene();
  173. }
  174. int Img::draw_robot(Position robot) {
  175. cv::arrowedLine(this->img, (cv::Point2f)robot.center, (cv::Point2f)robot.direction, cv::Scalar(0,0,255),3,8,0);
  176. return 0;
  177. }
  178. int Img::draw_all_robots(std::list<Position> robots) {
  179. for(Position robot : robots){
  180. cv::arrowedLine(this->img, (cv::Point2f)robot.center, (cv::Point2f)robot.direction, cv::Scalar(0,0,255),3,8,0);
  181. }
  182. return robots.size();
  183. }
  184. int Img::draw_arena(Arene areneToDraw) {
  185. cv::rectangle(this->img,areneToDraw.arene.tl(),areneToDraw.arene.br(),cv::Scalar(0,0,125),2,8,0);
  186. return 0;
  187. }
  188. ImageMat Img::cropArena(Arene arene) {
  189. return this->img(arene.arene);
  190. }