Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
tutorial-count-coins.cpp
1
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)
13#include <visp3/core/vpMomentObject.h>
14#include <visp3/imgproc/vpImgproc.h>
16#endif
17
18int 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))
23
24 std::string input_filename = "coins1.jpg";
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(HAVE_OPENCV_HIGHGUI)
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;
76 vpImageMorphology::erosion<unsigned char>(I_open, vpImageMorphology::CONNEXITY_4);
77 vpImageMorphology::dilatation<unsigned char>(I_open, vpImageMorphology::CONNEXITY_4);
79
81 vpImage<unsigned char> I_close = I_open;
82 vpImageMorphology::dilatation<unsigned char>(I_close, vpImageMorphology::CONNEXITY_4);
83 vpImageMorphology::erosion<unsigned char>(I_close, vpImageMorphology::CONNEXITY_4);
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);
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
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#else
148 (void)argc;
149 (void)argv;
150#endif
151 return EXIT_SUCCESS;
152}
static const vpColor red
Definition vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
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 of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:223
Type * bitmap
points toward the bitmap
Definition vpImage.h:139
unsigned int getHeight() const
Definition vpImage.h:184
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:77
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:508
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:510
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)
VISP_EXPORT void drawContours(vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &contours, unsigned char grayValue=255)
VISP_EXPORT void fillHoles(vpImage< unsigned char > &I)
Definition vpMorph.cpp:45
VISP_EXPORT unsigned char autoThreshold(vpImage< unsigned char > &I, const vp::vpAutoThresholdMethod &method, const unsigned char backgroundValue=0, const unsigned char foregroundValue=255)
@ CONTOUR_RETR_EXTERNAL
Definition vpContours.h:201
vpAutoThresholdMethod
Definition vpImgproc.h:66
@ AUTO_THRESHOLD_OTSU
Definition vpImgproc.h:83