File: tutorial-image-filter.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (125 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! \example tutorial-image-filter.cpp

#include <visp3/core/vpImageFilter.h>
#include <visp3/gui/vpDisplayD3D.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>

void display(vpImage<unsigned char> &I, const std::string &title);
void display(vpImage<double> &D, const std::string &title);

void display(vpImage<unsigned char> &I, const std::string &title)
{
#if defined(VISP_HAVE_X11)
  vpDisplayX d(I);
#elif defined(HAVE_OPENCV_HIGHGUI)
  vpDisplayOpenCV d(I);
#elif defined(VISP_HAVE_GTK)
  vpDisplayGTK d(I);
#elif defined(VISP_HAVE_GDI)
  vpDisplayGDI d(I);
#elif defined(VISP_HAVE_D3D9)
  vpDisplayD3D d(I);
#else
  std::cout << "No image viewer is available..." << std::endl;
#endif

  vpDisplay::setTitle(I, title);
  vpDisplay::display(I);
  vpDisplay::displayText(I, 15, 15, "Click to continue...", vpColor::red);
  vpDisplay::flush(I);
  vpDisplay::getClick(I);
}

void display(vpImage<double> &D, const std::string &title)
{
  vpImage<unsigned char> I; // Image to display
  vpImageConvert::convert(D, I);
  display(I, title);
}

int main(int argc, char **argv)
{
  try {
    if (argc != 2) {
      printf("Usage: %s <image name.[pgm,ppm,jpeg,png,bmp]>\n", argv[0]);
      return EXIT_FAILURE;
    }
    //! [vpImage construction]
    vpImage<unsigned char> I;
    //! [vpImage construction]

    try {
      vpImageIo::read(I, argv[1]);
    }
    catch (...) {
      std::cout << "Cannot read image \"" << argv[1] << "\"" << std::endl;
      return EXIT_FAILURE;
    }

    display(I, "Original image");

    //! [Gaussian blur]
    vpImage<double> F;
    vpImageFilter::gaussianBlur(I, F);
    //! [Gaussian blur]
    display(F, "Blur (default)");

    vpImageFilter::gaussianBlur(I, F, 7, 2.);
    display(F, "Blur (var=2)");

    //! [Gradients x]
    vpImage<double> dIx;
    vpImageFilter::getGradX(I, dIx);
    //! [Gradients x]
    display(dIx, "Gradient dIx");

    //! [Gradients y]
    vpImage<double> dIy;
    vpImageFilter::getGradY(I, dIy);
    //! [Gradients y]
    display(dIy, "Gradient dIy");

    //! [Canny]
    vpImage<unsigned char> C;
    vpImageFilter::canny(I, C, 5, -1., 3);
    display(C, "Canny");
    //! [Canny]

    //! [Convolution kernel]
    vpMatrix K(3, 3); // Sobel kernel along x
    K[0][0] = 1;
    K[0][1] = 0;
    K[0][2] = -1;
    K[1][0] = 2;
    K[1][1] = 0;
    K[1][2] = -2;
    K[2][0] = 1;
    K[2][1] = 0;
    K[2][2] = -1;
    //! [Convolution kernel]
    //! [Convolution]
    vpImage<double> Gx;
    vpImageFilter::filter(I, Gx, K);
    //! [Convolution]
    display(Gx, "Sobel x");

    //! [Gaussian pyramid]
    size_t nlevel = 3;
    std::vector<vpImage<unsigned char> > pyr(nlevel);
    pyr[0] = I;
    for (size_t i = 1; i < nlevel; i++) {
      vpImageFilter::getGaussPyramidal(pyr[i - 1], pyr[i]);
      display(pyr[i], "Pyramid");
    }
    //! [Gaussian pyramid]
    return EXIT_SUCCESS;
  }
  catch (const vpException &e) {
    std::cout << "Catch an exception: " << e << std::endl;
    return EXIT_FAILURE;
  }
}