Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testDisplayScaled.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Display testing.
33 *
34*****************************************************************************/
35
36#include <sstream>
37
38#include <visp3/core/vpImageTools.h>
39#include <visp3/core/vpIoTools.h>
40#include <visp3/gui/vpDisplayD3D.h>
41#include <visp3/gui/vpDisplayGDI.h>
42#include <visp3/gui/vpDisplayGTK.h>
43#include <visp3/gui/vpDisplayOpenCV.h>
44#include <visp3/gui/vpDisplayX.h>
45#include <visp3/io/vpImageIo.h>
46
47template <typename Type> bool test(const std::string &display, vpImage<Type> &I, unsigned int scale, bool click)
48{
49 bool success = true;
50 unsigned int radius(I.getHeight() / 4);
51 int scale_ = (int)scale;
52 int radius_ = (int)radius;
53 unsigned int thickness = 2;
54 vpImagePoint center(I.getHeight() / 2, I.getWidth() / 2);
55 vpImagePoint offset(30, 160);
56 vpImagePoint v_offset(radius, 0);
57 vpImagePoint h_offset(0, radius);
58 vpRect roi(center, radius_ + scale_, radius_);
59 std::string itype;
60
61 // backup the input image
62 vpImage<Type> Ibackup(I);
63
64 vpDisplay *d = NULL;
65 if (display == "GDI") {
66#ifdef VISP_HAVE_GDI
67 d = new vpDisplayGDI;
68#endif
69 } else if (display == "GTK") {
70#ifdef VISP_HAVE_GTK
71 d = new vpDisplayGTK;
72#endif
73 } else if (display == "X") {
74#ifdef VISP_HAVE_X11
75 d = new vpDisplayX;
76#endif
77 } else if (display == "OpenCV") {
78#ifdef HAVE_OPENCV_HIGHGUI
79 d = new vpDisplayOpenCV;
80#endif
81 } else if (display == "D3D9") {
82#ifdef VISP_HAVE_D3D9
83 d = new vpDisplayD3D;
84#endif
85 }
86 std::cout << "Start test for " << display << " renderer..." << std::endl;
87 std::cout << " Screen resolution: " << d->getScreenWidth() << " " << d->getScreenHeight() << std::endl;
88 d->setDownScalingFactor(scale);
89 d->init(I);
92
93 vpImage<Type> crop;
94 vpImageTools::crop(I, vpImagePoint(0, 245), (unsigned int)roi.getHeight(), (unsigned int)roi.getWidth(), crop);
95 I.insert(crop, roi.getTopLeft());
98
99 // Compare input and rendered images
100 if (sizeof(Type) == 1) {
101 itype = "uchar";
102 vpImage<Type> Iinsert = I;
103 vpImage<vpRGBa> Isampled;
104 vpImage<vpRGBa> Icolor;
105 vpImageConvert::convert(Iinsert, Icolor);
106 Icolor.subsample(scale, scale, Isampled);
107
108 vpImage<vpRGBa> Irendered;
109 vpDisplay::getImage(I, Irendered);
110
111 if (Isampled != Irendered) {
112 success = false;
113 std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
114
115 std::stringstream ss;
116 ss << "Isampled-" << itype << "-scale-" << scale;
117#ifdef VISP_HAVE_OPENCV
118 ss << ".png";
119#else
120 ss << ".ppm";
121#endif
122
123 vpImageIo::write(Isampled, ss.str());
124
125 ss.str("");
126 ss.clear();
127 ss << "Irendered-" << itype << "-scale-" << scale;
128#ifdef VISP_HAVE_OPENCV
129 ss << ".png";
130#else
131 ss << ".ppm";
132#endif
133
134 vpImageIo::write(Irendered, ss.str());
135 vpImage<vpRGBa> Idiff;
136 vpImageTools::imageDifference(Isampled, Irendered, Idiff);
137
138 ss.str("");
139 ss.clear();
140 ss << "Idiff-" << itype << "-scale-" << scale;
141#ifdef VISP_HAVE_OPENCV
142 ss << ".png";
143#else
144 ss << ".ppm";
145#endif
146
147 vpImageIo::write(Idiff, ss.str());
148 } else {
149 std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
150 }
151 } else {
152 itype = "rgba";
153 vpImage<Type> Iinsert = I;
154 vpImage<Type> Isampled; // vpRGBa necessary
155 Iinsert.subsample(scale, scale, Isampled);
156
157 vpImage<vpRGBa> Irendered; // vpRGBa necessary
158 vpDisplay::getImage(I, Irendered);
159
160 vpImage<vpRGBa> IsampledCopy; // vpRGBa necessary
161
162 vpImageConvert::convert(Isampled, IsampledCopy);
163 if (IsampledCopy != Irendered) {
164 success = false;
165 std::cout << " -- Test width scale= " << scale << " type= " << itype << ": failed" << std::endl;
166
167 std::stringstream ss;
168 ss << "Isampled-" << itype << "-scale-" << scale;
169#ifdef VISP_HAVE_OPENCV
170 ss << ".png";
171#else
172 ss << ".ppm";
173#endif
174
175 vpImageIo::write(Isampled, ss.str());
176
177 ss.str("");
178 ss.clear();
179 ss << "Irendered-" << itype << "-scale-" << scale;
180#ifdef VISP_HAVE_OPENCV
181 ss << ".png";
182#else
183 ss << ".ppm";
184#endif
185
186 vpImageIo::write(Irendered, ss.str());
187 vpImage<vpRGBa> Idiff;
188 vpImageTools::imageDifference(IsampledCopy, Irendered, Idiff);
189 ss.str("");
190 ss.clear();
191 ss << "Idiff-" << itype << "-scale-" << scale;
192#ifdef VISP_HAVE_OPENCV
193 ss << ".png";
194#else
195 ss << ".ppm";
196#endif
197
198 vpImageIo::write(Idiff, ss.str());
199
200 } else {
201 std::cout << " ++ Test width scale= " << scale << " type= " << itype << ": succeed" << std::endl;
202 }
203 }
204
205 vpDisplay::displayRectangle(I, center - v_offset - h_offset, radius, radius, vpColor::blue, false, thickness);
206 vpDisplay::displayRectangle(I, center, center + v_offset + h_offset, vpColor::blue, false, thickness);
207 vpDisplay::displayRectangle(I, vpRect(center - v_offset - h_offset, center + v_offset + h_offset), vpColor::blue,
208 false, thickness);
209 vpDisplay::displayRectangle(I, center - v_offset * 3. / 2 + h_offset, radius / 2, radius / 2, vpColor::green, true);
210 vpDisplay::displayCircle(I, center, radius, vpColor::blue, false, thickness);
211 vpDisplay::displayArrow(I, center, center - v_offset / 4 - h_offset, vpColor::red, 10, 6, thickness);
212 vpDisplay::displayCross(I, center - radius / 2., radius, vpColor::green, thickness);
213 vpDisplay::displayDotLine(I, center - v_offset - h_offset, center, vpColor::cyan, thickness);
214 vpDisplay::displayLine(I, center + v_offset - h_offset, center - v_offset + h_offset, vpColor::cyan, thickness);
215 int nbpoints = (int)(radius * sqrt(2.) / 8 / scale);
216 for (int i = 0; i < nbpoints; i++) {
218 I, center - h_offset / 2. + vpImagePoint(-i * radius_ / (nbpoints * 2), i * radius_ / (nbpoints * 2)),
220 vpDisplay::displayPoint(I, center - h_offset + vpImagePoint(-i * radius_ / nbpoints, i * radius_ / nbpoints),
221 vpColor::cyan, thickness);
222 }
223
224 if (click)
225 vpDisplay::displayText(I, 10 * scale_, 10 * scale_, "A click to continue", vpColor::red);
226 else
227 vpDisplay::displayText(I, 10 * scale_, 10 * scale_, "This is an image", vpColor::red);
228
230
231 vpImage<vpRGBa> Irendered;
232 vpDisplay::getImage(I, Irendered);
233
234 std::stringstream ss;
235 ss << "overlay-" << display << "-" << itype << "-scale-" << scale;
236#ifdef VISP_HAVE_OPENCV
237 ss << ".png";
238#else
239 ss << ".ppm";
240#endif
241 std::cout << " Overlay saved in: " << ss.str() << std::endl;
242 vpImageIo::write(Irendered, ss.str());
243
244 if (click)
246
247 // Restore the input image
248 I = Ibackup;
249
251
252 if (d != NULL)
253 delete d;
254
255 if (success)
256 return true;
257 else
258 return false;
259}
260
261int main(int argc, const char *argv[])
262{
263 bool opt_click = true;
264 bool opt_display = true;
265 std::string opt_ipath;
266 std::string env_ipath;
267 std::string ipath;
268
269 for (int i = 0; i < argc; i++) {
270 if (std::string(argv[i]) == "-c")
271 opt_click = false;
272 else if (std::string(argv[i]) == "-d")
273 opt_display = false;
274 else if (std::string(argv[i]) == "-i")
275 opt_ipath = std::string(argv[i + 1]);
276 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
277 std::cout << "\nUsage: " << argv[0] << " [-i <image path>] [-c] [-d] [--help]\n" << std::endl;
278 std::cout << "\nOptions: " << std::endl;
279 std::cout << " -i <input image path> : set image input path.\n"
280 << " From this path read \"Klimt/Klimt.pgm\" image.\n"
281 << " Setting the VISP_INPUT_IMAGE_PATH environment\n"
282 << " variable produces the same behaviour than using\n"
283 << " this option." << std::endl;
284 std::cout << " -c : disable mouse click" << std::endl;
285 std::cout << " -d : disable display" << std::endl;
286 std::cout << " -h, --help : print this help\n" << std::endl;
287 return EXIT_SUCCESS;
288 }
289 }
290
291 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
292 // environment variable value
294
295 // Set the default input path
296 if (!env_ipath.empty())
297 ipath = env_ipath;
298
299 // Get the option values
300 if (!opt_ipath.empty())
301 ipath = opt_ipath;
302
303 std::string filename;
304
305 std::vector<std::string> display;
306 if (opt_display) {
307#ifdef VISP_HAVE_GDI
308 display.push_back("GDI");
309#endif
310#ifdef VISP_HAVE_GTK
311 display.push_back("GTK");
312#endif
313#ifdef VISP_HAVE_X11
314 display.push_back("X");
315#endif
316#ifdef VISP_HAVE_OPENCV
317 display.push_back("OpenCV");
318#endif
319#ifdef VISP_HAVE_D3D9
320 display.push_back("D3D9");
321#endif
322
323 if (display.size() == 0) {
324 std::cout << "No display available. We stop here." << std::endl;
325 return EXIT_FAILURE;
326 }
328 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
329 vpImageIo::read(I, filename);
330
332 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
333 vpImageIo::read(C, filename);
334
335 int nbfailure = 0;
336
337 for (unsigned int i = 0; i < display.size(); i++) {
338
339 for (unsigned int scale = 1; scale < 4; scale++) {
340 if (!test(display[i], I, scale, opt_click))
341 nbfailure++;
342 if (!test(display[i], C, scale, opt_click))
343 nbfailure++;
344 }
345 }
346 if (nbfailure == 0)
347 std::cout << "Test succeed" << std::endl;
348 else
349 std::cout << "Test failed with " << nbfailure << " failures" << std::endl;
350 }
351
352 return EXIT_SUCCESS;
353}
static const vpColor red
Definition vpColor.h:211
static const vpColor cyan
Definition vpColor.h:220
static const vpColor blue
Definition vpColor.h:217
static const vpColor green
Definition vpColor.h:214
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
unsigned int getScreenHeight()
unsigned int getScreenWidth()
Class that defines generic functionalities for display.
Definition vpDisplay.h:173
static void close(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void displayCircle(const vpImage< unsigned char > &I, const vpImageCircle &circle, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayROI(const vpImage< unsigned char > &I, const vpRect &roi)
virtual void setDownScalingFactor(unsigned int scale)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
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 displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
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 displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
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 of the vpImage class member functions.
Definition vpImage.h:135
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition vpImage.h:1466
unsigned int getWidth() const
Definition vpImage.h:242
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition vpImage.h:1361
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
Defines a rectangle in the plane.
Definition vpRect.h:76