Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageDraw.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8  BIAS is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 2.1 of the License, or
11  (at your option) any later version.
12 
13  BIAS is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with BIAS; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21 #ifndef _BIAS_IMAGE_DRAW_H_
22 #define _BIAS_IMAGE_DRAW_H_
23 
24 #include <bias_config.h>
25 
26 #ifdef BIAS_HAVE_OPENCV
27 // for font names
28 # include <cxcore.h>
29 #endif
30 
31 #include <Base/Image/ColourRGB.hh>
32 #include <Base/Image/Image.hh>
33 #include <Base/Geometry/HomgPoint2D.hh>
34 
35 namespace BIAS {
36 
37  /** @class ImgPosition
38  * @ingroup g_image_other
39  * @brief interface class used to ease handover in function calls
40  * @author Felix Woelk
41  * @date 09/2007
42  */
43  class BIASImageUtilsBase_EXPORT ImgPosition
44  {
45  public:
47  ImgPosition(const int x, const int y) { Pos_[0]=x; Pos_[1]=y; }
48  ImgPosition(const ImgPosition& pos) { Pos_[0]=pos[0]; Pos_[1]=pos[1]; }
50 
51  int& operator[](const int i)
52  { BIASASSERT(i>=0&&i<2); return Pos_[i]; }
53 
54  const int& operator[](const int i) const
55  { BIASASSERT(i>=0&&i<2); return Pos_[i]; }
56 
58  { Pos_[0]=pos[0]; Pos_[1]=pos[1]; return *this; }
59 
60  protected:
61  int Pos_[2];
62  };
63 
64 
65  /** @class ImageDraw
66  @ingroup g_image_other
67  @brief drawing simple entities into the image like rectangles or lines
68  As all functions are static they have to be called by
69  eg. ImageDraw::RectangleCenter() and not with an object.
70  @author fw */
71  template <class StorageType=unsigned char>
72  class BIASImageUtilsBase_EXPORT ImageDraw
73  {
74  public:
75  /* @brief returns value with good contrast to t */
76  static StorageType GetContrastValue(StorageType t);
77 
78  ////////////////////////////////////////////////////////////
79  /// rectangles
80  ////////////////////////////////////////////////////////////
81 
82  ////////////////////////////////////
83  // specifying rectangle corners
84 
85  /** @brief Draws the rectangle defined by upper left and bottom right
86  corner with value[i] in channel i **/
87  static int RectangleCorners(Image<StorageType>& im,
88  const int minx, const int miny,
89  const int maxx, const int maxy,
90  const StorageType value[]);
91 
92  /** @brief Draws the filled rectangle defined by upper left and bottom right
93  corner with value[i] in channel i **/
94  static int RectangleCornersFill(Image<StorageType>& im,
95  const int minx, const int miny,
96  const int maxx, const int maxy,
97  const StorageType value[]);
98 
99  /** @brief as above but fills all channels with value */
100  static int RectangleCorners(Image<StorageType>& im,
101  const int minx, const int miny,
102  const int maxx, const int maxy,
103  const StorageType value);
104 
105  /** @brief decides which color to use */
106  static int RectangleCorners(Image<StorageType>& im,
107  const int minx, const int miny,
108  const int maxx, const int maxy);
109 
110  /** @brief faster for one channel images */
111  static int RectangleCornersGrey(Image<StorageType>& im,
112  const int minx, const int miny,
113  const int maxx, const int maxy,
114  const StorageType GreyValue);
115 
116  /** @brief faster for one channel images */
117  static int RectangleCornersGreyFill(Image<StorageType>& im,
118  const int minx, const int miny,
119  const int maxx, const int maxy,
120  const StorageType GreyValue);
121 
122  //////////////////////////
123  // specifying the rectangle center and size
124 
125  /** @brief Draws the rectangle around X, Y with Size and Value[i]
126  in channel i
127  @author Felix Woelk **/
128  static int RectangleCenter(Image<StorageType>& im, const int x,
129  const int y, const int size,
130  const StorageType value[]);
131 
132  /** @brief fills the channels with value */
133  static int RectangleCenterGrey(Image<StorageType>& im, const int x,
134  const int y, const int size,
135  const StorageType value);
136 
137  static int RectangleCenterGreyFill(Image<StorageType>& im, const int x,
138  const int y, const int size,
139  const StorageType value);
140 
141  /** convenience wrapper */
142  inline
143  static int RectangleCenter(Image<StorageType>& im, const unsigned coo[2],
144  const int size, const StorageType value[])
145  { return RectangleCenter(im, coo[0], coo[1], size, value); };
146 
147  /** @brief draws a rectangle of size around x and y,
148  decides which color to use */
149  static int RectangleCenter(Image<StorageType>& im, const int x,
150  const int y, const int size);
151 
152  /** convenience wrapper */
153  static int RectangleCenter(Image<StorageType>& im, const int coo[2],
154  const int size);
155 
156 
157  //////////////////////////////////////////////////////////////
158  /// lines
159  /////////////////////////////////////////////////////////////
160 
161 
162  /** @brief Draws a line between the two points using the bresenham
163  algorithm uses value[i] in channel i **/
164  static int Line(Image<StorageType>& im, const unsigned int start[2],
165  const unsigned int end[2],
166  const StorageType value[]);
167  static int Line(Image<StorageType>& im,
168  const int start[2],
169  const int end[2],
170  const StorageType value[],
171  int width);
172 
173  /// differnet interface for the function above
174  static int Line(Image<StorageType>& im, const int start[2],
175  const int end[2],
176  const StorageType value[]);
177 
178  /** @brief uses value in every channel */
179  static int Line(Image<StorageType>& im, const unsigned int start[2],
180  const unsigned int end[2],
181  const StorageType value);
182 
183  /** @brief faster algorithm for 1 channel images */
184  static int LineGrey(Image<StorageType>& im, const unsigned int start[2],
185  const unsigned int end[2],
186  const StorageType value);
187 
188  /** @brief decides which color to use */
189  static int Line(Image<StorageType>& im, const unsigned int start[2],
190  const unsigned int end[2]);
191 
192  /** @brief faster algorithm for 1 channel images,
193  decides which color to use */
194  static int LineGrey(Image<StorageType>& im, const unsigned int start[2],
195  const unsigned int end[2]);
196 
197  //////////////////////////
198  // just wrappers
199 
200 
201  /** Draw a solid line with a specific thickness.
202  Internally implemented as drawing multiple Bresenham lines, for now.
203  \bug This routine is a HACK and incorrect. A generela "solid fill by 4 given corners" algorithm has to be used or a Bresenham with eps=thckness/2.
204  @param Value the line color.
205  @param thickness of the line in pixel (perpendicular to the direction).
206  @author Jan Woetzel 01/2005
207  DEPRECATED by JW 2005/08/09, replaced by OpenCV routine call.
208  */
209  static int Line(Image<StorageType>& im,
210  const unsigned int StartX,
211  const unsigned int StartY,
212  const unsigned int EndX,
213  const unsigned int EndY,
214  const StorageType color[],
215  const float thickness );
216 
217 
218  /** convenience wrapper */
219  static int Line(Image<StorageType>& im, const unsigned int StartX,
220  const unsigned int StartY, const unsigned int EndX,
221  const unsigned int EndY,
222  const StorageType Value[]);
223 
224  /** convenience wrapper */
225  static int Line(Image<StorageType>& im, const unsigned int StartX,
226  const unsigned int StartY, const unsigned int EndX,
227  const unsigned int EndY,
228  const StorageType Value);
229 
230  /** convenience wrapper */
231  static int Line(Image<StorageType>& im, const unsigned int StartX,
232  const unsigned int StartY, const unsigned int EndX,
233  const unsigned int EndY);
234 
235  /** convenience wrapper */
236  static int LineGrey(Image<StorageType>& im, const unsigned int StartX,
237  const unsigned int StartY, const unsigned int EndX,
238  const unsigned int EndY,
239  const StorageType Value);
240 
241  /** convenience wrapper */
242  inline static int Line(Image<StorageType>& im, unsigned int coo[4],
243  const StorageType value[])
244  { return Line(im, &coo[0], &coo[2], value); };
245 
246  /** @brief Convenience wrapper, draws a line even if start or end point are
247  not positioned in the image.
248  untested so far
249  @author woelk 06/2005 */
250  static int Line(Image<StorageType>& im, const double start[2],
251  const double end[2],
252  const StorageType value[]);
253 
254  ///////////////////////////
255  // interpolated lines
256 
257 
258  /** @brief draws an anti-aliased line that automatically clips to the image borders.
259  * @param im Image to draw at
260  * @param StartX Start point x coordinate
261  * @param StartY Start point y coordinate
262  * @param EndX End point x coordinate
263  * @param EndY End point y coordinate
264  * @param Value Color values for each channel
265  * @param Thickness Thickness of the line
266  * @param Opacity Opacity of the line
267  * @author jordt 11/2009 */
268  static int InterpolatedLine(Image<StorageType>& im,
269  const int StartX, const int StartY,
270  const int EndX, const int EndY,
271  const StorageType Value[],
272  const float thickness = 1.0,
273  const float Opacity = 1.0);
274 
275 
276  /** @brief draws an anti-aliased line that automatically clips to the image borders.
277  * @param im Image to draw at
278  * @param Start Start point
279  * @param End End point
280  * @param Value Color values for each channel
281  * @param Thickness Thickness of the line
282  * @param Opacity Opacity of the line
283  * @author jordt 11/2009 */
284  static int InterpolatedLine(Image<StorageType>& im,
285  const HomgPoint2D Start,
286  const HomgPoint2D End,
287  const StorageType Value[],
288  const float Thickness = 1.0,
289  const float Opacity = 1.0);
290 
291  /** @brief draws an anti-aliased line that automatically clips to the image borders.
292  * @param im Image to draw at
293  * @param StartX Start point x coordinate
294  * @param StartY Start point y coordinate
295  * @param EndX End point x coordinate
296  * @param EndY End point y coordinate
297  * @param Value Grey level
298  * @param Thickness Thickness of the line
299  * @param Opacity Opacity of the line
300  * @author jordt 11/2009 */
301  static int InterpolatedLineGrey(Image<StorageType>& im,
302  const int StartX, const int StartY,
303  const int EndX, const int EndY,
304  const StorageType Value,
305  const float thickness = 1.0,
306  const float Opacity = 1.0);
307 
308  /** @brief draws an anti-aliased line that automatically clips to the image borders.
309  * @param im Image to draw at
310  * @param Start Start point
311  * @param End End point
312  * @param Value Grey level
313  * @param Thickness Thickness of the line
314  * @param Opacity Opacity of the line
315  * @author jordt 11/2009 */
316  static int InterpolatedLineGrey(Image<StorageType>& im,
317  const HomgPoint2D Start,
318  const HomgPoint2D End,
319  const StorageType Value,
320  const float thickness = 1.0,
321  const float Opacity = 1.0);
322 
323 
325  unsigned int coo[4],
326  const StorageType Value)
327  { return InterpolatedLineGrey(im, coo[0], coo[1], coo[2],
328  coo[3], Value); };
329 
330 
331  ////////////////////////////////////
332  // arrows
333 
334  /** @brief draws an arrow from start to end, the tips of the head are
335  length pixel back on the line and width pixel away from the line
336  @author woelk 12/2003 */
337  static int Arrow(Image<StorageType>& im, const unsigned start[2],
338  const unsigned end[2], const unsigned length,
339  const unsigned width,
340  const StorageType value[]);
341 
342 
343  /////////////////////////////////////////
344  // circles
345  /** @brief draws a circular line, either using Value or
346  a good contrast value */
347  static int CircleCenter(Image<StorageType>& im, unsigned int CenterX,
348  unsigned int CenterY, unsigned int Radius,
349  const StorageType Value[]=NULL);
350 
351  /** @brief draws a filled circle using Value */
352  static int CircleCenterFilled(Image<StorageType>& im, unsigned int CenterX,
353  unsigned int CenterY, unsigned int Radius,
354  const StorageType Value[]);
355 
356  /** @brief draws an anti-aliased circular line that clips automatically to the image borders.
357  * @param CenterX Center point x coordinate
358  * @param CenterY Center point y coordinate
359  * @param Radius Circle radius
360  * @param Value Color values for each channel
361  * @param Thickness Thickness of the line
362  * @param Opacity Opacity of the line
363  * @author jordt 11/2009 */
364  static int InterpolatedCircleCenter(Image<StorageType>& im, unsigned int CenterX,
365  unsigned int CenterY, unsigned int Radius,
366  const StorageType Value[],
367  const float Thickness = 1.0,
368  const float Opacity = 1.0);
369  /** Convenience wrapper for InterpolatedCircleCenter().
370  */
371  static int InterpolatedCircleCenter(Image<StorageType>& im, unsigned int CenterX,
372  unsigned int CenterY, unsigned int Radius,
373  const std::vector<StorageType>& Value,
374  const float Thickness = 1.0,
375  const float Opacity = 1.0){
376  StorageType* color = new StorageType[Value.size()];
377  for(unsigned int i=0; i<Value.size(); i++){
378  color[i] = Value[i];
379  }
380  return InterpolatedCircleCenter(im, CenterX, CenterY, Radius, color, Thickness, Opacity);
381  delete [] color;
382  }
383 
384  /** @brief draws an anti-aliased circular line that clips automatically to the image borders.
385  * @param Center Center point
386  * @param Radius Circle radius
387  * @param Value Color values for each channel
388  * @param Thickness Thickness of the line
389  * @param Opacity Opacity of the line
390  * @author jordt 11/2009 */
391  static int InterpolatedCircleCenter(Image<StorageType>& im,
392  HomgPoint2D Center,
393  float Radius,
394  const StorageType Value[],
395  const float Thickness = 1.0,
396  const float Opacity = 1.0);
397 
398  //////////////////////////////////////////////////////////
399  // ellipses
400  //////////////////////////////////////////////////////////
401 
402  /** @brief draws an ellipse at center with half axes a and b
403  @author woelk 05/2005 */
404  static int Ellipse(Image<StorageType>& im, double center[2],
405  double a[2], double b[2],
406  const StorageType Value[]);
407 
408  //////////////////////////////////////////////////////////
409  // wrapper(s) for OpenCV draw functions (Jan Woetzel 2005)
410  //////////////////////////////////////////////////////////
411 
412  // define some olors for easier usage:
413 
414  /** @brief OpenCV: Draw Text into image
415  @param message text to display.
416  @param posX (bottom) left corner of textbox, may be negative and outside image
417  @param posY bottom (left) corner of textbox, may be negative and outside image
418  @param colorRGB color to use
419  @param fontface e.g:
420  CV_FONT_HERSHEY_SIMPLEX - normal size sans-serif font =0
421  CV_FONT_HERSHEY_PLAIN - small size sans-serif font =1 (default)
422  CV_FONT_HERSHEY_DUPLEX - normal size sans-serif font (more complex than CV_FONT_HERSHEY_SIMPLEX)
423  CV_FONT_HERSHEY_COMPLEX - normal size serif font
424  CV_FONT_HERSHEY_TRIPLEX - normal size serif font (more complex than CV_FONT_HERSHEY_COMPLEX)
425  CV_FONT_HERSHEY_COMPLEX_SMALL - smaller version of CV_FONT_HERSHEY_COMPLEX
426  CV_FONT_HERSHEY_SCRIPT_SIMPLEX - hand-writing style font
427  CV_FONT_HERSHEY_SCRIPT_COMPLEX - more complex variant of CV_FONT_HERSHEY_SCRIPT_SIMPLEX
428  @author Jan Woetzel 2005 */
429  static void Text(BIAS::Image<StorageType>& dstImg,
430  const std::string & message,
431  const int & posX=0,
432  const int & posY=20,
433  const ColourRGB<StorageType> &colorRGB=ColourRGB<StorageType>(255,255,255),
434  const int fontface=1,
435  const double hscale=1.0,
436  const double vscale=1.0,
437  const double shear=0,
438  const int thickness=1,
439  const int linetype=8
440  );
441 
442  /** @brief ImageMagick: Draw Text into image
443  @param message text to display.
444  @param posX (bottom) left corner of textbox, may be negative and outside image
445  @param posY bottom (left) corner of textbox, may be negative and outside image
446  @param colorRGB color to use
447  @date 11/2010
448  @author ischiller */
449  static void TextIM(BIAS::Image<StorageType>& dstImg,
450  const std::string & message,
451  const int & posX=0,
452  const int & posY=20,
453  const ColourRGB<StorageType> &colorRGB=ColourRGB<StorageType>(255,255,255),
454  const double hscale=1.0,
455  const double vscale=1.0,
456  const double shear=0,
457  const int thickness=1,
458  const int linetype=8
459  );
460 
461  /** OpenCV arrow drawing
462  @author woelk 12 / 2007 */
463  static void Arrow(Image<StorageType>& im, const int startX,
464  const int startY, const int endX, const int endY,
465  const BIAS::ColourRGB<StorageType> &colorRGB
466  = ColourRGB<StorageType>(255, 255, 255),
467  const int thickness=1 );
468 
469  /** OpenCV: Draws a circle
470  @author JW 2005 */
471  static void Circle(BIAS::Image<StorageType>& dstImg,
472  const int & centerX,
473  const int & centerY,
474  const int & radius=3,
475  const BIAS::ColourRGB<StorageType> &colorRGB=ColourRGB<StorageType>(255,255,255),
476  const int & thickness=1,
477  const int & linetype=8,
478  const int & shift=0
479  );
480 
481  /** OpenCV: Draws a line segment connecting two points
482  @author JW 2005 */
483  static void Line(BIAS::Image<StorageType>& dstImg,
484  const int & pt1X,
485  const int & pt1Y,
486  const int & pt2X,
487  const int & pt2Y,
488  const BIAS::ColourRGB<StorageType> &colorRGB,
489  const int thickness=1,
490  const int linetype=8,
491  const int shift=0
492  );
493 
494  /** @brief OpenCV: Draws a simple, thick or filled rectangle @author JW 2005
495  @authior Jan Woetzel 2005 */
496  static void Rectangle(
497  BIAS::Image<StorageType>& dstImg,
498  const int & pt1X,
499  const int & pt1Y,
500  const int & pt2X,
501  const int & pt2Y,
502  const BIAS::ColourRGB<StorageType> &colorRGB=ColourRGB<StorageType>(255,255,255),
503  const int thickness=1,
504  const int linetype=8,
505  const int shift=0
506  );
507 
508  /** OpenCV: Draws simple or thick elliptic arc or fills ellipse sector
509  angles are given in degree
510  @author JW 2005 */
511  static void Ellipse(
512  BIAS::Image<StorageType>& dstImg,
513  const int & centerX,
514  const int & centerY,
515  const int & axesX,
516  const int & axesY,
517  const double & angle,
518  const double & start_angle=0,
519  const double & end_angle=360,
520  const BIAS::ColourRGB<StorageType> &colorRGB=ColourRGB<StorageType>(255,255,255),
521  const int & thickness=1,
522  const int & linetype=8,
523  const int & shift=0
524  );
525 
526 
527 
528 
529  }; // class ImageDraw
530 
531 } // namespace
532 
533 #endif // _BIAS_IMAGE_DRAW_H_
ImgPosition(const ImgPosition &pos)
Definition: ImageDraw.hh:48
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
interface class used to ease handover in function calls
Definition: ImageDraw.hh:43
ImgPosition(const int x, const int y)
Definition: ImageDraw.hh:47
static int InterpolatedLineGrey(Image< StorageType > &im, unsigned int coo[4], const StorageType Value)
Definition: ImageDraw.hh:324
interface class used to ease handover in function calls
Definition: ColourRGB.hh:34
const int & operator[](const int i) const
Definition: ImageDraw.hh:54
static int InterpolatedCircleCenter(Image< StorageType > &im, unsigned int CenterX, unsigned int CenterY, unsigned int Radius, const std::vector< StorageType > &Value, const float Thickness=1.0, const float Opacity=1.0)
Convenience wrapper for InterpolatedCircleCenter().
Definition: ImageDraw.hh:371
static int Line(Image< StorageType > &im, unsigned int coo[4], const StorageType value[])
convenience wrapper
Definition: ImageDraw.hh:242
static int RectangleCenter(Image< StorageType > &im, const unsigned coo[2], const int size, const StorageType value[])
convenience wrapper
Definition: ImageDraw.hh:143
int & operator[](const int i)
Definition: ImageDraw.hh:51
Ellipse in 2D with dfifferent representations.
Definition: Ellipse.hh:36
ImgPosition & operator=(const ImgPosition &pos)
Definition: ImageDraw.hh:57
The image template class for specific storage types.
Definition: Image.hh:78
drawing simple entities into the image like rectangles or lines As all functions are static they have...
Definition: ImageDraw.hh:72