Visual Servoing Platform  version 3.1.0
testDisplayScaled.cpp
1 #include <sstream>
2 
3 #include <visp3/core/vpImageTools.h>
4 #include <visp3/core/vpIoTools.h>
5 #include <visp3/gui/vpDisplayD3D.h>
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayGTK.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/io/vpImageIo.h>
11 
12 template <typename Type> bool test(const std::string &display, vpImage<Type> &I, unsigned int scale, bool click)
13 {
14  bool success = true;
15  unsigned int radius(I.getHeight() / 4);
16  int scale_ = (int)scale;
17  int radius_ = (int)radius;
18  unsigned int thickness = 2;
19  vpImagePoint center(I.getHeight() / 2, I.getWidth() / 2);
20  vpImagePoint offset(30, 160);
21  vpImagePoint v_offset(radius, 0);
22  vpImagePoint h_offset(0, radius);
23  vpRect roi(center, radius_ + scale_, radius_);
24  std::string itype;
25 
26  // backup the input image
27  vpImage<Type> Ibackup(I);
28 
29  vpDisplay *d = NULL;
30  if (display == "GDI") {
31 #ifdef VISP_HAVE_GDI
32  d = new vpDisplayGDI;
33 #endif
34  } else if (display == "GTK") {
35 #ifdef VISP_HAVE_GTK
36  d = new vpDisplayGTK;
37 #endif
38  } else if (display == "X") {
39 #ifdef VISP_HAVE_X11
40  d = new vpDisplayX;
41 #endif
42  } else if (display == "OpenCV") {
43 #ifdef VISP_HAVE_OPENCV
44  d = new vpDisplayOpenCV;
45 #endif
46  } else if (display == "D3D9") {
47 #ifdef VISP_HAVE_D3D9
48  d = new vpDisplayD3D;
49 #endif
50  }
51  std::cout << "Start test for " << display << " renderer..." << std::endl;
52  std::cout << " Screen resolution: " << d->getScreenWidth() << " " << d->getScreenHeight() << std::endl;
53  d->setDownScalingFactor(scale);
54  d->init(I);
57 
58  vpImage<Type> crop;
59  vpImageTools::crop(I, vpImagePoint(0, 245), (unsigned int)roi.getHeight(), (unsigned int)roi.getWidth(), crop);
60  I.insert(crop, roi.getTopLeft());
63 
64  // Compare input and rendered images
65  if (sizeof(Type) == 1) {
66  itype = "uchar";
67  vpImage<Type> Iinsert = I;
68  vpImage<vpRGBa> Isampled;
69  vpImage<vpRGBa> Icolor;
70  vpImageConvert::convert(Iinsert, Icolor);
71  Icolor.subsample(scale, scale, Isampled);
72 
73  vpImage<vpRGBa> Irendered;
74  vpDisplay::getImage(I, Irendered);
75 
76  if (Isampled != Irendered) {
77  success = false;
78  std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
79 
80  std::stringstream ss;
81  ss << "Isampled-" << itype << "-scale-" << scale;
82 #ifdef VISP_HAVE_OPENCV
83  ss << ".png";
84 #else
85  ss << ".ppm";
86 #endif
87 
88  vpImageIo::write(Isampled, ss.str());
89 
90  ss.str("");
91  ss.clear();
92  ss << "Irendered-" << itype << "-scale-" << scale;
93 #ifdef VISP_HAVE_OPENCV
94  ss << ".png";
95 #else
96  ss << ".ppm";
97 #endif
98 
99  vpImageIo::write(Irendered, ss.str());
100  vpImage<vpRGBa> Idiff;
101  vpImageTools::imageDifference(Isampled, Irendered, Idiff);
102 
103  ss.str("");
104  ss.clear();
105  ss << "Idiff-" << itype << "-scale-" << scale;
106 #ifdef VISP_HAVE_OPENCV
107  ss << ".png";
108 #else
109  ss << ".ppm";
110 #endif
111 
112  vpImageIo::write(Idiff, ss.str());
113  } else {
114  std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
115  }
116  } else {
117  itype = "rgba";
118  vpImage<Type> Iinsert = I;
119  vpImage<Type> Isampled; // vpRGBa necessary
120  Iinsert.subsample(scale, scale, Isampled);
121 
122  vpImage<vpRGBa> Irendered; // vpRGBa necessary
123  vpDisplay::getImage(I, Irendered);
124 
125  vpImage<vpRGBa> IsampledCopy; // vpRGBa necessary
126 
127  vpImageConvert::convert(Isampled, IsampledCopy);
128  if (IsampledCopy != Irendered) {
129  success = false;
130  std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
131 
132  std::stringstream ss;
133  ss << "Isampled-" << itype << "-scale-" << scale;
134 #ifdef VISP_HAVE_OPENCV
135  ss << ".png";
136 #else
137  ss << ".ppm";
138 #endif
139 
140  vpImageIo::write(Isampled, ss.str());
141 
142  ss.str("");
143  ss.clear();
144  ss << "Irendered-" << itype << "-scale-" << scale;
145 #ifdef VISP_HAVE_OPENCV
146  ss << ".png";
147 #else
148  ss << ".ppm";
149 #endif
150 
151  vpImageIo::write(Irendered, ss.str());
152  vpImage<vpRGBa> Idiff;
153  vpImageTools::imageDifference(IsampledCopy, Irendered, Idiff);
154  ss.str("");
155  ss.clear();
156  ss << "Idiff-" << itype << "-scale-" << scale;
157 #ifdef VISP_HAVE_OPENCV
158  ss << ".png";
159 #else
160  ss << ".ppm";
161 #endif
162 
163  vpImageIo::write(Idiff, ss.str());
164 
165  } else {
166  std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
167  }
168  }
169 
170  vpDisplay::displayRectangle(I, center - v_offset - h_offset, radius, radius, vpColor::blue, false, thickness);
171  vpDisplay::displayRectangle(I, center, center + v_offset + h_offset, vpColor::blue, false, thickness);
172  vpDisplay::displayRectangle(I, vpRect(center - v_offset - h_offset, center + v_offset + h_offset), vpColor::blue,
173  false, thickness);
174  vpDisplay::displayRectangle(I, center - v_offset * 3. / 2 + h_offset, radius / 2, radius / 2, vpColor::green, true);
175  vpDisplay::displayCircle(I, center, radius, vpColor::blue, false, thickness);
176  vpDisplay::displayArrow(I, center, center - v_offset / 4 - h_offset, vpColor::red, 10, 6, thickness);
177  vpDisplay::displayCross(I, center - radius / 2., radius, vpColor::green, thickness);
178  vpDisplay::displayDotLine(I, center - v_offset - h_offset, center, vpColor::cyan, thickness);
179  vpDisplay::displayLine(I, center + v_offset - h_offset, center - v_offset + h_offset, vpColor::cyan, thickness);
180  int nbpoints = (int)(radius * sqrt(2.) / 8 / scale);
181  for (int i = 0; i < nbpoints; i++) {
183  I, center - h_offset / 2. + vpImagePoint(-i * radius_ / (nbpoints * 2), i * radius_ / (nbpoints * 2)),
184  vpColor::cyan);
185  vpDisplay::displayPoint(I, center - h_offset + vpImagePoint(-i * radius_ / nbpoints, i * radius_ / nbpoints),
186  vpColor::cyan, thickness);
187  }
188 
189  if (click)
190  vpDisplay::displayText(I, 10 * scale_, 10 * scale_, "A click to continue", vpColor::red);
191  else
192  vpDisplay::displayText(I, 10 * scale_, 10 * scale_, "This is an image", vpColor::red);
193 
194  vpDisplay::flush(I);
195 
196  vpImage<vpRGBa> Irendered;
197  vpDisplay::getImage(I, Irendered);
198 
199  std::stringstream ss;
200  ss << "overlay-" << display << "-" << itype << "-scale-" << scale;
201 #ifdef VISP_HAVE_OPENCV
202  ss << ".png";
203 #else
204  ss << ".ppm";
205 #endif
206  std::cout << " Overlay saved in: " << ss.str() << std::endl;
207  vpImageIo::write(Irendered, ss.str());
208 
209  if (click)
211 
212  // Restore the input image
213  I = Ibackup;
214 
215  vpDisplay::close(I);
216 
217  if (d != NULL)
218  delete d;
219 
220  if (success)
221  return true;
222  else
223  return false;
224 }
225 
226 int main(int argc, const char *argv[])
227 {
228  bool opt_click = true;
229  bool opt_display = true;
230  std::string opt_ipath;
231  std::string env_ipath;
232  std::string ipath;
233 
234  for (int i = 0; i < argc; i++) {
235  if (std::string(argv[i]) == "-c")
236  opt_click = false;
237  else if (std::string(argv[i]) == "-d")
238  opt_display = false;
239  else if (std::string(argv[i]) == "-i")
240  opt_ipath = std::string(argv[i + 1]);
241  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
242  std::cout << "\nUsage: " << argv[0] << " [-i <image path>] [-c] [-d] [--help]\n" << std::endl;
243  std::cout << "\nOptions: " << std::endl;
244  std::cout << " -i <input image path> : set image input path.\n"
245  << " From this path read \"Klimt/Klimt.pgm\" image.\n"
246  << " Setting the VISP_INPUT_IMAGE_PATH environment\n"
247  << " variable produces the same behaviour than using\n"
248  << " this option." << std::endl;
249  std::cout << " -c : disable mouse click" << std::endl;
250  std::cout << " -d : disable display" << std::endl;
251  std::cout << " -h, --help : print this help\n" << std::endl;
252  return 0;
253  }
254  }
255 
256  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
257  // environment variable value
258  env_ipath = vpIoTools::getViSPImagesDataPath();
259 
260  // Set the default input path
261  if (!env_ipath.empty())
262  ipath = env_ipath;
263 
264  // Get the option values
265  if (!opt_ipath.empty())
266  ipath = opt_ipath;
267 
268  std::string filename;
269 
270  std::vector<std::string> display;
271  if (opt_display) {
272 #ifdef VISP_HAVE_GDI
273  display.push_back("GDI");
274 #endif
275 #ifdef VISP_HAVE_GTK
276  display.push_back("GTK");
277 #endif
278 #ifdef VISP_HAVE_X11
279  display.push_back("X");
280 #endif
281 #ifdef VISP_HAVE_OPENCV
282  display.push_back("OpenCV");
283 #endif
284 #ifdef VISP_HAVE_D3D9
285  display.push_back("D3D9");
286 #endif
287 
288  if (display.size() == 0) {
289  std::cout << "No display available. We stop here." << std::endl;
290  return 0;
291  }
293  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
294  vpImageIo::read(I, filename);
295 
296  vpImage<vpRGBa> C;
297  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
298  vpImageIo::read(C, filename);
299 
300  int nbfailure = 0;
301 
302  for (unsigned int i = 0; i < display.size(); i++) {
303 
304  for (unsigned int scale = 1; scale < 4; scale++) {
305  if (!test(display[i], I, scale, opt_click))
306  nbfailure++;
307  if (!test(display[i], C, scale, opt_click))
308  nbfailure++;
309  }
310  }
311  if (nbfailure == 0)
312  std::cout << "Test succeed" << std::endl;
313  else
314  std::cout << "Test failed with " << nbfailure << " failures" << std::endl;
315  }
316 
317  return 0;
318 }
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1210
static void close(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:232
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:1207
vpDisplayGDI()
Basic constructor.
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
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
static const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:180
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1435
static const vpColor cyan
Definition: vpColor.h:189
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1102
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:144
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
static void crop(const vpImage< Type > &I, double roi_top, double roi_left, unsigned int roi_height, unsigned int roi_width, vpImage< Type > &crop, unsigned int v_scale=1, unsigned int h_scale=1)
Definition: vpImageTools.h:237
Defines a rectangle in the plane.
Definition: vpRect.h:78
static void displayROI(const vpImage< unsigned char > &I, const vpRect &roi)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
unsigned int getWidth() const
Definition: vpImage.h:229
Definition of the vpImage class member functions.
Definition: vpImage.h:116
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor blue
Definition: vpColor.h:186