1 //! \example tutorial-count-coins.cpp
2
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]
13 #include <visp3/core/vpMomentObject.h>
14 #include <visp3/imgproc/vpImgproc.h>
15 //! [Include]
16 #endif
17
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20 //! [Macro defined]
21 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
22 //! [Macro defined]
23
24 std::string input_filename = "coins1.pgm";
25 vp::vpAutoThresholdMethod method = vp::AUTO_THRESHOLD_OTSU;
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
47 //! [Read]
48 vpImage<unsigned char> I;
49 vpImageIo::read(I, input_filename);
50 //! [Read]
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;
62 //! [Binarisation]
63 I_bin = I;
64 vp::autoThreshold(I_bin, method, white_foreground ? 0 : 255, white_foreground ? 255 : 0);
65 //! [Binarisation]
66 d2.init(I_bin, I.getWidth(), 0, "Binarisation");
67
68 //! [Fill holes]
69 I_fill = I_bin;
70 vp::fillHoles(I_fill);
71 //! [Fill holes]
72 d3.init(I_fill, 0, I.getHeight() + 80, "Fill holes");
73
74 //! [Opening]
75 vpImage<unsigned char> I_open = I_fill;
76 vpImageMorphology::erosion(I_open, vpImageMorphology::CONNEXITY_4);
77 vpImageMorphology::dilatation(I_open, vpImageMorphology::CONNEXITY_4);
78 //! [Opening]
79
80 //! [Closing]
81 vpImage<unsigned char> I_close = I_open;
82 vpImageMorphology::dilatation(I_close, vpImageMorphology::CONNEXITY_4);
83 vpImageMorphology::erosion(I_close, vpImageMorphology::CONNEXITY_4);
84 //! [Closing]
85 d4.init(I_close, I.getWidth(), I.getHeight() + 80, "Closing");
86
87 //! [Find contours]
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);
95 //! [Find contours]
96
97 //! [Draw contours]
98 vpImage<vpRGBa> I_draw_contours(I_contours.getHeight(), I_contours.getWidth(), vpRGBa());
99 vp::drawContours(I_draw_contours, contours, vpColor::red);
100 //! [Draw contours]
101 d5.init(I_draw_contours, 0, 2 * I.getHeight() + 80, "Contours");
102
103 vpDisplay::display(I);
104 vpDisplay::display(I_bin);
105 vpDisplay::display(I_fill);
106 vpDisplay::display(I_close);
107 vpDisplay::display(I_draw_contours);
108
109 //! [Count coins]
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 }
137 //! [Count coins]
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 }
155