Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-count-coins.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)
12 #include <visp3/core/vpMomentObject.h>
14 #include <visp3/imgproc/vpImgproc.h>
16 #endif
17 
18 int main(int argc, char *argv[])
19 {
21 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
22 
24  std::string input_filename = "coins1.pgm";
26  bool white_foreground = false;
27 
28  for (int i = 1; i < argc; i++) {
29  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
30  input_filename = std::string(argv[i + 1]);
31  } else if (std::string(argv[i]) == "--method" && i + 1 < argc) {
32  method = (vp::vpAutoThresholdMethod)atoi(argv[i + 1]);
33  } else if (std::string(argv[i]) == "--white_foreground") {
34  white_foreground = true;
35  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
36  std::cout << "Usage: " << argv[0]
37  << " [--input <input image>]"
38  " [--method <0: Huang, 1: Intermodes, 2: IsoData, 3: "
39  "Mean, 4: Otsu, 5: Triangle>]"
40  " [--white_foreground]"
41  " [--help]"
42  << std::endl;
43  return EXIT_SUCCESS;
44  }
45  }
46 
49  vpImageIo::read(I, input_filename);
51 
52 #ifdef VISP_HAVE_X11
53  vpDisplayX d, d2, d3, d4, d5;
54 #elif defined(VISP_HAVE_GDI)
55  vpDisplayGDI d, d2, d3, d4, d5;
56 #elif defined(VISP_HAVE_OPENCV)
57  vpDisplayOpenCV d, d2, d3, d4, d5;
58 #endif
59  d.init(I, 0, 0, "Coins");
60 
61  vpImage<unsigned char> I_bin, I_fill;
63  I_bin = I;
64  vp::autoThreshold(I_bin, method, white_foreground ? 0 : 255, white_foreground ? 255 : 0);
66  d2.init(I_bin, I.getWidth(), 0, "Binarisation");
67 
69  I_fill = I_bin;
70  vp::fillHoles(I_fill);
72  d3.init(I_fill, 0, I.getHeight() + 80, "Fill holes");
73 
75  vpImage<unsigned char> I_open = I_fill;
79 
81  vpImage<unsigned char> I_close = I_open;
85  d4.init(I_close, I.getWidth(), I.getHeight() + 80, "Closing");
86 
88  vpImage<unsigned char> I_contours(I_close.getHeight(), I_close.getWidth());
89  for (unsigned int cpt = 0; cpt < I_close.getSize(); cpt++)
90  I_contours.bitmap[cpt] = I_close.bitmap[cpt] ? 1 : 0;
91 
92  vp::vpContour vp_contours;
93  std::vector<std::vector<vpImagePoint> > contours;
94  vp::findContours(I_contours, vp_contours, contours, vp::CONTOUR_RETR_EXTERNAL);
96 
98  vpImage<vpRGBa> I_draw_contours(I_contours.getHeight(), I_contours.getWidth(), vpRGBa());
99  vp::drawContours(I_draw_contours, contours, vpColor::red);
101  d5.init(I_draw_contours, 0, 2 * I.getHeight() + 80, "Contours");
102 
104  vpDisplay::display(I_bin);
105  vpDisplay::display(I_fill);
106  vpDisplay::display(I_close);
107  vpDisplay::display(I_draw_contours);
108 
110  int nb_coins = 0;
111  for (size_t i = 0; i < contours.size(); i++) {
112  std::vector<vpPoint> vec_p;
113 
114  for (size_t j = 0; j < contours[i].size(); j++) {
115  vpPoint pt;
116  pt.set_x(contours[i][j].get_u());
117  pt.set_y(contours[i][j].get_v());
118  vec_p.push_back(pt);
119  }
120 
121  vpMomentObject obj(1);
122  obj.setType(vpMomentObject::DENSE_POLYGON);
123  obj.fromVector(vec_p);
124 
125  // sign(m00) depends of the contour orientation (clockwise or
126  // counter-clockwise) that's why we use fabs
127  if (std::fabs(obj.get(0, 0)) >= I.getSize() / 200) {
128  nb_coins++;
129  std::stringstream ss;
130  ss << "Coin " << nb_coins;
131 
132  int centroid_x = (int)std::fabs(obj.get(1, 0) / obj.get(0, 0));
133  int centroid_y = (int)std::fabs(obj.get(0, 1) / obj.get(0, 0));
134  vpDisplay::displayText(I_draw_contours, centroid_y, centroid_x - 20, ss.str(), vpColor::red);
135  }
136  }
138 
139  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to quit.", vpColor::red);
140 
141  vpDisplay::flush(I);
142  vpDisplay::flush(I_bin);
143  vpDisplay::flush(I_fill);
144  vpDisplay::flush(I_close);
145  vpDisplay::flush(I_draw_contours);
146  vpDisplay::getClick(I_draw_contours);
147 
148  return EXIT_SUCCESS;
149 #else
150  (void)argc;
151  (void)argv;
152  return 0;
153 #endif
154 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Class for generic objects.
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
VISP_EXPORT void fillHoles(vpImage< unsigned char > &I)
Definition: vpMorph.cpp:54
Definition: vpRGBa.h:66
static const vpColor red
Definition: vpColor.h:179
Class that defines what is a point.
Definition: vpPoint.h:58
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:472
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:474
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
Definition: vpContours.cpp:250
vpAutoThresholdMethod
Definition: vpImgproc.h:58
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
unsigned int getSize() const
Definition: vpImage.h:225
unsigned int getWidth() const
Definition: vpImage.h:244