Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-count-coins.cpp
1
2
3#include <cstdlib>
4#include <iostream>
5#include <visp3/core/vpConfig.h>
6#include <visp3/core/vpImage.h>
7#include <visp3/gui/vpDisplayFactory.h>
8#include <visp3/io/vpImageIo.h>
9
10#if defined(VISP_HAVE_MODULE_IMGPROC)
12#include <visp3/core/vpMomentObject.h>
13#include <visp3/imgproc/vpImgproc.h>
15#endif
16
17int main(int argc, char *argv[])
18{
20#if defined(VISP_HAVE_MODULE_IMGPROC) && defined(VISP_HAVE_DISPLAY)
22
23#ifdef ENABLE_VISP_NAMESPACE
24 using namespace VISP_NAMESPACE_NAME;
25#endif
26
27 std::string input_filename = "coins1.jpg";
29 bool white_foreground = false;
30
31 for (int i = 1; i < argc; i++) {
32 if (std::string(argv[i]) == "--input" && i + 1 < argc) {
33 input_filename = std::string(argv[i + 1]);
34 }
35 else if (std::string(argv[i]) == "--method" && i + 1 < argc) {
36 method = static_cast<VISP_NAMESPACE_NAME::vpAutoThresholdMethod>(atoi(argv[i + 1]));
37 }
38 else if (std::string(argv[i]) == "--white_foreground") {
39 white_foreground = true;
40 }
41 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
42 std::cout << "Usage: " << argv[0]
43 << " [--input <input image>]"
44 " [--method <0: Huang, 1: Intermodes, 2: IsoData, 3: "
45 "Mean, 4: Otsu, 5: Triangle>]"
46 " [--white_foreground]"
47 " [--help]"
48 << std::endl;
49 return EXIT_SUCCESS;
50 }
51 }
52
55 vpImageIo::read(I, input_filename);
57
58#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
59 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(), display2 = vpDisplayFactory::createDisplay(),
61#else
67#endif
68 display->init(I, 0, 0, "Coins");
69
70 vpImage<unsigned char> I_bin, I_fill;
72 I_bin = I;
73 VISP_NAMESPACE_NAME::autoThreshold(I_bin, method, white_foreground ? 0 : 255, white_foreground ? 255 : 0);
75 display2->init(I_bin, I.getWidth(), 0, "Binarisation");
76
78 I_fill = I_bin;
81 display3->init(I_fill, 0, I.getHeight() + 80, "Fill holes");
82
84 vpImage<unsigned char> I_open = I_fill;
88
90 vpImage<unsigned char> I_close = I_open;
94 display4->init(I_close, I.getWidth(), I.getHeight() + 80, "Closing");
95
97 vpImage<unsigned char> I_contours(I_close.getHeight(), I_close.getWidth());
98 for (unsigned int cpt = 0; cpt < I_close.getSize(); cpt++)
99 I_contours.bitmap[cpt] = I_close.bitmap[cpt] ? 1 : 0;
100
102 std::vector<std::vector<vpImagePoint> > contours;
105
107 vpImage<vpRGBa> I_draw_contours(I_contours.getHeight(), I_contours.getWidth(), vpRGBa());
108 VISP_NAMESPACE_NAME::drawContours(I_draw_contours, contours, vpColor::red);
110 display5->init(I_draw_contours, 0, 2 * I.getHeight() + 80, "Contours");
111
113 vpDisplay::display(I_bin);
114 vpDisplay::display(I_fill);
115 vpDisplay::display(I_close);
116 vpDisplay::display(I_draw_contours);
117
119 int nb_coins = 0;
120 for (size_t i = 0; i < contours.size(); i++) {
121 std::vector<vpPoint> vec_p;
122
123 for (size_t j = 0; j < contours[i].size(); j++) {
124 vpPoint pt;
125 pt.set_x(contours[i][j].get_u());
126 pt.set_y(contours[i][j].get_v());
127 vec_p.push_back(pt);
128 }
129
130 vpMomentObject obj(1);
132 obj.fromVector(vec_p);
133
134 // sign(m00) depends of the contour orientation (clockwise or
135 // counter-clockwise) that's why we use fabs
136 if (std::fabs(obj.get(0, 0)) >= I.getSize() / 200) {
137 nb_coins++;
138 std::stringstream ss;
139 ss << "Coin " << nb_coins;
140
141 int centroid_x = static_cast<int>(std::fabs(obj.get(1, 0) / obj.get(0, 0)));
142 int centroid_y = static_cast<int>(std::fabs(obj.get(0, 1) / obj.get(0, 0)));
143 vpDisplay::displayText(I_draw_contours, centroid_y, centroid_x - 20, ss.str(), vpColor::red);
144 }
145 }
147
148 vpDisplay::displayText(I_draw_contours, 20, 20, "Click to quit.", vpColor::red);
149
151 vpDisplay::flush(I_bin);
152 vpDisplay::flush(I_fill);
153 vpDisplay::flush(I_close);
154 vpDisplay::flush(I_draw_contours);
155 vpDisplay::getClick(I_draw_contours);
156
157#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11) && defined(VISP_HAVE_DISPLAY)
158 if (display != nullptr) {
159 delete display;
160 }
161
162 if (display2 != nullptr) {
163 delete display2;
164 }
165
166 if (display3 != nullptr) {
167 delete display3;
168 }
169
170 if (display4 != nullptr) {
171 delete display4;
172 }
173
174 if (display5 != nullptr) {
175 delete display5;
176 }
177#endif
178#else
179 (void)argc;
180 (void)argv;
181#endif
182 return EXIT_SUCCESS;
183}
static const vpColor red
Definition vpColor.h:198
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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)
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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:471
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.