00001 /*!@file Image/DrawOps.H functions for drawing on images 00002 */ 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2002 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; filed July 23, 2001, following provisional applications // 00013 // No. 60/274,674 filed March 8, 2001 and 60/288,724 filed May 4, 2001).// 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Dirk Walther <walther@caltech.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/DrawOps.H $ 00035 // $Id: DrawOps.H 14653 2011-03-30 03:56:15Z dberg $ 00036 // 00037 00038 #ifndef IMAGE_DRAWOPS_H_DEFINED 00039 #define IMAGE_DRAWOPS_H_DEFINED 00040 00041 #include "Util/Types.H" 00042 #include "Image/SimpleFont.H" 00043 #include "Image/Pixels.H" 00044 00045 #include <string> 00046 #include <vector> 00047 00048 template <class T> class Point2D; 00049 class Dims; 00050 class Rectangle; 00051 template <class T> class Image; 00052 template <class T> class PixRGB; 00053 class ColorMap; 00054 00055 //! draw the trajectory for the points in foas onto img 00056 /*! Yellow circles and red arrows are drawn, connecting the points in foas. 00057 @param foas a vector with the foci of attention ised for drawing*/ 00058 void drawTraj(Image< PixRGB<byte> >& img, 00059 const Point2D<int>* foas_begin, const Point2D<int>* foas_end); 00060 00061 //! build a combo between a color and greyscale image 00062 /*! @param colimg the color image (e.g., trajectory) 00063 @param bwimg the greyscale image (e.g., saliency map) 00064 @param xcombo will combine horizontally if true, otherwise vertically 00065 @param interp use bilinear interpolation for any image rescaling */ 00066 Image< PixRGB<byte> > colGreyCombo(const Image< PixRGB<byte> >& colimg, 00067 const Image<float>& bwimg, 00068 const bool xcombo, 00069 const bool interp = true); 00070 00071 //! build a combo between a color and color image 00072 /*! @param colimg1 the color image (e.g., trajectory + saliency map) 00073 @param colimg2 the second color image (e.g., the scene highlighted by TRM) 00074 @param xcombo will combine horizontally if true, otherwise vertically 00075 @param interp use bilinear interpolation for any image rescaling */ 00076 Image< PixRGB<byte> > colColCombo(const Image< PixRGB<byte> >& colimg1, 00077 const Image< PixRGB<byte> >& colimg2, 00078 const bool xcombo, 00079 const bool interp = true); 00080 00081 //! highlight an image in the regions given by a mask 00082 Image< PixRGB<byte> > highlightRegions(const Image< PixRGB<byte> >& img, 00083 const Image<byte>& mask, 00084 const int maxval = 320); 00085 00086 //! warp image onto height map 00087 Image< PixRGB<byte> > warp3Dmap(const Image< PixRGB<byte> >& img, 00088 const Image<float>& hmap, 00089 const float pitch, const float yaw, 00090 Dims& imdims); 00091 00092 //! Set values at all locations where mask is non-zero 00093 void inplaceSetValMask(Image<float>& dest, 00094 const Image<byte>& mask, const float val); 00095 00096 00097 //! Draws a disk with color "value" in image 00098 template <class T_or_RGB> 00099 void drawDisk(Image<T_or_RGB>& dst, 00100 const Point2D<int>& center, const int radius, 00101 const T_or_RGB value); 00102 00103 //! Draw a line from p1 to p2 of color col and pixel radius rad 00104 template <class T_or_RGB> 00105 void drawLine(Image<T_or_RGB>& dst, 00106 const Point2D<int>& p1, const Point2D<int>& p2, const T_or_RGB col, 00107 const int rad = 1); 00108 00109 //! Draw a line at pos angle ori(rad) and length len 00110 template <class T_or_RGB> 00111 void drawLine(Image<T_or_RGB>& dst, 00112 const Point2D<int>& pos, float ori, float len, const T_or_RGB col, 00113 const int rad = 1); 00114 00115 //! Draw a corner at pos and ori with an ang 00116 template <class T_or_RGB> 00117 void drawCorner(Image<T_or_RGB>& dst, 00118 const Point2D<int>& pos, float ori, float ang, float len, 00119 const T_or_RGB col, const int rad = 1); 00120 00121 00122 //! Draw a rectangle of color col and line width of rad*2, with rounded corners 00123 template <class T_or_RGB> 00124 void drawRect(Image<T_or_RGB>& dst, 00125 const Rectangle& rect, const T_or_RGB col, const int rad = 1); 00126 00127 //! Draw a rectangle of the given color and line width, with square corners 00128 template <class T_or_RGB> 00129 void drawRectSquareCorners(Image<T_or_RGB>& dst, const Rectangle &r, 00130 const T_or_RGB color, const int linewidth); 00131 00132 //! Draw a rectangle of color col and pixel radius rad, rotate by ori 00133 template <class T_or_RGB> 00134 void drawRectOR(Image<T_or_RGB>& dst, 00135 const Rectangle& rect, const T_or_RGB col, const int rad = 1, 00136 const float ori = 0.0F); 00137 00138 //! Draw a rectangle of color col and pixel radius rad, no size assertions 00139 template <class T_or_RGB> 00140 void drawRectEZ(Image<T_or_RGB>& dst, 00141 const Rectangle& rect, const T_or_RGB col, const int rad = 1); 00142 00143 //! Draw a filled rectangle with the given fill value 00144 template <class T_or_RGB> 00145 void drawFilledRect(Image<T_or_RGB>& dst, const Rectangle& rect, 00146 const T_or_RGB fillVal); 00147 00148 //! Draw a cross of color col, half-size siz and pixel radius rad 00149 template <class T_or_RGB> 00150 void drawCross(Image<T_or_RGB>& dst, 00151 const Point2D<int>& p, const T_or_RGB col, const int siz = 3, 00152 const int rad = 1); 00153 00154 //! Draw a cross of color col, half-size siz and pixel radius rad 00155 template <class T_or_RGB> 00156 void drawCrossOR(Image<T_or_RGB>& dst, 00157 const Point2D<int>& p, const T_or_RGB col, const int siz = 3, 00158 const int rad = 1, const float ori = 0.0F); 00159 00160 //! Draw a square patch of color col and size siz 00161 template <class T_or_RGB> 00162 void drawPatch(Image<T_or_RGB>& dst, 00163 const Point2D<int>& p, const int siz, const T_or_RGB col); 00164 00165 //! Draw a square patch of color col and size siz, with a black border 00166 template <class T_or_RGB> 00167 void drawPatchBB(Image<T_or_RGB>& dst, 00168 const Point2D<int>& p, const int siz, const T_or_RGB col, const T_or_RGB bgcol = T_or_RGB()); 00169 00170 //! Draw a circle of color col and radius radius, with pixel radius rad 00171 template <class T_or_RGB> 00172 void drawCircle(Image<T_or_RGB>& dst, 00173 const Point2D<int>& p, const int radius, const T_or_RGB col, 00174 const int rad = 1); 00175 00176 //! Draw a ellipse of color col and radius radiusx, radiusy, with pixel radius rad 00177 template <class T_or_RGB> 00178 void drawEllipse(Image<T_or_RGB>& dst, 00179 const Point2D<int>& p, const int radiusx, 00180 const int radiusy, const T_or_RGB col, const int rad = 1); 00181 00182 //! Draw an arrow from p1 to p2 with pixel radius rad 00183 template <class T_or_RGB> 00184 void drawArrow(Image<T_or_RGB>& dst, 00185 const Point2D<int>& p1, const Point2D<int>& p2, const T_or_RGB col, 00186 const int rad = 1); 00187 00188 //! Draw a grid 00189 template <class T_or_RGB> 00190 void drawGrid(Image<T_or_RGB>& dst, 00191 const int spacingX, const int spacingY, 00192 const int thickX, const int thickY, const T_or_RGB col); 00193 00194 //! Draw an nx * ny grid that exactly partitions the given image 00195 template <class T_or_RGB> 00196 void drawGrid(Image<T_or_RGB>& dst, const uint nx, const uint ny, 00197 const int thick, const T_or_RGB col); 00198 00199 //! Draw a superquadric shape. nSeg defines the quality of the shape. ie. how many line to draw 00200 template <class T_or_RGB> 00201 void drawSuperquadric(Image<T_or_RGB>& dst, 00202 const Point2D<int>& p, 00203 const float a, const float b, const float e, const T_or_RGB col, 00204 const float rot = 0, const float k1 = 0, const float k2 = 0, 00205 const float thetai = -M_PI, 00206 const float thetaf = M_PI, 00207 const int rad = 1, 00208 const int nSeg = 40); 00209 00210 00211 //! Draw a 2D contour 00212 /*! Will draw a disk of color col and radius rad in dst at every point where src is on a contour. dst should be 00213 initialized ahd have same dims as src. Nothing is drawn for point off contours. */ 00214 template <class T, class T_or_RGB> 00215 void drawContour2D(const Image<T>& src, Image<T_or_RGB>& dst, const T_or_RGB &col, const byte rad); 00216 00217 enum TextAnchor 00218 { 00219 ANCHOR_TOP_LEFT, 00220 ANCHOR_TOP_RIGHT, 00221 ANCHOR_BOTTOM_LEFT, 00222 ANCHOR_BOTTOM_RIGHT 00223 }; 00224 00225 //! Write some text in normal font; only ASCII chars 32 .. 126 are supported 00226 /*! See Image/SimpleFont.H for font choices. */ 00227 template <class T_or_RGB> 00228 void writeText(Image<T_or_RGB>& dst, 00229 const Point2D<int>& pt, const char* text, 00230 const T_or_RGB col = T_or_RGB(), 00231 const T_or_RGB bgcol = T_or_RGB(255), 00232 const SimpleFont& f = SimpleFont::FIXED(10), 00233 const bool transparent_bg = false, 00234 const TextAnchor anchor = ANCHOR_TOP_LEFT); 00235 00236 /// Make an image containing one or more lines of text 00237 /** The function will choose a font width that fits the desired image 00238 size. 00239 00240 @param w the desired width (in pixels) of the resulting image 00241 @param lines an array of strings to be written in the result, one 00242 line per string 00243 @param nlines the number of strings in the string array 00244 @param col the foreground color for the written text 00245 @param bg the background color for the written text 00246 @param max_chars_per_line if non-zero, then each line of text will 00247 be truncated at this many characters 00248 @param fontwidth the max font width unless it is zero, in which case 00249 it will be determined automatically to fit the text in the box. 00250 */ 00251 template <class T_or_RGB> 00252 Image<T_or_RGB> makeMultilineTextBox(const int w, 00253 const std::string* lines, 00254 const size_t nlines, 00255 const T_or_RGB col, 00256 const T_or_RGB bg = T_or_RGB(), 00257 const size_t max_chars_per_line = 0, 00258 const int fontwidth = 0); 00259 00260 //! draw a dot which is a point with 4 connected points 00261 /*! this is between the setVal method and drawCircle method for 00262 illustrating points 00263 */ 00264 template <class T_or_RGB> 00265 void drawPoint(Image<T_or_RGB>& dst, 00266 int X,int Y, T_or_RGB pix); 00267 00268 //! Draws a disk in image and check if it hits a target 00269 /*! @param mask input binary target mask to draw into 00270 @param center coordinates of disk center 00271 @param radius radisu of disk, in pixels 00272 @param value pixel value used to draw the disk 00273 @param targetval value with which the targets are represented 00274 @param floodval value to flood target with if target hit 00275 @return number of targets hit and flooded */ 00276 template <class T> 00277 int drawDiskCheckTarget(Image<T>& dst, 00278 Image<T>& mask, const Point2D<int>& center, 00279 const int radius, const T value, 00280 const T targetval, const T floodval); 00281 00282 //! Warp image in 3D onto z-map 00283 /*! @param ima Input image 00284 @param zmap Height map of same size as ima 00285 @param pitch 3D pitch angle, in degrees 00286 @param yaw 3D yaw angle, in degrees 00287 @param dims The result will have these dimensions, unless the dimensions are 00288 empty (i.e. 0-by-0), in which case the result will be the best size computed 00289 from ima's size and the pitch and yaw angles. 00290 */ 00291 template <class T> 00292 Image<PixRGB<T> > warp3D(const Image<PixRGB<T> >& ima, 00293 const Image<float>& zmap, 00294 const float pitch, const float yaw, 00295 const float zMax, Dims& dims); 00296 00297 //! Format a feature map for display 00298 /*! Take an Image<float> feature map as input and convert it to 00299 PixRGB<byte> by either multiplying the values by a factor or by 00300 normalizing them to 0 .. 255 (if factor is 0.0), and then using a 00301 given color map (which will also be displayed if showColorScale is 00302 true). Then rescale to the specified dims, using interpolation if 00303 desired. Then write a descriptor label in the top-left corner using 00304 a large font, and finally show the original range of values in the 00305 bottom-right corner uing a tiny font. */ 00306 Image< PixRGB<byte> > 00307 formatMapForDisplay(const Image<float>& img, const float factor, 00308 const Dims& newdims, const bool useInterp, 00309 const ColorMap& cmap, const bool showColorScale, 00310 const char *label); 00311 00312 //! Generate a plot from a vector of points. Choose numticks < 0 to 00313 //! disable x and y axes tick marks. class U should be a deque or vector 00314 template <typename T, class U> 00315 Image< PixRGB<byte> > 00316 linePlot(const U& points, const int w, const int h, 00317 const T& minVal = T(), const T& maxVal = T(), 00318 const char *title = "", 00319 const char *yLabel = "", const char *xLabel = "", 00320 const PixRGB<byte>& linecol=PixRGB<byte>(0,0,0), 00321 const PixRGB<byte>& bckcol=PixRGB<byte>(255,255,255), 00322 const int numticks = 4, const bool axisonright = false); 00323 00324 //! Generate multiline plot from a vector of lines 00325 00326 template <typename T> 00327 Image< PixRGB<byte> > 00328 multilinePlot(const std::vector<std::vector<T> >& lines, 00329 const int w, const int h, T minVal = T(), 00330 T maxVal = T(), const char *title = "", 00331 const char *xLabel = "", const char *yLabel = "", 00332 const std::vector<PixRGB<byte> >& linescolor = std::vector<PixRGB<byte> >(0), 00333 const PixRGB<byte>& gridcolor=PixRGB<byte>(255,0,0), 00334 const PixRGB<byte>& bckcolor=PixRGB<byte>(255,255,255)); 00335 00336 //! Draw a polygon with outlined polygon, no change outside 00337 template <class T_or_RGB> 00338 void drawOutlinedPolygon(Image<T_or_RGB>& img, 00339 const std::vector<Point2D<int> >& polygon, 00340 const T_or_RGB col, 00341 const Point2D<int> trans = Point2D<int>(0,0), 00342 const float rot = 0.0, 00343 const float scale = 1.0, 00344 const float k1 = 0, 00345 const float k2 = 0, 00346 const int rad = 1); 00347 00348 //! Draw a filled polygon with color col inside the polygon, no change outside 00349 template <class T_or_RGB> 00350 void drawFilledPolygon(Image<T_or_RGB>& img, 00351 const std::vector<Point2D<int> >& polygon, 00352 const T_or_RGB col); 00353 00354 //! Simple struct to define a bargraph meter 00355 struct MeterInfo 00356 { 00357 std::string label; 00358 double val; 00359 double valmax; 00360 double thresh; 00361 PixRGB<byte> color; 00362 }; 00363 00364 //! Draw some bargraph meters 00365 Image<PixRGB<byte> > drawMeters(const MeterInfo* infos, const size_t ninfo, 00366 const size_t nx, const Dims& meterdims); 00367 00368 00369 template <class T> 00370 Image<T> drawHistogram(std::vector<float> hist, int width, int height, T lineVal, T fillVal ); 00371 00372 // ###################################################################### 00373 /* So things look consistent in everyone's emacs... */ 00374 /* Local Variables: */ 00375 /* indent-tabs-mode: nil */ 00376 /* End: */ 00377 00378 00379 #endif // !IMAGE_DRAWOPS_H_DEFINED