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