Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testDisplayScaled.cpp
1 #include <sstream>
2 
3 #include <visp3/gui/vpDisplayD3D.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayGTK.h>
6 #include <visp3/gui/vpDisplayOpenCV.h>
7 #include <visp3/gui/vpDisplayX.h>
8 #include <visp3/io/vpImageIo.h>
9 #include <visp3/core/vpImageTools.h>
10 #include <visp3/core/vpIoTools.h>
11 
12 template<typename Type>
13 bool test(const std::string &display, vpImage<Type> &I, unsigned int scale, bool click)
14 {
15  bool success = true;
16  unsigned int radius(I.getHeight()/4);
17  int scale_ = (int)scale;
18  int radius_ = (int)radius;
19  unsigned int thickness = 2;
20  vpImagePoint center(I.getHeight()/2, I.getWidth()/2);
21  vpImagePoint offset(30, 160);
22  vpImagePoint v_offset(radius, 0);
23  vpImagePoint h_offset(0, radius);
24  vpRect roi(center, radius_+scale_, radius_);
25  std::string itype;
26 
27  // backup the input image
28  vpImage<Type> Ibackup(I);
29 
30  vpDisplay *d = NULL;
31  if (display == "GDI") {
32 #ifdef VISP_HAVE_GDI
33  d = new vpDisplayGDI;
34 #endif
35  }
36  else if (display == "GTK") {
37 #ifdef VISP_HAVE_GTK
38  d = new vpDisplayGTK;
39 #endif
40  }
41  else if (display == "X") {
42 #ifdef VISP_HAVE_X11
43  d = new vpDisplayX;
44 #endif
45  }
46  else if (display == "OpenCV") {
47 #ifdef VISP_HAVE_OPENCV
48  d = new vpDisplayOpenCV;
49 #endif
50  }
51  else if (display == "D3D9") {
52 #ifdef VISP_HAVE_D3D9
53  d = new vpDisplayD3D;
54 #endif
55  }
56  std::cout << "Start test for " << display << " renderer..." << std::endl;
57  std::cout << " Screen resolution: " << d->getScreenWidth() << " " << d->getScreenHeight() << std::endl;
58  d->setDownScalingFactor(scale);
59  d->init(I);
62 
63  vpImage<Type> crop;
64  vpImageTools::crop(I, vpImagePoint(0, 245), (unsigned int)roi.getHeight(), (unsigned int)roi.getWidth(), crop);
65  I.insert(crop, roi.getTopLeft());
68 
69  // Compare input and rendered images
70  if (sizeof(Type) == 1) {
71  itype = "uchar";
72  vpImage<Type> Iinsert = I;
73  vpImage<vpRGBa> Isampled;
74  vpImage<vpRGBa> Icolor;
75  vpImageConvert::convert(Iinsert, Icolor);
76  Icolor.subsample(scale, scale, Isampled);
77 
78  vpImage<vpRGBa> Irendered;
79  vpDisplay::getImage(I, Irendered);
80 
81  if (Isampled != Irendered) {
82  success = false;
83  std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
84 
85  std::stringstream ss;
86  ss << "Isampled-" << itype << "-scale-" << scale;
87  #ifdef VISP_HAVE_OPENCV
88  ss << ".png";
89  #else
90  ss << ".ppm";
91  #endif
92 
93  vpImageIo::write(Isampled, ss.str());
94 
95  ss.str(""); ss.clear();
96  ss << "Irendered-" << itype << "-scale-" << scale;
97  #ifdef VISP_HAVE_OPENCV
98  ss << ".png";
99  #else
100  ss << ".ppm";
101  #endif
102 
103  vpImageIo::write(Irendered, ss.str());
104  vpImage<vpRGBa> Idiff;
105  vpImageTools::imageDifference(Isampled, Irendered, Idiff);
106 
107  ss.str(""); ss.clear();
108  ss << "Idiff-" << itype << "-scale-" << scale;
109  #ifdef VISP_HAVE_OPENCV
110  ss << ".png";
111  #else
112  ss << ".ppm";
113  #endif
114 
115  vpImageIo::write(Idiff, ss.str());
116  }
117  else {
118  std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
119  }
120  }
121  else {
122  itype = "rgba";
123  vpImage<Type> Iinsert = I;
124  vpImage<Type> Isampled; // vpRGBa necessary
125  Iinsert.subsample(scale, scale, Isampled);
126 
127  vpImage<vpRGBa> Irendered; // vpRGBa necessary
128  vpDisplay::getImage(I, Irendered);
129 
130  vpImage<vpRGBa> IsampledCopy; // vpRGBa necessary
131 
132  vpImageConvert::convert(Isampled, IsampledCopy);
133  if (IsampledCopy != Irendered) {
134  success = false;
135  std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
136 
137  std::stringstream ss;
138  ss << "Isampled-" << itype << "-scale-" << scale;
139  #ifdef VISP_HAVE_OPENCV
140  ss << ".png";
141  #else
142  ss << ".ppm";
143  #endif
144 
145  vpImageIo::write(Isampled, ss.str());
146 
147  ss.str(""); ss.clear();
148  ss << "Irendered-" << itype << "-scale-" << scale;
149  #ifdef VISP_HAVE_OPENCV
150  ss << ".png";
151  #else
152  ss << ".ppm";
153  #endif
154 
155  vpImageIo::write(Irendered, ss.str());
156  vpImage<vpRGBa> Idiff;
157  vpImageTools::imageDifference(IsampledCopy, Irendered, Idiff);
158  ss.str(""); ss.clear();
159  ss << "Idiff-" << itype << "-scale-" << scale;
160  #ifdef VISP_HAVE_OPENCV
161  ss << ".png";
162  #else
163  ss << ".ppm";
164  #endif
165 
166  vpImageIo::write(Idiff, ss.str());
167 
168  }
169  else {
170  std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
171  }
172  }
173 
174  vpDisplay::displayRectangle(I, center-v_offset-h_offset, radius, radius, vpColor::blue, false, thickness);
175  vpDisplay::displayRectangle(I, center, center+v_offset+h_offset, vpColor::blue, false, thickness);
176  vpDisplay::displayRectangle(I, vpRect(center-v_offset-h_offset, center+v_offset+h_offset), vpColor::blue, false, thickness);
177  vpDisplay::displayRectangle(I, center - v_offset*3./2 + h_offset, radius/2, radius/2, vpColor::green, true);
178  vpDisplay::displayCircle (I, center, radius, vpColor::blue, false, thickness);
179  vpDisplay::displayArrow (I, center, center-v_offset/4-h_offset, vpColor::red, 10, 6, thickness);
180  vpDisplay::displayCross (I, center-radius/2., radius, vpColor::green, thickness);
181  vpDisplay::displayDotLine(I, center-v_offset-h_offset, center, vpColor::cyan, thickness);
182  vpDisplay::displayLine (I, center+v_offset-h_offset, center-v_offset+h_offset, vpColor::cyan, thickness);
183  int nbpoints = (int)(radius*sqrt(2.)/8/scale);
184  for (int i=0; i< nbpoints; i++) {
185  vpDisplay::displayPoint(I, center-h_offset/2.+vpImagePoint(-i*radius_/(nbpoints*2), i*radius_/(nbpoints*2)), vpColor::cyan);
186  vpDisplay::displayPoint(I, center-h_offset+vpImagePoint(-i*radius_/nbpoints, i*radius_/nbpoints), 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 
200  std::stringstream ss;
201  ss << "overlay-" << display << "-" << itype << "-scale-" << scale;
202 #ifdef VISP_HAVE_OPENCV
203  ss << ".png";
204 #else
205  ss << ".ppm";
206 #endif
207  std::cout << " Overlay saved in: " << ss.str() << std::endl;
208  vpImageIo::write(Irendered, ss.str());
209 
210  if (click)
212 
213  // Restore the input image
214  I = Ibackup;
215 
216  vpDisplay::close(I);
217 
218  if (d != NULL)
219  delete d;
220 
221  if (success)
222  return true;
223  else
224  return false;
225 }
226 
227 int main(int argc, const char *argv[])
228 {
229  bool opt_click = true;
230  bool opt_display = true;
231  std::string opt_ipath;
232  std::string env_ipath;
233  std::string ipath;
234 
235  for (int i=0; i<argc; i++) {
236  if (std::string(argv[i]) == "-c")
237  opt_click = false;
238  else if (std::string(argv[i]) == "-d")
239  opt_display = false;
240  else if (std::string(argv[i]) == "-i")
241  opt_ipath = std::string(argv[i+1]);
242  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
243  std::cout << "\nUsage: " << argv[0] << " [-i <image path>] [-c] [-d] [--help]\n" << std::endl;
244  std::cout << "\nOptions: " << std::endl;
245  std::cout << " -i <input image path> : set image input path.\n"
246  << " From this path read \"ViSP-images/Klimt/Klimt.pgm\" image.\n"
247  << " Setting the VISP_INPUT_IMAGE_PATH environment\n"
248  << " variable produces the same behaviour than using\n"
249  << " this option." << std::endl;
250  std::cout << " -c : disable mouse click" << std::endl;
251  std::cout << " -d : disable display" << std::endl;
252  std::cout << " -h, --help : print this help\n" << std::endl;
253  return 0;
254  }
255  }
256 
257  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH 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, "ViSP-images/Klimt/Klimt.pgm");
294  vpImageIo::read(I, filename);
295 
296  vpImage<vpRGBa> C;
297  filename = vpIoTools::createFilePath(ipath, "ViSP-images/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) ) nbfailure ++;
306  if (! test(display[i], C, scale, opt_click) ) nbfailure ++;
307  }
308  }
309  if (nbfailure == 0)
310  std::cout << "Test succeed" << std::endl;
311  else
312  std::cout << "Test failed with " << nbfailure << " failures" << std::endl;
313  }
314 
315  return 0;
316 }
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
static void close(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:226
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:248
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:153
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:166
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
static const vpColor cyan
Definition: vpColor.h:172
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 std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:155
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:1338
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)
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
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:266
void insert(const vpImage< Type > &src, const vpImagePoint topLeft)
Definition: vpImage.h:1226
unsigned int getHeight() const
Definition: vpImage.h:175
Defines a rectangle in the plane.
Definition: vpRect.h:82
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)
Definition of the vpImage class member functions.
Definition: vpImage.h:117
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:169