Visual Servoing Platform  version 3.1.0
tutorial-image-filter.cpp
1 
3 #include <visp3/core/vpImageFilter.h>
4 #include <visp3/gui/vpDisplayD3D.h>
5 #include <visp3/gui/vpDisplayGDI.h>
6 #include <visp3/gui/vpDisplayGTK.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #include <visp3/io/vpImageIo.h>
10 
11 void display(vpImage<unsigned char> &I, const std::string &title);
12 void display(vpImage<double> &D, const std::string &title);
13 
14 void display(vpImage<unsigned char> &I, const std::string &title)
15 {
16 #if defined(VISP_HAVE_X11)
17  vpDisplayX d(I);
18 #elif defined(VISP_HAVE_OPENCV)
19  vpDisplayOpenCV d(I);
20 #elif defined(VISP_HAVE_GTK)
21  vpDisplayGTK d(I);
22 #elif defined(VISP_HAVE_GDI)
23  vpDisplayGDI d(I);
24 #elif defined(VISP_HAVE_D3D9)
25  vpDisplayD3d d(I);
26 #else
27  std::cout << "No image viewer is available..." << std::endl;
28 #endif
29 
30  vpDisplay::setTitle(I, title.c_str());
32  vpDisplay::displayText(I, 15, 15, "Click to continue...", vpColor::red);
35 }
36 
37 void display(vpImage<double> &D, const std::string &title)
38 {
39  vpImage<unsigned char> I; // Image to display
41  display(I, title);
42 }
43 
44 int main(int argc, char **argv)
45 {
46  try {
47  if (argc != 2) {
48  printf("Usage: %s <image name.[pgm,ppm,jpeg,png,bmp]>\n", argv[0]);
49  return -1;
50  }
54 
55  try {
56  vpImageIo::read(I, argv[1]);
57  } catch (...) {
58  std::cout << "Cannot read image \"" << argv[1] << "\"" << std::endl;
59  return -1;
60  }
61 
62  display(I, "Original image");
63 
68  display(F, "Blur (default)");
69 
70  vpImageFilter::gaussianBlur(I, F, 7, 2);
71  display(F, "Blur (var=2)");
72 
74  vpImage<double> dIx;
77  display(dIx, "Gradient dIx");
78 
80  vpImage<double> dIy;
83  display(dIy, "Gradient dIy");
84 
86 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
88  vpImageFilter::canny(I, C, 5, 15, 3);
89  display(C, "Canny");
90 #endif
91 
94  vpMatrix K(3, 3); // Sobel kernel along x
95  K[0][0] = 1;
96  K[0][1] = 0;
97  K[0][2] = -1;
98  K[1][0] = 2;
99  K[1][1] = 0;
100  K[1][2] = -2;
101  K[2][0] = 1;
102  K[2][1] = 0;
103  K[2][2] = -1;
106  vpImage<double> Gx;
107  vpImageFilter::filter(I, Gx, K);
109  display(Gx, "Sobel x");
110 
112  size_t nlevel = 3;
113  std::vector<vpImage<unsigned char> > pyr(nlevel);
114  pyr[0] = I;
115  for (size_t i = 1; i < nlevel; i++) {
116  vpImageFilter::getGaussPyramidal(pyr[i - 1], pyr[i]);
117  display(pyr[i], "Pyramid");
118  }
120  return 0;
121  } catch (vpException &e) {
122  std::cout << "Catch an exception: " << e << std::endl;
123  return 1;
124  }
125 }
static void getGaussPyramidal(const vpImage< unsigned char > &I, vpImage< unsigned char > &GI)
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
static void getGradY(const vpImage< unsigned char > &I, vpImage< double > &dIy)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:180
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static void gaussianBlur(const vpImage< unsigned char > &I, vpImage< double > &GI, unsigned int size=7, double sigma=0., bool normalize=true)
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
static void filter(const vpImage< double > &I, vpImage< double > &Iu, vpImage< double > &Iv, const vpMatrix &M, const bool convolve=false)
static void canny(const vpImage< unsigned char > &I, vpImage< unsigned char > &Ic, const unsigned int gaussianFilterSize, const double thresholdCanny, const unsigned int apertureSobel)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)