1 //! \example tutorial-autothreshold.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/imgproc/vpImgproc.h>
14 //! [Include]
15 #endif
16 
main(int argc,const char ** argv)17 int main(int argc, const char **argv)
18 {
19 //! [Macro defined]
20 #if defined(VISP_HAVE_MODULE_IMGPROC) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
21   //! [Macro defined]
22   //!
23   std::string input_filename = "grid36-03.pgm";
24 
25   for (int i = 1; i < argc; i++) {
26     if (std::string(argv[i]) == "--input" && i + 1 < argc) {
27       input_filename = std::string(argv[i + 1]);
28     } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
29       std::cout << "Usage: " << argv[0] << " [--input <input image>] [--help]" << std::endl;
30       return EXIT_SUCCESS;
31     }
32   }
33 
34   vpImage<unsigned char> I;
35   vpImageIo::read(I, input_filename);
36 
37   vpImage<unsigned char> I_res(3 * I.getHeight(), 3 * I.getWidth());
38   I_res.insert(I, vpImagePoint(I.getHeight(), I.getWidth()));
39 
40 #ifdef VISP_HAVE_X11
41   vpDisplayX d;
42 #elif defined(VISP_HAVE_GDI)
43   vpDisplayGDI d;
44 #elif defined(VISP_HAVE_OPENCV)
45   vpDisplayOpenCV d;
46 #endif
47   d.setDownScalingFactor(vpDisplay::SCALE_2);
48   d.init(I_res);
49 
50   //! [Huang]
51   vpImage<unsigned char> I_huang = I;
52   vp::autoThreshold(I_huang, vp::AUTO_THRESHOLD_HUANG);
53   //! [Huang]
54   I_res.insert(I_huang, vpImagePoint());
55 
56   //! [Intermodes]
57   vpImage<unsigned char> I_intermodes = I;
58   vp::autoThreshold(I_intermodes, vp::AUTO_THRESHOLD_INTERMODES);
59   //! [Intermodes]
60   I_res.insert(I_intermodes, vpImagePoint(0, I.getWidth()));
61 
62   //! [IsoData]
63   vpImage<unsigned char> I_isodata = I;
64   vp::autoThreshold(I_isodata, vp::AUTO_THRESHOLD_ISODATA);
65   //! [IsoData]
66   I_res.insert(I_isodata, vpImagePoint(0, 2 * I.getWidth()));
67 
68   //! [Mean]
69   vpImage<unsigned char> I_mean = I;
70   vp::autoThreshold(I_mean, vp::AUTO_THRESHOLD_MEAN);
71   //! [Mean]
72   I_res.insert(I_mean, vpImagePoint(I.getHeight(), 0));
73 
74   //! [Otsu]
75   vpImage<unsigned char> I_otsu = I;
76   vp::autoThreshold(I_otsu, vp::AUTO_THRESHOLD_OTSU);
77   //! [Otsu]
78   I_res.insert(I_otsu, vpImagePoint(I.getHeight(), 2 * I.getWidth()));
79 
80   //! [Triangle]
81   vpImage<unsigned char> I_triangle = I;
82   vp::autoThreshold(I_triangle, vp::AUTO_THRESHOLD_TRIANGLE);
83   //! [Triangle]
84   I_res.insert(I_triangle, vpImagePoint(2 * I.getHeight(), 0));
85 
86   vpDisplay::display(I_res);
87 
88   vpDisplay::displayText(I_res, 30, 20, "Huang", vpColor::red);
89   vpDisplay::displayText(I_res, 30, 20 + I.getWidth(), "Intermodes", vpColor::red);
90   vpDisplay::displayText(I_res, 30, 20 + 2 * I.getWidth(), "IsoData", vpColor::red);
91   vpDisplay::displayText(I_res, 30 + I.getHeight(), 20, "Mean", vpColor::red);
92   vpDisplay::displayText(I_res, 30 + I.getHeight(), 20 + I.getWidth(), "Original", vpColor::red);
93   vpDisplay::displayText(I_res, 30 + I.getHeight(), 20 + 2 * I.getWidth(), "Otsu", vpColor::red);
94   vpDisplay::displayText(I_res, 30 + 2 * I.getHeight(), 20, "Triangle", vpColor::red);
95 
96   vpDisplay::flush(I_res);
97   vpDisplay::getClick(I_res);
98 
99   return EXIT_SUCCESS;
100 #else
101   (void)argc;
102   (void)argv;
103   return 0;
104 #endif
105 }
106