Visual Servoing Platform  version 3.6.1 under development (2024-11-21)
tutorial-count-coins.cpp
1 
3 #include <cstdlib>
4 #include <iostream>
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImage.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
11 
12 #if defined(VISP_HAVE_MODULE_IMGPROC)
14 #include <visp3/core/vpMomentObject.h>
15 #include <visp3/imgproc/vpImgproc.h>
17 #endif
18 
19 int main(int argc, char *argv[])
20 {
22 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
24 
25 #ifdef ENABLE_VISP_NAMESPACE
26  using namespace VISP_NAMESPACE_NAME;
27 #endif
28 
29  std::string input_filename = "coins1.jpg";
31  bool white_foreground = false;
32 
33  for (int i = 1; i < argc; i++) {
34  if (std::string(argv[i]) == "--input" && i + 1 < argc) {
35  input_filename = std::string(argv[i + 1]);
36  }
37  else if (std::string(argv[i]) == "--method" && i + 1 < argc) {
38  method = static_cast<VISP_NAMESPACE_NAME::vpAutoThresholdMethod>(atoi(argv[i + 1]));
39  }
40  else if (std::string(argv[i]) == "--white_foreground") {
41  white_foreground = true;
42  }
43  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
44  std::cout << "Usage: " << argv[0]
45  << " [--input <input image>]"
46  " [--method <0: Huang, 1: Intermodes, 2: IsoData, 3: "
47  "Mean, 4: Otsu, 5: Triangle>]"
48  " [--white_foreground]"
49  " [--help]"
50  << std::endl;
51  return EXIT_SUCCESS;
52  }
53  }
54 
57  vpImageIo::read(I, input_filename);
59 
60 #ifdef VISP_HAVE_X11
61  vpDisplayX d, d2, d3, d4, d5;
62 #elif defined(VISP_HAVE_GDI)
63  vpDisplayGDI d, d2, d3, d4, d5;
64 #elif defined(HAVE_OPENCV_HIGHGUI)
65  vpDisplayOpenCV d, d2, d3, d4, d5;
66 #endif
67  d.init(I, 0, 0, "Coins");
68 
69  vpImage<unsigned char> I_bin, I_fill;
71  I_bin = I;
72  VISP_NAMESPACE_NAME::autoThreshold(I_bin, method, white_foreground ? 0 : 255, white_foreground ? 255 : 0);
74  d2.init(I_bin, I.getWidth(), 0, "Binarisation");
75 
77  I_fill = I_bin;
80  d3.init(I_fill, 0, I.getHeight() + 80, "Fill holes");
81 
83  vpImage<unsigned char> I_open = I_fill;
84  vpImageMorphology::erosion<unsigned char>(I_open, vpImageMorphology::CONNEXITY_4);
85  vpImageMorphology::dilatation<unsigned char>(I_open, vpImageMorphology::CONNEXITY_4);
87 
89  vpImage<unsigned char> I_close = I_open;
90  vpImageMorphology::dilatation<unsigned char>(I_close, vpImageMorphology::CONNEXITY_4);
91  vpImageMorphology::erosion<unsigned char>(I_close, vpImageMorphology::CONNEXITY_4);
93  d4.init(I_close, I.getWidth(), I.getHeight() + 80, "Closing");
94 
96  vpImage<unsigned char> I_contours(I_close.getHeight(), I_close.getWidth());
97  for (unsigned int cpt = 0; cpt < I_close.getSize(); cpt++)
98  I_contours.bitmap[cpt] = I_close.bitmap[cpt] ? 1 : 0;
99 
100  VISP_NAMESPACE_NAME::vpContour vp_contours;
101  std::vector<std::vector<vpImagePoint> > contours;
104 
106  vpImage<vpRGBa> I_draw_contours(I_contours.getHeight(), I_contours.getWidth(), vpRGBa());
107  VISP_NAMESPACE_NAME::drawContours(I_draw_contours, contours, vpColor::red);
109  d5.init(I_draw_contours, 0, 2 * I.getHeight() + 80, "Contours");
110 
112  vpDisplay::display(I_bin);
113  vpDisplay::display(I_fill);
114  vpDisplay::display(I_close);
115  vpDisplay::display(I_draw_contours);
116 
118  int nb_coins = 0;
119  for (size_t i = 0; i < contours.size(); i++) {
120  std::vector<vpPoint> vec_p;
121 
122  for (size_t j = 0; j < contours[i].size(); j++) {
123  vpPoint pt;
124  pt.set_x(contours[i][j].get_u());
125  pt.set_y(contours[i][j].get_v());
126  vec_p.push_back(pt);
127  }
128 
129  vpMomentObject obj(1);
130  obj.setType(vpMomentObject::DENSE_POLYGON);
131  obj.fromVector(vec_p);
132 
133  // sign(m00) depends of the contour orientation (clockwise or
134  // counter-clockwise) that's why we use fabs
135  if (std::fabs(obj.get(0, 0)) >= I.getSize() / 200) {
136  nb_coins++;
137  std::stringstream ss;
138  ss << "Coin " << nb_coins;
139 
140  int centroid_x = (int)std::fabs(obj.get(1, 0) / obj.get(0, 0));
141  int centroid_y = (int)std::fabs(obj.get(0, 1) / obj.get(0, 0));
142  vpDisplay::displayText(I_draw_contours, centroid_y, centroid_x - 20, ss.str(), vpColor::red);
143  }
144  }
146 
147  vpDisplay::displayText(I_draw_contours, 20, 20, "Click to quit.", vpColor::red);
148 
149  vpDisplay::flush(I);
150  vpDisplay::flush(I_bin);
151  vpDisplay::flush(I_fill);
152  vpDisplay::flush(I_close);
153  vpDisplay::flush(I_draw_contours);
154  vpDisplay::getClick(I_draw_contours);
155 #else
156  (void)argc;
157  (void)argv;
158 #endif
159  return EXIT_SUCCESS;
160 }
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
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
unsigned int getSize() const
Definition: vpImage.h:221
Type * bitmap
points toward the bitmap
Definition: vpImage.h:135
unsigned int getHeight() const
Definition: vpImage.h:181
Class for generic objects.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:464
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:466
Definition: vpRGBa.h:65
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 void fillHoles(VISP_NAMESPACE_ADDRESSING vpImage< unsigned char > &I)
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)