Visual Servoing Platform  version 3.6.1 under development (2024-11-24)
tutorial-autothreshold.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/imgproc/vpImgproc.h>
16 #endif
17 
18 int main(int argc, const char **argv)
19 {
21 #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 = "grid36-03.pgm";
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]) == "--help" || std::string(argv[i]) == "-h") {
36  std::cout << "Usage: " << argv[0] << " [--input <input image>] [--help]" << std::endl;
37  return EXIT_SUCCESS;
38  }
39  }
40 
42  vpImageIo::read(I, input_filename);
43 
44  vpImage<unsigned char> I_res(3 * I.getHeight(), 3 * I.getWidth());
45  I_res.insert(I, vpImagePoint(I.getHeight(), I.getWidth()));
46 
47 #ifdef VISP_HAVE_X11
48  vpDisplayX d;
49 #elif defined(VISP_HAVE_GDI)
50  vpDisplayGDI d;
51 #elif defined(HAVE_OPENCV_HIGHGUI)
53 #endif
55  d.init(I_res);
56 
58  vpImage<unsigned char> I_huang = I;
61  I_res.insert(I_huang, vpImagePoint());
62 
64  vpImage<unsigned char> I_intermodes = I;
67  I_res.insert(I_intermodes, vpImagePoint(0, I.getWidth()));
68 
70  vpImage<unsigned char> I_isodata = I;
73  I_res.insert(I_isodata, vpImagePoint(0, 2 * I.getWidth()));
74 
76  vpImage<unsigned char> I_mean = I;
79  I_res.insert(I_mean, vpImagePoint(I.getHeight(), 0));
80 
82  vpImage<unsigned char> I_otsu = I;
85  I_res.insert(I_otsu, vpImagePoint(I.getHeight(), 2 * I.getWidth()));
86 
88  vpImage<unsigned char> I_triangle = I;
91  I_res.insert(I_triangle, vpImagePoint(2 * I.getHeight(), 0));
92 
93  vpDisplay::display(I_res);
94 
95  vpDisplay::displayText(I_res, 30, 20, "Huang", vpColor::red);
96  vpDisplay::displayText(I_res, 30, 20 + I.getWidth(), "Intermodes", vpColor::red);
97  vpDisplay::displayText(I_res, 30, 20 + 2 * I.getWidth(), "IsoData", vpColor::red);
98  vpDisplay::displayText(I_res, 30 + I.getHeight(), 20, "Mean", vpColor::red);
99  vpDisplay::displayText(I_res, 30 + I.getHeight(), 20 + I.getWidth(), "Original", vpColor::red);
100  vpDisplay::displayText(I_res, 30 + I.getHeight(), 20 + 2 * I.getWidth(), "Otsu", vpColor::red);
101  vpDisplay::displayText(I_res, 30 + 2 * I.getHeight(), 20, "Triangle", vpColor::red);
102 
103  vpDisplay::flush(I_res);
104  vpDisplay::getClick(I_res);
105 #else
106  (void)argc;
107  (void)argv;
108 #endif
109  return EXIT_SUCCESS;
110 }
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
void setDownScalingFactor(unsigned int scale)
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
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
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)