Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-contour.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.h>
7 #include <visp3/gui/vpDisplayFactory.h>
8 #include <visp3/io/vpImageIo.h>
9 
10 #if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_DISPLAY)
12 #include <visp3/imgproc/vpImgproc.h>
14 
15 #ifdef ENABLE_VISP_NAMESPACE
16 using namespace VISP_NAMESPACE_NAME;
17 #endif
18 
19 namespace
20 {
22 void displayContourInfo(const VISP_NAMESPACE_NAME::vpContour &contour, int level)
23 {
24  std::cout << "\nContour:" << std::endl;
25  std::cout << "\tlevel: " << level << std::endl;
26  std::cout << "\tcontour type: " << (contour.m_contourType == VISP_NAMESPACE_NAME::CONTOUR_OUTER ? "outer contour" : "hole contour")
27  << std::endl;
28  std::cout << "\tcontour size: " << contour.m_points.size() << std::endl;
29  std::cout << "\tnb children: " << contour.m_children.size() << std::endl;
30 
31  for (std::vector<VISP_NAMESPACE_NAME::vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
32  ++it) {
33  displayContourInfo(**it, level + 1);
34  }
35 }
37 
39 void drawContoursTree(vpImage<vpRGBa> &I, const VISP_NAMESPACE_NAME::vpContour &contour)
40 {
41  std::vector<std::vector<vpImagePoint> > contours;
42  contours.push_back(contour.m_points);
44 
45  for (std::vector<VISP_NAMESPACE_NAME::vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
46  ++it) {
47  drawContoursTree(I, **it);
48  }
49 }
51 } // namespace
52 #endif
53 
54 int main(int argc, const char **argv)
55 {
57 #if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_DISPLAY)
60  std::string input_filename = "grid36-03.pgm";
61  bool white_foreground = false;
63 
64  for (int i = 1; i < argc; i++) {
65  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
66  input_filename = std::string(argv[i + 1]);
67  }
68  else if (std::string(argv[i]) == "--white_foreground") {
69  white_foreground = true;
70  }
71  else if (std::string(argv[i]) == "--method" && i + 1 < argc) {
72  extraction_method = (VISP_NAMESPACE_NAME::vpContourRetrievalType)atoi(argv[i + 1]);
73  }
74  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
75  std::cout << "Usage: " << argv[0]
76  << " [--input <input image>] [--method <0: "
77  "CONTOUR_RETR_TREE, 1: CONTOUR_RETR_LIST, 2: "
78  "CONTOUR_RETR_EXTERNAL>]"
79  " [--white_foreground] [--help]"
80  << std::endl;
81  return EXIT_SUCCESS;
82  }
83  }
84 
87  vpImageIo::read(I, input_filename);
90 
91  vpImage<vpRGBa> I_draw_contours(I.getHeight(), I.getWidth());
92 
93 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
94  std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(I_bin, 0, 0, "After binarisation");
95  std::shared_ptr<vpDisplay> display2 = vpDisplayFactory::createDisplay(I_draw_contours, I_bin.getWidth(), 10, "Contours");
96 #else
97  vpDisplay *display = vpDisplayFactory::allocateDisplay(I_bin, 0, 0, "After binarisation");
98  vpDisplay *display2 = vpDisplayFactory::allocateDisplay(I_draw_contours, I_bin.getWidth(), 10, "Contours");
99 #endif
100 
102  VISP_NAMESPACE_NAME::autoThreshold(I, VISP_NAMESPACE_NAME::AUTO_THRESHOLD_OTSU, white_foreground ? 0 : 1, white_foreground ? 1 : 0);
104  for (unsigned int i = 0; i < I_bin.getSize(); i++) {
105  I_bin.bitmap[i] = 255 * I.bitmap[i];
106  }
107 
109  VISP_NAMESPACE_NAME::vpContour vp_contours;
110  std::vector<std::vector<vpImagePoint> > contours;
111  VISP_NAMESPACE_NAME::findContours(I, vp_contours, contours, extraction_method);
113 
115  VISP_NAMESPACE_NAME::drawContours(I_draw_contours, contours, vpColor::red);
117 
118  vpDisplay::display(I_bin);
119  vpDisplay::display(I_draw_contours);
120  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to draw outer / hole contours.", vpColor::red);
121  vpDisplay::flush(I_bin);
122  vpDisplay::flush(I_draw_contours);
123  vpDisplay::getClick(I_draw_contours);
124 
125  I_draw_contours = vpRGBa(0);
127  drawContoursTree(I_draw_contours, vp_contours);
129  displayContourInfo(vp_contours, 0);
130 
131  vpDisplay::display(I_bin);
132  vpDisplay::display(I_draw_contours);
133  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to quit.", vpColor::red);
134  vpDisplay::displayText(I_draw_contours, 20, I_draw_contours.getWidth() - 200, "Outer contour", vpColor::red);
135  vpDisplay::displayText(I_draw_contours, 20, I_draw_contours.getWidth() - 100, "Hole contour", vpColor::green);
136  vpDisplay::flush(I_bin);
137  vpDisplay::flush(I_draw_contours);
138  vpDisplay::getClick(I_draw_contours);
139 
140 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
141  if (display != nullptr) {
142  delete display;
143  }
144 
145  if (display2 != nullptr) {
146  delete display2;
147  }
148 #endif
149 #else
150  (void)argc;
151  (void)argv;
152 #endif
153  return EXIT_SUCCESS;
154 }
static const vpColor red
Definition: vpColor.h:198
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:242
Type * bitmap
points toward the bitmap
Definition: vpImage.h:135
unsigned int getHeight() const
Definition: vpImage.h:181
Definition: vpRGBa.h:70
VISP_EXPORT void findContours(const VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I_original, vpContour &contours, std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contourPts, const vpContourRetrievalType &retrievalMode=CONTOUR_RETR_TREE)
VISP_EXPORT void drawContours(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const std::vector< std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > > &contours, unsigned char grayValue=255)
VISP_EXPORT unsigned char autoThreshold(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I, const vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
std::vector< vpContour * > m_children
Children contour.
Definition: vpContours.h:212
std::vector< VISP_NAMESPACE_ADDRESSING vpImagePoint > m_points
Vector of points belonging to the contour.
Definition: vpContours.h:218
vpContourType m_contourType
Contour type.
Definition: vpContours.h:214