Visual Servoing Platform  version 3.6.1 under development (2024-04-27)
tutorial-munkres-assignment.cpp
1 
3 #include <functional>
4 
5 // Display
6 #include <visp3/gui/vpDisplayD3D.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayGTK.h>
9 #include <visp3/gui/vpDisplayOpenCV.h>
10 #include <visp3/gui/vpDisplayX.h>
11 
12 #include <visp3/core/vpColor.h>
13 
14 // Munkres
15 #include <visp3/core/vpMunkres.h>
16 
17 // Math
18 #include <visp3/core/vpUniRand.h>
19 
20 int main()
21 {
22  // Check if std:c++17 or higher
23 #if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
24 
25 #if defined(VISP_HAVE_DISPLAY)
26  // Create base img
27  vpImage<unsigned char> I(480, 640, 255);
28 
29  // Generate random points
31  vpUniRand rand {};
32  std::vector<vpImagePoint> rand_ips {};
33  while (rand_ips.size() < 10) {
34  rand_ips.emplace_back(rand.uniform(10, I.getHeight() - 10), rand.uniform(10, I.getWidth() - 10));
35  }
37 
38  try {
39  // Init display
40  const auto disp_scale_type = vpDisplay::SCALE_AUTO;
41 #if defined(VISP_HAVE_X11)
42  vpDisplayX d(I, disp_scale_type);
43 #elif defined(VISP_HAVE_GDI)
44  vpDisplayGDI d(I, disp_scale_type);
45 #elif defined(HAVE_OPENCV_HIGHGUI)
46  vpDisplayOpenCV d(I, disp_scale_type);
47 #elif defined(VISP_HAVE_GTK)
48  vpDisplayGTK d(I, disp_scale_type);
49 #elif defined(VISP_HAVE_D3D9)
50  vpDisplayD3D d(I, disp_scale_type);
51 #else
52  std::cout << "No image viewer is available..." << std::endl;
53 #endif
54  vpDisplay::setTitle(I, "Munkres Assignment Algorithm");
55 
56  // Local helper to display a point in the image
57  auto display_point = [&I](const vpImagePoint &ip, const vpColor &color) {
58  I.display->displayCircle(ip, 5, color, true, 1);
59  };
60 
62 
63  auto disp_lane { 0 };
64  vpDisplay::displayText(I, 15 * ++disp_lane, 15, "Left click to add a point", vpColor::black);
65  vpDisplay::displayText(I, 15 * ++disp_lane, 15, "Middle click to continue (run Munkres)", vpColor::black);
66  vpDisplay::displayText(I, 15 * ++disp_lane, 15, "Right click to quit", vpColor::black);
67 
68  std::for_each(begin(rand_ips), end(rand_ips), std::bind(display_point, std::placeholders::_1, vpColor::red));
70 
71  // Ask user to clic on point
73  std::vector<vpImagePoint> user_ips {};
75  while (button != vpMouseButton::button2) {
76  vpImagePoint ip {};
77  vpDisplay::getClick(I, ip, button, true);
78  if (button == vpMouseButton::button1) {
79  user_ips.push_back(ip);
80  }
81  else if (button == vpMouseButton::button3) {
82  return EXIT_SUCCESS;
83  }
84 
85  std::for_each(begin(user_ips), end(user_ips), std::bind(display_point, std::placeholders::_1, vpColor::green));
86 
88  }
90 
91  // Prepare Munkres (init cost matrix with random ip / user ip distances)
93  std::vector<std::vector<double> > cost_matrix(rand_ips.size(), std::vector<double>(user_ips.size()));
94  for (auto i = 0u; i < rand_ips.size(); i++) {
95  for (auto j = 0u; j < user_ips.size(); j++) {
96  cost_matrix.at(i).at(j) = vpImagePoint::distance(rand_ips.at(i), user_ips.at(j));
97  }
98  }
100 
101  // Display results
103  std::for_each(begin(rand_ips), end(rand_ips), std::bind(display_point, std::placeholders::_1, vpColor::red));
104  std::for_each(begin(user_ips), end(user_ips), std::bind(display_point, std::placeholders::_1, vpColor::green));
105 
107  for (const auto &[i, j] : vpMunkres::run(cost_matrix)) {
108  I.display->displayLine(rand_ips.at(i), user_ips.at(j), vpColor::blue, 1);
109  }
111 
112  vpDisplay::displayText(I, 15, 15, "Click to quit", vpColor::black);
113  vpDisplay::flush(I);
115 
116  }
117  catch (const vpException &e) {
118  std::cout << "Catch an exception: " << e << std::endl;
119  }
120 #endif // defined(VISP_HAVE_DISPLAY)
121 #endif
122  return EXIT_SUCCESS;
123 }
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor black
Definition: vpColor.h:205
static const vpColor blue
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:214
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:101
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayCircle(const vpImage< unsigned char > &I, const vpImageCircle &circle, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
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 setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:179
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
vpDisplay * display
Definition: vpImage.h:140
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:123