appkit  1.5.1
RoadNarrows Robotics Application Kit
WinOpenCv.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_win
8 //
9 // File: WinOpenCv.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-03 07:45:13 -0600 (Fri, 03 May 2013) $
14  * $Rev: 2904 $
15  *
16  * \brief RoadNarrows Robotics OpenCV Utilities.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2011-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_WIN_OPEN_CV_H
58 #define _RNR_WIN_OPEN_CV_H
59 
60 #include <stdio.h>
61 #include <math.h>
62 
63 #include "rnr/rnrconfig.h"
64 
65 #include "opencv2/core/core.hpp"
66 
67 /*!
68  * \brief RoadNarrows Robotics
69  */
70 namespace rnr
71 {
72  /*
73  * Image channel indices.
74  * Note: For some odd reason, OpenCV does not define these. And RGB is in
75  * reverse order than what would be expected.
76  */
77  #define CHANNEL_GRAY -1 ///< all channels summed to gray
78 
79  // RGB
80  #define CHANNEL_BLUE 0 ///< blue channel
81  #define CHANNEL_GREEN 1 ///< green channel
82  #define CHANNEL_RED 2 ///< red channel
83 
84  // HSV
85  #define CHANNEL_HUE 0 ///< hue (color) channel
86  #define CHANNEL_SATURATION 1 ///< saturation (intensity) channel
87  #define CHANNEL_VALUE 2 ///< value (brightness) channel
88 
89  const cv::Point nopoint(-1, -1); ///< integer 2D "No Point"
90  const cv::Point2f nopoint2D(-1.0, -1.0); ///< fpn 2D "No Point"
91  const cv::Point3f nopoint3D(-1.0, -1.0, -1.0); ///< fpn 3D "No Point"
92 
93  /*!
94  * \brief Check if point is not set ("no point").
95  *
96  * \param pt 2D integer point.
97  *
98  * \return true or false.
99  */
100  inline bool isnopoint(const cv::Point &pt)
101  {
102  return (pt.x < 0) || (pt.y < 0)? true: false;
103  }
104 
105  /*!
106  * \brief Check if point is not set ("no point").
107  *
108  * \param pt 2D floating-point number point.
109  *
110  * \return true or false.
111  */
112  inline bool isnopoint(const cv::Point2f &pt)
113  {
114  return (pt.x < 0.0) || (pt.y < 0.0)? true: false;
115  }
116 
117  /*!
118  * \brief Check if point is not set ("no point").
119  *
120  * \param pt 3D floating-point number point
121  *
122  * \return true or false.
123  */
124  inline bool isnopoint(const cv::Point3f &pt)
125  {
126  return (pt.x < 0.0) || (pt.y < 0.0) || (pt.z < 0.0)? true: false;
127  }
128 
129  /*!
130  * \brief Calculate the L1 (taxi cab) distance between two 2D points.
131  *
132  * \param pt1 2D integer point 1.
133  * \param pt2 2D integer point 2.
134  *
135  * \return ||pt2-pt1||<sub>1</sub>
136  */
137  inline int distL1(const cv::Point &p1, const cv::Point &p2)
138  {
139  return (int)(fabs((double)(p1.x-p2.x)) + fabs((double)(p1.y-p2.y)));
140  }
141 
142  /*!
143  * \brief Calculate the L1 (taxi cab) distance between two 2D points.
144  *
145  * \param pt1 2D floating-point number point 1.
146  * \param pt2 2D floating-point number point 2.
147  *
148  * \return ||pt2-pt1||<sub>1</sub>
149  */
150  inline int distL1(const cv::Point2f &p1, const cv::Point2f &p2)
151  {
152  return fabs(p1.x-p2.x) + fabs(p1.y-p2.y);
153  }
154 
155  /*!
156  * \brief Calculate the L1 (taxi cab) distance between two 3D points.
157  *
158  * \param pt1 3D floating-point number point 1.
159  * \param pt2 3D floating-point number point 2.
160  *
161  * \return ||pt2-pt1||<sub>1</sub>
162  */
163  inline int distL1(const cv::Point3f &p1, const cv::Point3f &p2)
164  {
165  return fabs(p1.x-p2.x) + fabs(p1.y-p2.y) + fabs(p1.z-p2.z);
166  }
167 
168  /*!
169  * \brief Calculate the L2 (euclidean) distance between two 2D points.
170  *
171  * \param pt1 2D integer point 1.
172  * \param pt2 2D integer point 2.
173  *
174  * \return Distance.
175  */
176  inline int distL2(const cv::Point &p1, const cv::Point &p2)
177  {
178  return (int)sqrt( (double)((p1.x-p2.x)*(p1.x-p2.x)) +
179  (double)((p1.y-p2.y)*(p1.y-p2.y)) );
180  }
181 
182  /*!
183  * \brief Calculate the L2 (euclidean) distance between two 2D points.
184  *
185  * \param pt1 2D floating-point number point 1.
186  * \param pt2 2D floating-point number point 2.
187  *
188  * \return Distance.
189  */
190  inline double distL2(const cv::Point2f &p1, const cv::Point2f &p2)
191  {
192  return sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
193  }
194 
195  /*!
196  * \brief Calculate the L2 (euclidean) distance between two 3D points.
197  *
198  * \param pt1 3D floating-point number point 1.
199  * \param pt2 3D floating-point number point 2.
200  *
201  * \return Distance.
202  */
203  inline double distL2(const cv::Point3f &p1, const cv::Point3f &p2)
204  {
205  return sqrt( (p1.x-p2.x)*(p1.x-p2.x) +
206  (p1.y-p2.y)*(p1.y-p2.y) +
207  (p1.z-p2.z)*(p1.z-p2.z) );
208  }
209 
210  /*!
211  * \brief Calculate the Linf distance between two 2D points.
212  *
213  * \param pt1 2D integer point 1.
214  * \param pt2 2D integer point 2.
215  *
216  * \return Distance.
217  */
218  inline int distLinf(const cv::Point &p1, const cv::Point &p2)
219  {
220  return (int)fmax(fabs((double)(p1.x-p2.x)), fabs((double)(p1.y-p2.y)));
221  }
222 
223  /*!
224  * \brief Calculate the Linf distance between two 2D points.
225  *
226  * \param pt1 2D floating-point number point 1.
227  * \param pt2 2D floating-point number point 2.
228  *
229  * \return Distance.
230  */
231  inline int distLinf(const cv::Point2f &p1, const cv::Point2f &p2)
232  {
233  return fmax(fabs(p1.x-p2.x), fabs(p1.y-p2.y));
234  }
235 
236  /*!
237  * \brief Calculate the Linf distance between two 3D points.
238  *
239  * \param pt1 3D floating-point number point 1.
240  * \param pt2 3D floating-point number point 2.
241  *
242  * \return Distance.
243  */
244  inline int distLinf(const cv::Point3f &p1, const cv::Point3f &p2)
245  {
246  return fmax(fmax(fabs(p1.x-p2.x), fabs(p1.y-p2.y)), fabs(p1.z-p2.z));
247  }
248 
249  /*!
250  * \brief Check if point x,y is with rectangle r.
251  *
252  * \param x X integer coordinate.
253  * \param y Y integer coordinate.
254  * \param r Integer ectangle.
255  *
256  * \return true or flase
257  */
258  inline bool isinrect(int x, int y, cv::Rect &r)
259  {
260  return (x >= r.x) && (x < r.x+r.width) && (y >= r.y) && (y < r.y+r.height)?
261  true: false;
262  }
263 
264  /*!
265  * \brief Calculate the nearest 4:3 aspect ratio from the width component of
266  * the target size.
267  *
268  * \param siz Target object dimensions.
269  *
270  * \return The 4:3 dimensions \h_le target siz.
271  */
272  inline cv::Size ar43width(cv::Size &siz)
273  {
274  cv::Size siz43;
275  int w = siz.width;
276  int r = w % 4;
277  siz43.height = (3 * (w-r)) / 4;
278  siz43.width = (4 * siz43.height) / 3;
279  return siz43;
280  }
281 
282  /*!
283  * \brief Calculate the nearest 4:3 aspect ratio from the target width.
284  *
285  * \param width Target width
286  *
287  * \return The 4:3 dimension with width \h_le target width.
288  */
289  inline cv::Size ar43width(int width)
290  {
291  cv::Size siz43;
292  int w = width;
293  int r = w % 4;
294  siz43.height = (3 * (w-r)) / 4;
295  siz43.width = (4 * siz43.height) / 3;
296  return siz43;
297  }
298 
299  /*!
300  * \brief Calculate the nearest 4:3 aspect ratio from the height component of
301  * the target size.
302  *
303  * \param siz Target object dimensions.
304  *
305  * \return 4:3 dimensions \h_le siz
306  */
307  inline cv::Size ar43height(cv::Size &siz)
308  {
309  cv::Size siz43;
310  int h = siz.height;
311  int r = h % 3;
312  siz43.width = (4 * (h-r)) / 3;
313  siz43.height = (3 * siz43.width) / 4;
314  return siz43;
315  }
316 
317  /*!
318  * \brief Calculate the nearest 4:3 aspect ratio from the target height.
319  *
320  * \param height Target height.
321  *
322  * \return 4:3 dimensions with height \h_le target height.
323  */
324  inline cv::Size ar43height(int height)
325  {
326  cv::Size siz43;
327  int h = height;
328  int r = h % 3;
329  siz43.width = (4 * (h-r)) / 3;
330  siz43.height = (3 * siz43.width) / 4;
331  return siz43;
332  }
333 
334  /*!
335  * \brief Debug print rectangle.
336  *
337  * \param s Preface string.
338  * \param r Rectangle.
339  */
340  inline void dbgrect(const std::string &str, const cv::Rect &r)
341  {
342  fprintf(stderr, "%s=(%d,%d,%d,%d)\n",
343  str.c_str(), r.x, r.y, r.width, r.height);
344  }
345 
346  /*!
347  * \brief Debug print point.
348  *
349  * \param s Preface string.
350  * \param pt Point.
351  */
352  inline void dbgpoint(const std::string &str, const cv::Point &pt)
353  {
354  fprintf(stderr, "%s=(%d,%d)\n", str.c_str(), pt.x, pt.y);
355  }
356 
357  /*!
358  * \brief Debug print size.
359  *
360  * \param s Preface string.
361  * \param siz Size.
362  */
363  inline void dbgsize(const std::string &str, const cv::Size &siz)
364  {
365  fprintf(stderr, "%s=(%d,%d)\n", str.c_str(), siz.width, siz.height);
366  }
367 } // namespace
368 
369 
370 #endif // _RNR_WIN_OPEN_CV_H
cv::Size ar43height(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the height component of the target size.
Definition: WinOpenCv.h:307
int distLinf(const cv::Point &p1, const cv::Point &p2)
Calculate the Linf distance between two 2D points.
Definition: WinOpenCv.h:218
void dbgrect(const std::string &str, const cv::Rect &r)
Debug print rectangle.
Definition: WinOpenCv.h:340
const cv::Point3f nopoint3D(-1.0,-1.0,-1.0)
fpn 3D "No Point"
int distL2(const cv::Point &p1, const cv::Point &p2)
Calculate the L2 (euclidean) distance between two 2D points.
Definition: WinOpenCv.h:176
void dbgsize(const std::string &str, const cv::Size &siz)
Debug print size.
Definition: WinOpenCv.h:363
const cv::Point nopoint(-1,-1)
integer 2D "No Point"
cv::Size ar43width(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the width component of the target size.
Definition: WinOpenCv.h:272
void dbgpoint(const std::string &str, const cv::Point &pt)
Debug print point.
Definition: WinOpenCv.h:352
const cv::Point2f nopoint2D(-1.0,-1.0)
fpn 2D "No Point"
int distL1(const cv::Point &p1, const cv::Point &p2)
Calculate the L1 (taxi cab) distance between two 2D points.
Definition: WinOpenCv.h:137
RoadNarrows Robotics.
Definition: Camera.h:74
bool isnopoint(const cv::Point &pt)
Check if point is not set ("no point").
Definition: WinOpenCv.h:100
bool isinrect(int x, int y, cv::Rect &r)
Check if point x,y is with rectangle r.
Definition: WinOpenCv.h:258