Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testImageDraw.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test for vpImageDraw class.
32  */
39 #include <iostream>
40 #include <visp3/core/vpFont.h>
41 #include <visp3/core/vpImageDraw.h>
42 #include <visp3/io/vpImageIo.h>
43 
44 int main(int argc, char *argv[])
45 {
46 #ifdef ENABLE_VISP_NAMESPACE
47  using namespace VISP_NAMESPACE_NAME;
48 #endif
49  bool save = false;
50  for (int i = 1; i < argc; i++) {
51  if (std::string(argv[i]) == "--save") {
52  save = true;
53  }
54  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
55  std::cout << "\nUsage: " << argv[0] << " [--save] [--help] [-h]\n" << std::endl;
56  return EXIT_SUCCESS;
57  }
58  }
59  std::cout << "Save: " << save << std::endl;
60 
61  const std::string visp = "ViSP: Open source Visual Servoing Platform";
62 
63  // vpRGBa
64  {
65  vpImage<vpRGBa> I(480, 640, vpRGBa(255));
66  vpImagePoint iP1, iP2;
67 
68  iP1.set_i(20);
69  iP1.set_j(10);
70  iP2.set_i(20);
71  iP2.set_j(30);
72  vpImageDraw::drawArrow(I, iP1, iP2, vpColor::green, 4, 2, 3);
73 
74  iP1.set_i(20);
75  iP1.set_j(60);
76  vpFont font(32);
77  font.drawText(I, "Test...", iP1, vpColor::blue);
78 
79  iP1.set_i(60);
80  iP1.set_j(60);
81  font.drawText(I, "...tse", iP1, vpColor::red);
82 
83  iP1.set_i(60);
84  iP1.set_j(260);
85  font.drawText(I, "&é(-è_çà)", iP1, vpColor::red);
86 
87  iP1.set_i(200);
88  iP1.set_j(200);
89  font.drawText(I, "Test...", iP1, vpColor::white, vpColor::black);
90 
91  vpFont font2(20);
92  vpImagePoint textSize = font2.getMeasure(visp);
93  vpImagePoint textPos = vpImagePoint(24, 620 - textSize.get_j());
94  font2.drawText(I, visp, textPos, vpColor::darkGreen);
95  vpImageDraw::drawRectangle(I, vpRect(textPos.get_u(), textPos.get_v(), textSize.get_u(), textSize.get_v()),
97 
98  iP1.set_i(80);
99  iP1.set_j(220);
100  iP2.set_i(80);
101  iP2.set_j(480);
102  vpImageDraw::drawCircle(I, iP1, 30, vpColor::red, 3);
103 
104  iP1.set_i(20);
105  iP1.set_j(220);
106  vpImageDraw::drawCross(I, iP1, 5, vpColor::blue, 1);
107 
108  iP1.set_i(140);
109  iP1.set_j(10);
110  iP2.set_i(140);
111  iP2.set_j(50);
113 
114  iP1.set_i(120);
115  iP1.set_j(180);
116  iP2.set_i(160);
117  iP2.set_j(250);
119 
120  iP1.set_i(160);
121  iP1.set_j(280);
122  iP2.set_i(120);
123  iP2.set_j(340);
125 
126  iP1.set_i(220);
127  iP1.set_j(400);
128  iP2.set_i(120);
129  iP2.set_j(400);
131 
132  iP1.set_i(220);
133  iP1.set_j(480);
134  iP2.set_i(120);
135  iP2.set_j(450);
137 
138  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
139  vpCameraParameters cam(600, 600, 320, 240);
140  vpImageDraw::drawFrame(I, cMo, cam, 0.05, vpColor::none, 3);
141 
142  iP1.set_i(140);
143  iP1.set_j(80);
144  iP2.set_i(140);
145  iP2.set_j(150);
146  vpImageDraw::drawLine(I, iP1, iP2, vpColor::orange, 3);
147 
148  iP1.set_i(140);
149  iP1.set_j(400);
151 
152  iP1.set_i(350);
153  iP1.set_j(20);
154  int w = 60;
155  int h = 50;
156  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), vpColor::red, false, 3);
157 
158  iP1.set_i(350);
159  iP1.set_j(110);
160  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), vpColor::red, true, 3);
161 
162  iP1.set_i(350);
163  iP1.set_j(200);
164  iP2.set_i(400);
165  iP2.set_j(260);
166  vpImageDraw::drawRectangle(I, vpRect(iP1, iP2), vpColor::orange, false, 3);
167 
168  iP1.set_i(350);
169  iP1.set_j(290);
170  iP2.set_i(400);
171  iP2.set_j(350);
172  vpRect rectangle(iP1, iP2);
173  vpImageDraw::drawRectangle(I, rectangle, vpColor::yellow, false, 3);
174 
175  std::vector<vpImagePoint> polygon;
176  polygon.push_back(vpImagePoint(250, 500));
177  polygon.push_back(vpImagePoint(350, 600));
178  polygon.push_back(vpImagePoint(450, 500));
179  polygon.push_back(vpImagePoint(350, 400));
181 
182  polygon.clear();
183  polygon.push_back(vpImagePoint(300, 500));
184  polygon.push_back(vpImagePoint(350, 550));
185  polygon.push_back(vpImagePoint(400, 500));
186  polygon.push_back(vpImagePoint(350, 450));
187  vpImageDraw::drawPolygon(I, polygon, vpColor::cyan, 3, false);
188 
189  if (save) {
190  std::string filename = "canvas_color.png";
191  std::cout << "Save " << filename << std::endl;
192  vpImageIo::write(I, filename);
193  }
194  }
195 
196  // unsigned char
197  {
198  vpImage<unsigned char> I(480, 640, 0);
199  vpImagePoint iP1, iP2;
200  unsigned char color = 255;
201 
202  iP1.set_i(20);
203  iP1.set_j(10);
204  iP2.set_i(20);
205  iP2.set_j(30);
206  vpImageDraw::drawArrow(I, iP1, iP2, color, 4, 2, 3);
207 
208  iP1.set_i(20);
209  iP1.set_j(60);
210  vpFont font(32);
211  font.drawText(I, "Test...", iP1, color);
212 
213  iP1.set_i(60);
214  iP1.set_j(60);
215  font.drawText(I, "...tse", iP1, color);
216 
217  iP1.set_i(60);
218  iP1.set_j(260);
219  font.drawText(I, "&é(-è_çà)", iP1, color);
220 
221  iP1.set_i(200);
222  iP1.set_j(200);
223  font.drawText(I, "Test...", iP1, 0, 255);
224 
225  vpFont font2(20);
226  vpImagePoint textSize = font2.getMeasure(visp);
227  vpImagePoint textPos = vpImagePoint(24, 620 - textSize.get_j());
228  font2.drawText(I, visp, textPos, 255);
229  vpImageDraw::drawRectangle(I, vpRect(textPos.get_u(), textPos.get_v(), textSize.get_u(), textSize.get_v()), 255);
230 
231  iP1.set_i(80);
232  iP1.set_j(220);
233  iP2.set_i(80);
234  iP2.set_j(480);
235  vpImageDraw::drawCircle(I, iP1, 30, color, 3);
236 
237  iP1.set_i(20);
238  iP1.set_j(220);
239  vpImageDraw::drawCross(I, iP1, 5, color, 1);
240 
241  iP1.set_i(140);
242  iP1.set_j(10);
243  iP2.set_i(140);
244  iP2.set_j(50);
245  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
246 
247  iP1.set_i(120);
248  iP1.set_j(180);
249  iP2.set_i(160);
250  iP2.set_j(250);
251  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
252 
253  iP1.set_i(160);
254  iP1.set_j(280);
255  iP2.set_i(120);
256  iP2.set_j(340);
257  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
258 
259  iP1.set_i(220);
260  iP1.set_j(400);
261  iP2.set_i(120);
262  iP2.set_j(400);
263  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
264 
265  iP1.set_i(220);
266  iP1.set_j(480);
267  iP2.set_i(120);
268  iP2.set_j(450);
269  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
270 
271  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
272  vpCameraParameters cam(600, 600, 320, 240);
273  vpImageDraw::drawFrame(I, cMo, cam, 0.05, color, 3);
274 
275  iP1.set_i(140);
276  iP1.set_j(80);
277  iP2.set_i(140);
278  iP2.set_j(150);
279  vpImageDraw::drawLine(I, iP1, iP2, color, 3);
280 
281  iP1.set_i(140);
282  iP1.set_j(400);
283  vpImageDraw::drawPoint(I, iP1, color);
284 
285  iP1.set_i(350);
286  iP1.set_j(20);
287  int w = 60;
288  int h = 50;
289  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), color, false, 3);
290 
291  iP1.set_i(350);
292  iP1.set_j(110);
293  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), color, true, 3);
294 
295  iP1.set_i(350);
296  iP1.set_j(200);
297  iP2.set_i(400);
298  iP2.set_j(260);
299  vpImageDraw::drawRectangle(I, vpRect(iP1, iP2), color, false, 3);
300 
301  iP1.set_i(350);
302  iP1.set_j(290);
303  iP2.set_i(400);
304  iP2.set_j(350);
305  vpRect rectangle(iP1, iP2);
306  vpImageDraw::drawRectangle(I, rectangle, color, false, 3);
307 
308  std::vector<vpImagePoint> polygon;
309  polygon.push_back(vpImagePoint(250, 500));
310  polygon.push_back(vpImagePoint(350, 600));
311  polygon.push_back(vpImagePoint(450, 500));
312  polygon.push_back(vpImagePoint(350, 400));
313  vpImageDraw::drawPolygon(I, polygon, color, 3);
314 
315  polygon.clear();
316  polygon.push_back(vpImagePoint(300, 500));
317  polygon.push_back(vpImagePoint(350, 550));
318  polygon.push_back(vpImagePoint(400, 500));
319  polygon.push_back(vpImagePoint(350, 450));
320  vpImageDraw::drawPolygon(I, polygon, color, 3, false);
321 
322  if (save) {
323  std::string filename = "canvas_gray.png";
324  std::cout << "Save " << filename << std::endl;
325  vpImageIo::write(I, filename);
326  }
327  }
328 
329  return EXIT_SUCCESS;
330 }
Generic class defining intrinsic camera parameters.
static const vpColor white
Definition: vpColor.h:212
static const vpColor red
Definition: vpColor.h:217
static const vpColor cyan
Definition: vpColor.h:226
static const vpColor none
Definition: vpColor.h:229
static const vpColor orange
Definition: vpColor.h:227
static const vpColor darkRed
Definition: vpColor.h:218
static const vpColor blue
Definition: vpColor.h:223
static const vpColor darkGreen
Definition: vpColor.h:221
static const vpColor yellow
Definition: vpColor.h:225
static const vpColor black
Definition: vpColor.h:211
static const vpColor green
Definition: vpColor.h:220
Font drawing functions for image.
Definition: vpFont.h:55
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void drawArrow(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void drawRectangle(vpImage< unsigned char > &I, const vpRect &rectangle, unsigned char color, bool fill=false, unsigned int thickness=1)
static void drawCircle(vpImage< unsigned char > &I, const vpImageCircle &circle, unsigned char color, unsigned int thickness=1)
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
static void drawPoint(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned char color, unsigned int thickness=1)
static void drawFrame(vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, unsigned char color, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
static void drawPolygon(vpImage< unsigned char > &I, const std::vector< vpImagePoint > &vip, unsigned char color, unsigned int thickness=1, bool closed=true)
static void drawCross(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, unsigned char color, unsigned int thickness=1)
static void drawDottedLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_j(double jj)
Definition: vpImagePoint.h:309
double get_j() const
Definition: vpImagePoint.h:125
void set_i(double ii)
Definition: vpImagePoint.h:298
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
Definition: vpRGBa.h:65
Defines a rectangle in the plane.
Definition: vpRect.h:79
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
Class that consider the case of a translation vector.
Definition: vpIoTools.h:61