YARP
Yet Another Robot Platform
ImageDraw.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SIG_IMAGEDRAW_H
8 #define YARP_SIG_IMAGEDRAW_H
9 
10 #include <cmath>
11 
12 #include <yarp/sig/Image.h>
13 
14 
15 namespace yarp {
16  namespace sig{
23  namespace draw {
24 
25  template <class T>
26  void addSegment(ImageOf<T>& dest, const T& pix,
27  int x, int y, int dx, int dy) {
28  const double vx = double(dx - x);
29  const double vy = double(dy - y);
30  // can't use fmax - not portable --paulfitz
31  double vbigger = fabs((fabs(vy)>fabs(vx))?vy:vx);
32  const int steps = int(2*vbigger);
33  const double r = 1.0 / steps;
34  for (int i = 0; i <= steps; i++) {
35  dest.safePixel(int(x+vx*i*r),int(y+vy*i*r)) = pix;
36  }
37  }
38 
39  template <class T>
40  void addCircle(ImageOf<T>& dest, const T& pix,
41  int i, int j, int r) {
42  float d, r2 = (float)(r*r);
43  for (int ii=i-r; ii<=i+r; ii++) {
44  for (int jj=j-r; jj<=j+r; jj++) {
45  d = float((ii-i)*(ii-i)+(jj-j)*(jj-j));
46  if (d<=r2) {
47  dest.safePixel(ii,jj) = pix;
48  }
49  }
50  }
51  }
52 
53  template <class T>
54  void addCrossHair(ImageOf<T>& dest, const T& pix,
55  int i, int j, int r) {
56  for (int ii=i-r; ii<=i+r; ii++) {
57  for (int jj=j-r; jj<=j+r; jj++) {
58  if (ii==i||jj==j) {
59  dest.safePixel(ii,jj) = pix;
60  }
61  }
62  }
63  }
64 
65  template <class T>
66  void addCircleOutline(ImageOf<T>& dest, const T& pix,
67  int i, int j, int r) {
68  float d, r2 = float(r*r), r2l = float((r-1.1)*(r-1.1));
69  for (int ii=i-r; ii<=i+r; ii++) {
70  for (int jj=j-r; jj<=j+r; jj++) {
71  d = float((ii-i)*(ii-i)+(jj-j)*(jj-j));
72  if (d<=r2 && d>=r2l) {
73  dest.safePixel(ii,jj) = pix;
74  }
75  }
76  }
77  }
78 
79  template <class T>
80  void addOvalOutline(ImageOf<T>& dest, const T& pix,
81  int i, int j, int h2, int w2) {
82  float x, y;
83  for (float th=0; th<2.0*3.14159; th+=0.01) {
84  x = j+w2*cos(th);
85  y = i+h2*sin(th);
86  dest.safePixel((int)y,(int)x) = pix;
87  }
88  }
89 
90 
91  template <class T>
92  void addRectangleOutline(ImageOf<T>& dest, const T& pix,
93  int i, int j, int w, int h) {
94  for (int ii=i-w; ii<=i+w; ii++) {
95  dest.safePixel(ii,j-h) = pix;
96  dest.safePixel(ii,j-h+1) = pix;
97  dest.safePixel(ii,j+h) = pix;
98  dest.safePixel(ii,j+h-1) = pix;
99  }
100  for (int jj=j-h; jj<=j+h; jj++) {
101  dest.safePixel(i-w,jj) = pix;
102  dest.safePixel(i-w+1,jj) = pix;
103  dest.safePixel(i+w,jj) = pix;
104  dest.safePixel(i+w-1,jj) = pix;
105  }
106  }
107 
111  template <class T>
112  void addRectangle(ImageOf<T>& dest, const T& pix,
113  int i, int j, int w, int h) {
114  for (int ii=i-w; ii<=i+w; ii++) {
115  for (int jj=j-h; jj<=j+h; jj++) {
116  dest.safePixel(ii,jj) = pix;
117  }
118  }
119  }
120 
121  template <class T>
123  const T& thetalo, const T& thetahi,
124  const T& pix0, const T& pix1) {
125  int h = src.height();
126  int w = src.width();
127  for (int i=0; i<h; i++) {
128  for (int j=0; j<w; j++) {
129  if (src(i,j)>=thetalo && src(i,j)<=thetahi) {
130  dest(i,j) = pix1;
131  } else {
132  dest(i,j) = pix0;
133  }
134  }
135  }
136  return 0;
137  }
138 
139  template <class T>
140  void setImagePixels(ImageOf<T>& src, const T& pix) {
141  int h = src.height();
142  int w = src.width();
143  for (int i=0; i<h; i++) {
144  for (int j=0; j<w; j++) {
145  src(i,j) = pix;
146  }
147  }
148  }
149 
150 #ifndef IMGFOR
151 #define IMGFOR(img,i,j) for (size_t i=0; i<(img).width(); i++) for (size_t j=0; j<(img).height(); j++)
152 #endif
153 
154  }
155  }
156 } // end namespace yarp::sig::draw
157 
158 #endif // YARP_SIG_IMAGEDRAW_H
Typed image class.
Definition: Image.h:658
T & safePixel(size_t x, size_t y)
Definition: Image.h:690
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
void addOvalOutline(ImageOf< T > &dest, const T &pix, int i, int j, int h2, int w2)
Definition: ImageDraw.h:80
void addRectangleOutline(ImageOf< T > &dest, const T &pix, int i, int j, int w, int h)
Definition: ImageDraw.h:92
void addCrossHair(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:54
int applyThreshold(ImageOf< T > &src, ImageOf< T > &dest, const T &thetalo, const T &thetahi, const T &pix0, const T &pix1)
Definition: ImageDraw.h:122
void addSegment(ImageOf< T > &dest, const T &pix, int x, int y, int dx, int dy)
Definition: ImageDraw.h:26
void addCircle(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:40
void addRectangle(ImageOf< T > &dest, const T &pix, int i, int j, int w, int h)
warning : i, j is x, y center of rectangle
Definition: ImageDraw.h:112
void setImagePixels(ImageOf< T > &src, const T &pix)
Definition: ImageDraw.h:140
void addCircleOutline(ImageOf< T > &dest, const T &pix, int i, int j, int r)
Definition: ImageDraw.h:66
The main, catch-all namespace for YARP.
Definition: dirs.h:16