Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpTemplateTrackerZone.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Template tracker.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpConfig.h>
37 
38 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
39 #include <opencv2/imgproc/imgproc.hpp>
40 #endif
41 
42 #include <visp3/tt/vpTemplateTrackerZone.h>
43 
48  vpTemplateTrackerZone::vpTemplateTrackerZone() : Zone(), min_x(-1), min_y(-1), max_x(-1), max_y(-1) { }
49 
54  : Zone(), min_x(-1), min_y(-1), max_x(-1), max_y(-1)
55 {
56  *this = z;
57 }
58 
63 {
64  min_x = -1;
65  min_y = -1;
66  max_x = -1;
67  max_y = -1;
68 
69  Zone.clear();
70 }
71 
76 {
77  clear();
78 
79  this->copy(z);
80  return (*this);
81 }
82 
106 {
107  Zone.clear();
108 
109  std::vector<vpImagePoint> vip;
110 
111  bool end = false;
112 
113  do {
114  vpImagePoint p;
116  if (vpDisplay::getClick(I, p, button, false)) {
117  vip.push_back(p);
118 
120 
121  if (vip.size() > 1) {
122  if (delaunay) {
123  // Draw a line between the 2 last points
124  vpDisplay::displayLine(I, p, vip[vip.size() - 2], vpColor::blue, 3);
125  }
126  else {
127  if (vip.size() % 3 == 2)
128  // draw line between point 2-1
129  vpDisplay::displayLine(I, p, vip[vip.size() - 2], vpColor::blue, 3);
130  else if (vip.size() % 3 == 0) {
131  // draw line between point 3-2
132  vpDisplay::displayLine(I, p, vip[vip.size() - 2], vpColor::blue, 3);
133  // draw line between point 3-1
134  vpDisplay::displayLine(I, p, vip[vip.size() - 3], vpColor::blue, 3);
135  }
136  }
137  }
138 
139  if (button == vpMouseButton::button3)
140  end = true;
141  }
142 
143  vpTime::wait(20);
144  vpDisplay::flush(I);
145  } while (!end);
146 
147  initFromPoints(I, vip, delaunay);
148 }
149 
163 void vpTemplateTrackerZone::initFromPoints(const vpImage<unsigned char> &I, const std::vector<vpImagePoint> &vip,
164  bool delaunay)
165 {
166  if (delaunay) {
167  if (vip.size() == 3) {
168  initFromPoints(I, vip, false);
169  }
170  else if (vip.size() == 4) {
171  std::vector<vpImagePoint> vip_delaunay;
172  vip_delaunay.push_back(vip[0]);
173  vip_delaunay.push_back(vip[1]);
174  vip_delaunay.push_back(vip[2]);
175  vip_delaunay.push_back(vip[2]);
176  vip_delaunay.push_back(vip[3]);
177  vip_delaunay.push_back(vip[0]);
178  initFromPoints(I, vip_delaunay, false);
179  }
180  else {
181 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
182  // Init Delaunay
183  cv::Subdiv2D subdiv(cv::Rect(0, 0, (int)I.getWidth(), (int)I.getHeight()));
184  for (size_t i = 0; i < vip.size(); i++) {
185  cv::Point2f fp((float)vip[i].get_u(), (float)vip[i].get_v());
186  // std::cout << "Click point: " << vip[i] << std::endl;
187  subdiv.insert(fp);
188  }
189 
190  // Compute Delaunay triangulation
191  std::vector<cv::Vec6f> triangleList;
192  subdiv.getTriangleList(triangleList);
193 
194  // Keep only the Delaunay points that are inside the area
195  vpRect rect(0, 0, I.getWidth(), I.getHeight());
196 
197  std::vector<vpImagePoint> vip_delaunay;
198  for (size_t i = 0; i < triangleList.size(); i++) {
199  cv::Vec6f t = triangleList[i];
200  std::vector<vpImagePoint> p(3);
201 
202  p[0].set_uv(t[0], t[1]);
203  p[1].set_uv(t[2], t[3]);
204  p[2].set_uv(t[4], t[5]);
205 
206  if (p[0].inRectangle(rect) && p[1].inRectangle(rect) && p[2].inRectangle(rect)) {
207  vip_delaunay.push_back(p[0]);
208  vip_delaunay.push_back(p[1]);
209  vip_delaunay.push_back(p[2]);
210  }
211  }
212 
213  initFromPoints(I, vip_delaunay, false);
214 #else
215  throw vpException(vpException::functionNotImplementedError, "Delaunay triangulation is not available!");
216 #endif
217  }
218  }
219  else {
220  Zone.clear();
221  for (unsigned int i = 0; i < vip.size(); i += 3) {
222  vpTemplateTrackerTriangle triangle(vip[i], vip[i + 1], vip[i + 2]);
223  add(triangle);
224 
225  // vpDisplay::displayLine(I, vip[i], vip[i+1], vpColor::green,
226  // 1); vpDisplay::displayLine(I, vip[i+1], vip[i+2],
227  // vpColor::green, 1); vpDisplay::displayLine(I, vip[i+2], vip[i],
228  // vpColor::green,1); vpDisplay::flush(I) ;
229 
230  // Update the bounding box
231  if ((triangle.getMinx() < min_x) || (min_x == -1))
232  min_x = (int)triangle.getMinx();
233  if ((triangle.getMaxx() > max_x) || (max_x == -1))
234  max_x = (int)triangle.getMaxx();
235  if ((triangle.getMiny() < min_y) || (min_y == -1))
236  min_y = (int)triangle.getMiny();
237  if ((triangle.getMaxy() > max_y) || (max_y == -1))
238  max_y = (int)triangle.getMaxy();
239  }
240  }
241 }
242 
248 {
249  Zone.push_back(t);
250 
251  // Update the bounding box
252  if ((t.getMinx() < min_x) || (min_x == -1))
253  min_x = (int)t.getMinx();
254  if ((t.getMaxx() > max_x) || (max_x == -1))
255  max_x = (int)t.getMaxx();
256  if ((t.getMiny() < min_y) || (min_y == -1))
257  min_y = (int)t.getMiny();
258  if ((t.getMaxy() > max_y) || (max_y == -1))
259  max_y = (int)t.getMaxy();
260 }
261 
268 bool vpTemplateTrackerZone::inZone(const int &i, const int &j) const
269 {
270  std::vector<vpTemplateTrackerTriangle>::const_iterator Iterateurvecteur;
271  for (Iterateurvecteur = Zone.begin(); Iterateurvecteur != Zone.end(); ++Iterateurvecteur) {
272  if (Iterateurvecteur->inTriangle(i, j))
273  return true;
274  }
275  return false;
276 }
277 
284 bool vpTemplateTrackerZone::inZone(const double &i, const double &j) const
285 {
286  std::vector<vpTemplateTrackerTriangle>::const_iterator Iterateurvecteur;
287  for (Iterateurvecteur = Zone.begin(); Iterateurvecteur != Zone.end(); ++Iterateurvecteur) {
288  if (Iterateurvecteur->inTriangle(i, j))
289  return true;
290  }
291  return false;
292 }
293 
301 bool vpTemplateTrackerZone::inZone(const int &i, const int &j, unsigned int &id_triangle) const
302 {
303  unsigned int id = 0;
304  std::vector<vpTemplateTrackerTriangle>::const_iterator Iterateurvecteur;
305  for (Iterateurvecteur = Zone.begin(); Iterateurvecteur != Zone.end(); ++Iterateurvecteur) {
306  if (Iterateurvecteur->inTriangle(i, j)) {
307  id_triangle = id;
308  return true;
309  }
310  id++;
311  }
312  return false;
313 }
314 
322 bool vpTemplateTrackerZone::inZone(const double &i, const double &j, unsigned int &id_triangle) const
323 {
324  unsigned int id = 0;
325  std::vector<vpTemplateTrackerTriangle>::const_iterator Iterateurvecteur;
326  for (Iterateurvecteur = Zone.begin(); Iterateurvecteur != Zone.end(); ++Iterateurvecteur) {
327  if (Iterateurvecteur->inTriangle(i, j)) {
328  id_triangle = id;
329  return true;
330  }
331  id++;
332  }
333  return false;
334 }
335 
355 {
356  if (i > getNbTriangle() - 1)
357  throw(vpException(vpException::badValue, "Cannot get triangle with index %u", i));
358 
359  T = Zone[i];
360 }
376 {
377  if (i > getNbTriangle() - 1)
378  throw(vpException(vpException::badValue, "Cannot get triangle with index %u", i));
379 
380  return Zone[i];
381 }
387 {
388  double xc = 0;
389  double yc = 0;
390  int cpt = 0;
391  for (int i = min_y; i < max_y; i++)
392  for (int j = min_x; j < max_x; j++)
393  if (inZone(i, j)) {
394  xc += j;
395  yc += i;
396  cpt++;
397  }
398  if (!cpt) {
399  throw(vpException(vpException::divideByZeroError, "Cannot compute the zone center: size = 0"));
400  }
401  xc = xc / cpt;
402  yc = yc / cpt;
403  vpImagePoint ip;
404  ip.set_uv(xc, yc);
405  return ip;
406 }
407 
412 int vpTemplateTrackerZone::getMaxx() const { return max_x; }
417 int vpTemplateTrackerZone::getMaxy() const { return max_y; }
422 int vpTemplateTrackerZone::getMinx() const { return min_x; }
427 int vpTemplateTrackerZone::getMiny() const { return min_y; }
428 
434 {
435  vpRect bbox;
438  return bbox;
439 }
440 
446 void vpTemplateTrackerZone::display(const vpImage<unsigned char> &I, const vpColor &col, unsigned int thickness)
447 {
448  std::vector<vpImagePoint> ip;
449  for (unsigned int i = 0; i < Zone.size(); i++) {
450  vpTemplateTrackerTriangle triangle;
451  Zone[i].getCorners(ip);
452  vpDisplay::displayLine(I, ip[0], ip[1], col, thickness);
453  vpDisplay::displayLine(I, ip[1], ip[2], col, thickness);
454  vpDisplay::displayLine(I, ip[2], ip[0], col, thickness);
455  }
456 }
457 
463 void vpTemplateTrackerZone::display(const vpImage<vpRGBa> &I, const vpColor &col, unsigned int thickness)
464 {
465  std::vector<vpImagePoint> ip;
466  for (unsigned int i = 0; i < Zone.size(); i++) {
467  vpTemplateTrackerTriangle triangle;
468  Zone[i].getCorners(ip);
469  vpDisplay::displayLine(I, ip[0], ip[1], col, thickness);
470  vpDisplay::displayLine(I, ip[1], ip[2], col, thickness);
471  vpDisplay::displayLine(I, ip[2], ip[0], col, thickness);
472  }
473 }
474 
479 
487 void vpTemplateTrackerZone::fillTriangle(vpImage<unsigned char> &I, unsigned int id, unsigned char gray_level)
488 {
489  assert(id < getNbTriangle());
490  vpTemplateTrackerTriangle triangle;
491  getTriangle(id, triangle);
492  for (int i = 0; i < (int)I.getHeight(); i++) {
493  for (int j = 0; j < (int)I.getWidth(); j++) {
494  if (triangle.inTriangle(i, j)) {
495  I[i][j] = gray_level;
496  }
497  }
498  }
499 }
500 
505 {
506  vpTemplateTrackerZone tempZone;
508  vpTemplateTrackerTriangle TtempDown;
509  for (unsigned int i = 0; i < getNbTriangle(); i++) {
510  getTriangle(i, Ttemp);
511  TtempDown = Ttemp.getPyramidDown();
512  tempZone.add(TtempDown);
513  }
514  return tempZone;
515 }
516 
523 {
524  vpTemplateTrackerTriangle triangle;
525  for (unsigned int i = 0; i < z.getNbTriangle(); i++) {
526  z.getTriangle(i, triangle);
527  add(triangle);
528  // Update the bounding box
529  if ((triangle.getMinx() < min_x) || (min_x == -1))
530  min_x = (int)triangle.getMinx();
531  if ((triangle.getMaxx() > max_x) || (max_x == -1))
532  max_x = (int)triangle.getMaxx();
533  if ((triangle.getMiny() < min_y) || (min_y == -1))
534  min_y = (int)triangle.getMiny();
535  if ((triangle.getMaxy() > max_y) || (max_y == -1))
536  max_y = (int)triangle.getMaxy();
537  }
538 }
539 
547 vpImagePoint vpTemplateTrackerZone::getCenter(int borne_x, int borne_y) const
548 {
549  int cpt_pt = 0;
550  double x_center = 0, y_center = 0;
551  for (int j = 0; j < borne_x; j++)
552  for (int i = 0; i < borne_y; i++)
553  if (inZone(i, j)) {
554  x_center += j;
555  y_center += i;
556  cpt_pt++;
557  }
558 
559  if (!cpt_pt) {
560  throw(vpException(vpException::divideByZeroError, "Cannot compute the zone center: size = 0"));
561  }
562 
563  x_center = x_center / cpt_pt;
564  y_center = y_center / cpt_pt;
565  vpImagePoint center;
566  center.set_uv(x_center, y_center);
567  return center;
568 }
569 
574 {
575  double area = 0;
576  vpTemplateTrackerTriangle triangle;
577  for (unsigned int i = 0; i < getNbTriangle(); i++) {
578  getTriangle(i, triangle);
579  area += triangle.getArea();
580  }
581  return area;
582 }
583 END_VISP_NAMESPACE
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:73
@ functionNotImplementedError
Function not implemented.
Definition: vpException.h:66
@ divideByZeroError
Division by zero.
Definition: vpException.h:70
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_uv(double u, double v)
Definition: vpImagePoint.h:357
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Defines a rectangle in the plane.
Definition: vpRect.h:79
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:296
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:366
bool inTriangle(const vpImagePoint &ip) const
vpTemplateTrackerTriangle getPyramidDown() const
vpTemplateTrackerZone getPyramidDown() const
std::vector< vpTemplateTrackerTriangle > Zone
Vector of triangles that defines the zone.
int max_y
Bounding box parameter.
vpTemplateTrackerZone & operator=(const vpTemplateTrackerZone &z)
void initFromPoints(const vpImage< unsigned char > &I, const std::vector< vpImagePoint > &ip, bool delaunay=false)
bool inZone(const int &i, const int &j) const
void getTriangle(unsigned int i, vpTemplateTrackerTriangle &T) const
unsigned int getNbTriangle() const
void copy(const vpTemplateTrackerZone &z)
int max_x
Bounding box parameter.
void fillTriangle(vpImage< unsigned char > &I, unsigned int id, unsigned char gray_level)
void add(const vpTemplateTrackerTriangle &t)
vpImagePoint getCenter() const
void display(const vpImage< unsigned char > &I, const vpColor &col=vpColor::green, unsigned int thickness=3)
int min_y
Bounding box parameter.
void initClick(const vpImage< unsigned char > &I, bool delaunay=false)
int min_x
Bounding box parameter.
VISP_EXPORT int wait(double t0, double t)