Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
testImageDraw.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  * Test for vpImageDraw class.
33  *
34 *****************************************************************************/
41 #include <iostream>
42 #include <visp3/core/vpFont.h>
43 #include <visp3/core/vpImageDraw.h>
44 #include <visp3/io/vpImageIo.h>
45 
46 int main(int argc, char *argv[])
47 {
48  bool save = false;
49  for (int i = 1; i < argc; i++) {
50  if (std::string(argv[i]) == "--save") {
51  save = true;
52  }
53  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
54  std::cout << "\nUsage: " << argv[0] << " [--save] [--help] [-h]\n" << std::endl;
55  return EXIT_SUCCESS;
56  }
57  }
58  std::cout << "Save: " << save << std::endl;
59 
60  const std::string visp = "ViSP: Open source Visual Servoing Platform";
61 
62  // vpRGBa
63  {
64  vpImage<vpRGBa> I(480, 640, vpRGBa(255));
65  vpImagePoint iP1, iP2;
66 
67  iP1.set_i(20);
68  iP1.set_j(10);
69  iP2.set_i(20);
70  iP2.set_j(30);
71  vpImageDraw::drawArrow(I, iP1, iP2, vpColor::green, 4, 2, 3);
72 
73  iP1.set_i(20);
74  iP1.set_j(60);
75  vpFont font(32);
76  font.drawText(I, "Test...", iP1, vpColor::blue);
77 
78  iP1.set_i(60);
79  iP1.set_j(60);
80  font.drawText(I, "...tse", iP1, vpColor::red);
81 
82  iP1.set_i(60);
83  iP1.set_j(260);
84  font.drawText(I, "&é(-è_çà)", iP1, vpColor::red);
85 
86  iP1.set_i(200);
87  iP1.set_j(200);
88  font.drawText(I, "Test...", iP1, vpColor::white, vpColor::black);
89 
90  vpFont font2(20);
91  vpImagePoint textSize = font2.getMeasure(visp);
92  vpImagePoint textPos = vpImagePoint(24, 620 - textSize.get_j());
93  font2.drawText(I, visp, textPos, vpColor::darkGreen);
94  vpImageDraw::drawRectangle(I, vpRect(textPos.get_u(), textPos.get_v(), textSize.get_u(), textSize.get_v()),
96 
97  iP1.set_i(80);
98  iP1.set_j(220);
99  iP2.set_i(80);
100  iP2.set_j(480);
101  vpImageDraw::drawCircle(I, iP1, 30, vpColor::red, 3);
102 
103  iP1.set_i(20);
104  iP1.set_j(220);
105  vpImageDraw::drawCross(I, iP1, 5, vpColor::blue, 1);
106 
107  iP1.set_i(140);
108  iP1.set_j(10);
109  iP2.set_i(140);
110  iP2.set_j(50);
112 
113  iP1.set_i(120);
114  iP1.set_j(180);
115  iP2.set_i(160);
116  iP2.set_j(250);
118 
119  iP1.set_i(160);
120  iP1.set_j(280);
121  iP2.set_i(120);
122  iP2.set_j(340);
124 
125  iP1.set_i(220);
126  iP1.set_j(400);
127  iP2.set_i(120);
128  iP2.set_j(400);
130 
131  iP1.set_i(220);
132  iP1.set_j(480);
133  iP2.set_i(120);
134  iP2.set_j(450);
136 
137  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
138  vpCameraParameters cam(600, 600, 320, 240);
139  vpImageDraw::drawFrame(I, cMo, cam, 0.05, vpColor::none, 3);
140 
141  iP1.set_i(140);
142  iP1.set_j(80);
143  iP2.set_i(140);
144  iP2.set_j(150);
145  vpImageDraw::drawLine(I, iP1, iP2, vpColor::orange, 3);
146 
147  iP1.set_i(140);
148  iP1.set_j(400);
150 
151  iP1.set_i(350);
152  iP1.set_j(20);
153  int w = 60;
154  int h = 50;
155  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), vpColor::red, false, 3);
156 
157  iP1.set_i(350);
158  iP1.set_j(110);
159  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), vpColor::red, true, 3);
160 
161  iP1.set_i(350);
162  iP1.set_j(200);
163  iP2.set_i(400);
164  iP2.set_j(260);
165  vpImageDraw::drawRectangle(I, vpRect(iP1, iP2), vpColor::orange, false, 3);
166 
167  iP1.set_i(350);
168  iP1.set_j(290);
169  iP2.set_i(400);
170  iP2.set_j(350);
171  vpRect rectangle(iP1, iP2);
172  vpImageDraw::drawRectangle(I, rectangle, vpColor::yellow, false, 3);
173 
174  std::vector<vpImagePoint> polygon;
175  polygon.push_back(vpImagePoint(250, 500));
176  polygon.push_back(vpImagePoint(350, 600));
177  polygon.push_back(vpImagePoint(450, 500));
178  polygon.push_back(vpImagePoint(350, 400));
180 
181  polygon.clear();
182  polygon.push_back(vpImagePoint(300, 500));
183  polygon.push_back(vpImagePoint(350, 550));
184  polygon.push_back(vpImagePoint(400, 500));
185  polygon.push_back(vpImagePoint(350, 450));
186  vpImageDraw::drawPolygon(I, polygon, vpColor::cyan, 3, false);
187 
188  if (save) {
189  std::string filename = "canvas_color.png";
190  std::cout << "Save " << filename << std::endl;
191  vpImageIo::write(I, filename);
192  }
193  }
194 
195  // unsigned char
196  {
197  vpImage<unsigned char> I(480, 640, 0);
198  vpImagePoint iP1, iP2;
199  unsigned char color = 255;
200 
201  iP1.set_i(20);
202  iP1.set_j(10);
203  iP2.set_i(20);
204  iP2.set_j(30);
205  vpImageDraw::drawArrow(I, iP1, iP2, color, 4, 2, 3);
206 
207  iP1.set_i(20);
208  iP1.set_j(60);
209  vpFont font(32);
210  font.drawText(I, "Test...", iP1, color);
211 
212  iP1.set_i(60);
213  iP1.set_j(60);
214  font.drawText(I, "...tse", iP1, color);
215 
216  iP1.set_i(60);
217  iP1.set_j(260);
218  font.drawText(I, "&é(-è_çà)", iP1, color);
219 
220  iP1.set_i(200);
221  iP1.set_j(200);
222  font.drawText(I, "Test...", iP1, 0, 255);
223 
224  vpFont font2(20);
225  vpImagePoint textSize = font2.getMeasure(visp);
226  vpImagePoint textPos = vpImagePoint(24, 620 - textSize.get_j());
227  font2.drawText(I, visp, textPos, 255);
228  vpImageDraw::drawRectangle(I, vpRect(textPos.get_u(), textPos.get_v(), textSize.get_u(), textSize.get_v()), 255);
229 
230  iP1.set_i(80);
231  iP1.set_j(220);
232  iP2.set_i(80);
233  iP2.set_j(480);
234  vpImageDraw::drawCircle(I, iP1, 30, color, 3);
235 
236  iP1.set_i(20);
237  iP1.set_j(220);
238  vpImageDraw::drawCross(I, iP1, 5, color, 1);
239 
240  iP1.set_i(140);
241  iP1.set_j(10);
242  iP2.set_i(140);
243  iP2.set_j(50);
244  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
245 
246  iP1.set_i(120);
247  iP1.set_j(180);
248  iP2.set_i(160);
249  iP2.set_j(250);
250  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
251 
252  iP1.set_i(160);
253  iP1.set_j(280);
254  iP2.set_i(120);
255  iP2.set_j(340);
256  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
257 
258  iP1.set_i(220);
259  iP1.set_j(400);
260  iP2.set_i(120);
261  iP2.set_j(400);
262  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
263 
264  iP1.set_i(220);
265  iP1.set_j(480);
266  iP2.set_i(120);
267  iP2.set_j(450);
268  vpImageDraw::drawDottedLine(I, iP1, iP2, color, 3);
269 
270  vpHomogeneousMatrix cMo(vpTranslationVector(0.15, -0.07, 0.37), vpRotationMatrix(vpRxyzVector(0.1, -0.4, 0.41)));
271  vpCameraParameters cam(600, 600, 320, 240);
272  vpImageDraw::drawFrame(I, cMo, cam, 0.05, color, 3);
273 
274  iP1.set_i(140);
275  iP1.set_j(80);
276  iP2.set_i(140);
277  iP2.set_j(150);
278  vpImageDraw::drawLine(I, iP1, iP2, color, 3);
279 
280  iP1.set_i(140);
281  iP1.set_j(400);
282  vpImageDraw::drawPoint(I, iP1, color);
283 
284  iP1.set_i(350);
285  iP1.set_j(20);
286  int w = 60;
287  int h = 50;
288  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), color, false, 3);
289 
290  iP1.set_i(350);
291  iP1.set_j(110);
292  vpImageDraw::drawRectangle(I, vpRect(iP1, w, h), color, true, 3);
293 
294  iP1.set_i(350);
295  iP1.set_j(200);
296  iP2.set_i(400);
297  iP2.set_j(260);
298  vpImageDraw::drawRectangle(I, vpRect(iP1, iP2), color, false, 3);
299 
300  iP1.set_i(350);
301  iP1.set_j(290);
302  iP2.set_i(400);
303  iP2.set_j(350);
304  vpRect rectangle(iP1, iP2);
305  vpImageDraw::drawRectangle(I, rectangle, color, false, 3);
306 
307  std::vector<vpImagePoint> polygon;
308  polygon.push_back(vpImagePoint(250, 500));
309  polygon.push_back(vpImagePoint(350, 600));
310  polygon.push_back(vpImagePoint(450, 500));
311  polygon.push_back(vpImagePoint(350, 400));
312  vpImageDraw::drawPolygon(I, polygon, color, 3);
313 
314  polygon.clear();
315  polygon.push_back(vpImagePoint(300, 500));
316  polygon.push_back(vpImagePoint(350, 550));
317  polygon.push_back(vpImagePoint(400, 500));
318  polygon.push_back(vpImagePoint(350, 450));
319  vpImageDraw::drawPolygon(I, polygon, color, 3, false);
320 
321  if (save) {
322  std::string filename = "canvas_gray.png";
323  std::cout << "Save " << filename << std::endl;
324  vpImageIo::write(I, filename);
325  }
326  }
327 
328  return EXIT_SUCCESS;
329 }
Generic class defining intrinsic camera parameters.
static const vpColor white
Definition: vpColor.h:206
static const vpColor red
Definition: vpColor.h:211
static const vpColor black
Definition: vpColor.h:205
static const vpColor cyan
Definition: vpColor.h:220
static const vpColor none
Definition: vpColor.h:223
static const vpColor orange
Definition: vpColor.h:221
static const vpColor darkRed
Definition: vpColor.h:212
static const vpColor blue
Definition: vpColor.h:217
static const vpColor darkGreen
Definition: vpColor.h:215
static const vpColor yellow
Definition: vpColor.h:219
static const vpColor green
Definition: vpColor.h:214
Font drawing functions for image.
Definition: vpFont.h:54
Implementation of an homogeneous matrix and operations on such kind of matrices.
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 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 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:287
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:304
double get_j() const
Definition: vpImagePoint.h:125
void set_i(double ii)
Definition: vpImagePoint.h:293
double get_u() const
Definition: vpImagePoint.h:136
double get_v() const
Definition: vpImagePoint.h:147
Definition: vpRGBa.h:61
Defines a rectangle in the plane.
Definition: vpRect.h:76
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:176
Class that consider the case of a translation vector.
Definition: vpIoTools.h:60