JeVois  1.10
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Coordinates.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 #include <jevois/Image/RawImage.H>
21 
22 namespace jevois
23 {
24  namespace coords
25  {
26  /*! \defgroup coordhelpers Helper functions to convert coordinates from camera resolution to standardized
27 
28  Different machine vision algorithms in JeVois may be able to operate with different camera resolutions, such as
29  1280x1024, 320x240, or 176x144. When some item of interest is detected in the camera frame, one may often want
30  to send the coordinates of that thing to the serial port. This poses a problem if one were to directly send the
31  image coordinates of the item out, which is that now the receiver (e.g., an Arduino) needs to know which camera
32  image resolution was used, so that it can properly interpret these coordinates. For example, if the visual
33  attention (saliency) algorithm is running with 640x480 camera input, then a salient object at the center of the
34  camera's field of view would have coordinates 320,240. But if the same saliency algorithm is configured to
35  process 320x240 input video (so that it can run at a higher framerate), now an object at the center of the field
36  of view would have coordinates 160,120. If one connects an Arduino that controls, for example, a pan/tilt head
37  to JeVois, we need a way to communicate coordinates of target objects in the world independently of the video
38  resolution used by the camera.
39 
40  Thus, JeVois defines a standardized coordinate system, as follows:
41 
42  - center fo the camera's field of view is at x=0, y=0
43  - left edge of the camera image is always at x=-1000
44  - right edge of the camera image is always at x=1000
45  - top edge of the camera image is usually at y=-750
46  - bottom edge of the camera image is usually at y=750
47 
48  Note that the value of 750 here comes from the assumption of a 4:3 aspect ratio for the camera sensor, and is
49  actually defined in JEVOIS_CAMERA_ASPECT. All camera sensor resolutions use 4:3 aspect ratio and hence have y
50  values between -750 and 750, except for 1280x1024, which has y values between -800 and 800.
51 
52  When writing a machine vision algorithm that sends over serial the coordinates of things detected in the camera
53  frames, be sure to first transform those coordinates from image to standardized space.
54 
55  For more on how standardized coordinates are used to communicate with embedded controllers, and for 3D
56  coordinates, see \ref UserSerialStyle
57 
58  \ingroup utils */
59 
60  //! Aspect ratio of the JeVois camera
61  /*! \ingroup coordhelpers */
62 #define JEVOIS_CAMERA_ASPECT (4.0 / 3.0)
63 
64  //! Transform coordinates in-place from camera to standardized, using a RawImage to establish image size
65  /*! The RawImage from the camera is used to specify pixel width and height of the camera image, and this is the
66  source coordinate system. The destination coordinate system is the standardized one, with x in [-1000 ... 1000]
67  and y in [-750 ... 750].
68 
69  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
70  values over serial port.
71 
72  \ingroup coordhelpers */
73  void imgToStd(float & x, float & y, RawImage const & camimg, float const eps = 0.1F);
74 
75  //! Transform coordinates in-place from camera to standardized, using given image width and height
76  /*! The width and height are used to specify pixel width and height of the camera image, and this is the source
77  coordinate system. The destination coordinate system is the standardized one, with x in [-1000 ... 1000] and y
78  in [-750 ... 750].
79 
80  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
81  values over serial port.
82 
83  \ingroup coordhelpers */
84  void imgToStd(float & x, float & y, unsigned int const width, unsigned int const height, float const eps = 0.1F);
85 
86  //! Transform X coordinate in-place from camera to standardized, using given image width and height
87  /*! The width is used to specify pixel width of the camera image, and this is the source
88  coordinate system. The destination coordinate system is the standardized one, with x in [-1000 ... 1000].
89 
90  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
91  values over serial port.
92 
93  \ingroup coordhelpers */
94  void imgToStdX(float & x, unsigned int const width, float const eps = 0.1F);
95 
96  //! Transform Y coordinate in-place from camera to standardized, using given image width and height
97  /*! The height is used to specify pixel height of the camera image, and this is the source
98  coordinate system. The destination coordinate system is the standardized one, with y in [-750 ... 750].
99 
100  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
101  values over serial port.
102 
103  \ingroup coordhelpers */
104  void imgToStdY(float & y, unsigned int const height, float const eps = 0.1F);
105 
106  //! Transform size in-place from camera to standardized, using given image width and height
107  /*! Arguments w and h define the size of an object in pixels, which will be converted to standardized units. The
108  width and height are used to specify pixel width and height of the camera image, and this is the source
109  coordinate system. The destination coordinate system is the standardized one, with x in [-1000 ... 1000] and y
110  in [-750 ... 750].
111 
112  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
113  values over serial port.
114 
115  \ingroup coordhelpers */
116  void imgToStdSize(float & w, float & h, unsigned int const width, unsigned int const height,
117  float const eps = 0.1F);
118 
119  //! Transform coordinates in-place from standardized to image, using a RawImage to establish image size
120  /*! The RawImage would typically be from the camera is used to specify pixel width and height of the camera image,
121  and this is the destination coordinate system. The source coordinate system is the standardized one, with x in
122  [-1000 ... 1000] and y in [-750 ... 750].
123 
124  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
125  values over serial port.
126 
127  \ingroup coordhelpers */
128  void stdToImg(float & x, float & y, RawImage const & camimg, float const eps = 0.1F);
129 
130  //! Transform coordinates in-place from standardized to image, using a RawImage to establish image size
131  /*! The width and height would typically be from the camera and are used to specify pixel width and height of the
132  camera image, and this is the destination coordinate system. The source coordinate system is the standardized
133  one, with x in [-1000 ... 1000] and y in [-750 ... 750].
134 
135  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
136  values over serial port.
137 
138  \ingroup coordhelpers */
139  void stdToImg(float & x, float & y, unsigned int const width, unsigned int const height, float const eps = 0.1F);
140 
141  //! Transform size in-place from standardized to image, using a RawImage to establish image size
142  /*! Arguments w and h define the size of an object in standardized units, which will be converted to pixels. The
143  width and height would typically be from the camera and are used to specify pixel width and height of the
144  camera image, and this is the destination coordinate system. The source coordinate system is the standardized
145  one, with x in [-1000 ... 1000] and y in [-750 ... 750].
146 
147  eps is used for rounding of returned coordinates, which is convenient to avoid sending very long floating point
148  values over serial port.
149 
150  \ingroup coordhelpers */
151  void stdToImgSize(float & x, float & y, unsigned int const width, unsigned int const height,
152  float const eps = 0.1F);
153  }
154 }
155 
void imgToStdY(float &y, unsigned int const height, float const eps=0.1F)
Transform Y coordinate in-place from camera to standardized, using given image width and height...
Definition: Coordinates.C:43
void imgToStd(float &x, float &y, RawImage const &camimg, float const eps=0.1F)
Transform coordinates in-place from camera to standardized, using a RawImage to establish image size...
Definition: Coordinates.C:22
void stdToImgSize(float &x, float &y, unsigned int const width, unsigned int const height, float const eps=0.1F)
Transform size in-place from standardized to image, using a RawImage to establish image size...
Definition: Coordinates.C:74
void stdToImg(float &x, float &y, RawImage const &camimg, float const eps=0.1F)
Transform coordinates in-place from standardized to image, using a RawImage to establish image size...
Definition: Coordinates.C:60
void imgToStdSize(float &w, float &h, unsigned int const width, unsigned int const height, float const eps=0.1F)
Transform size in-place from camera to standardized, using given image width and height.
Definition: Coordinates.C:50
void imgToStdX(float &x, unsigned int const width, float const eps=0.1F)
Transform X coordinate in-place from camera to standardized, using given image width and height...
Definition: Coordinates.C:36