Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-contour.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpImage.h>
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/io/vpImageIo.h>
10 
11 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
12 #include <visp3/imgproc/vpImgproc.h>
15 
16 namespace
17 {
19 void displayContourInfo(const vp::vpContour &contour, int level)
20 {
21  std::cout << "\nContour:" << std::endl;
22  std::cout << "\tlevel: " << level << std::endl;
23  std::cout << "\tcontour type: " << (contour.m_contourType == vp::CONTOUR_OUTER ? "outer contour" : "hole contour")
24  << std::endl;
25  std::cout << "\tcontour size: " << contour.m_points.size() << std::endl;
26  std::cout << "\tnb children: " << contour.m_children.size() << std::endl;
27 
28  for (std::vector<vp::vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
29  ++it) {
30  displayContourInfo(**it, level + 1);
31  }
32 }
34 
36 void drawContoursTree(vpImage<vpRGBa> &I, const vp::vpContour &contour)
37 {
38  std::vector<std::vector<vpImagePoint> > contours;
39  contours.push_back(contour.m_points);
41 
42  for (std::vector<vp::vpContour *>::const_iterator it = contour.m_children.begin(); it != contour.m_children.end();
43  ++it) {
44  drawContoursTree(I, **it);
45  }
46 }
48 } // namespace
49 #endif
50 
51 int main(int argc, const char **argv)
52 {
54 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
55  std::string input_filename = "grid36-03.pgm";
58  bool white_foreground = false;
60 
61  for (int i = 1; i < argc; i++) {
62  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
63  input_filename = std::string(argv[i + 1]);
64  } else if (std::string(argv[i]) == "--white_foreground") {
65  white_foreground = true;
66  } else if (std::string(argv[i]) == "--method" && i + 1 < argc) {
67  extraction_method = (vp::vpContourRetrievalType)atoi(argv[i + 1]);
68  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
69  std::cout << "Usage: " << argv[0]
70  << " [--input <input image>] [--method <0: "
71  "CONTOUR_RETR_TREE, 1: CONTOUR_RETR_LIST, 2: "
72  "CONTOUR_RETR_EXTERNAL>]"
73  " [--white_foreground] [--help]"
74  << std::endl;
75  return EXIT_SUCCESS;
76  }
77  }
78 
81  vpImageIo::read(I, input_filename);
84 
85  vpImage<vpRGBa> I_draw_contours(I.getHeight(), I.getWidth());
86 
87 #ifdef VISP_HAVE_X11
88  vpDisplayX d, d2;
89 #elif defined(VISP_HAVE_GDI)
90  vpDisplayGDI d, d2;
91 #elif defined(VISP_HAVE_OPENCV)
92  vpDisplayOpenCV d, d2;
93 #endif
94  d.init(I_bin, 0, 0, "After binarisation");
95  d2.init(I_draw_contours, I_bin.getWidth(), 10, "Contours");
96 
98  vp::autoThreshold(I, vp::AUTO_THRESHOLD_OTSU, white_foreground ? 0 : 1, white_foreground ? 1 : 0);
100  for (unsigned int i = 0; i < I_bin.getSize(); i++) {
101  I_bin.bitmap[i] = 255 * I.bitmap[i];
102  }
103 
105  vp::vpContour vp_contours;
106  std::vector<std::vector<vpImagePoint> > contours;
107  vp::findContours(I, vp_contours, contours, extraction_method);
109 
111  vp::drawContours(I_draw_contours, contours, vpColor::red);
113 
114  vpDisplay::display(I_bin);
115  vpDisplay::display(I_draw_contours);
116  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to draw outer / hole contours.", vpColor::red);
117  vpDisplay::flush(I_bin);
118  vpDisplay::flush(I_draw_contours);
119  vpDisplay::getClick(I_draw_contours);
120 
121  I_draw_contours = 0;
123  drawContoursTree(I_draw_contours, vp_contours);
125  displayContourInfo(vp_contours, 0);
126 
127  vpDisplay::display(I_bin);
128  vpDisplay::display(I_draw_contours);
129  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to quit.", vpColor::red);
130  vpDisplay::displayText(I_draw_contours, 20, I_draw_contours.getWidth() - 200, "Outer contour", vpColor::red);
131  vpDisplay::displayText(I_draw_contours, 20, I_draw_contours.getWidth() - 100, "Hole contour", vpColor::green);
132  vpDisplay::flush(I_bin);
133  vpDisplay::flush(I_draw_contours);
134  vpDisplay::getClick(I_draw_contours);
135 
136  return EXIT_SUCCESS;
137 #else
138  (void)argc;
139  (void)argv;
140  return 0;
141 #endif
142 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Type * bitmap
points toward the bitmap
Definition: vpImage.h:141
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
std::vector< vpImagePoint > m_points
Definition: vpContours.h:175
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
std::vector< vpContour * > m_children
Definition: vpContours.h:172
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static const vpColor green
Definition: vpColor.h:182
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT void findContours(const vpImage< unsigned char > &I_original, vpContour &contours, std::vector< std::vector< vpImagePoint > > &contourPts, const vpContourRetrievalType &retrievalMode=vp::CONTOUR_RETR_TREE)
Definition: vpContours.cpp:300
static const vpColor red
Definition: vpColor.h:179
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
vpContourType m_contourType
Definition: vpContours.h:173
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
Definition: vpContours.cpp:250
VISP_EXPORT unsigned char autoThreshold(vpImage< unsigned char > &I, const vp::vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
unsigned int getHeight() const
Definition: vpImage.h:186
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
vpContourRetrievalType
Definition: vpContours.h:164
unsigned int getWidth() const
Definition: vpImage.h:244