Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

visops.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef _VISOPS_H_
00003 #define _VISOPS_H_
00004 
00005 #include "SketchTypes.h"
00006 #include "Sketch.h"
00007 #include "SketchSpace.h"
00008 
00009 #include "ShapeLine.h"
00010 
00011 class DualCoding::SketchIndices;
00012 
00013 //! Visual routines operators, used in DualCoding.
00014 namespace visops {
00015 
00016   using namespace DualCoding;
00017   using DualCoding::uchar;
00018   using DualCoding::uint;
00019 
00020   //! Connectivity used by oldlabelcc and neighborsum.
00021   enum Connectivity_t { FourWayConnect, EightWayConnect };
00022 
00023   //!@name Sketch creation
00024   //@{
00025 
00026   //! Returns an all-zero Sketch<bool> in the specified sketch space
00027   Sketch<bool> zeros(SketchSpace& space);
00028 
00029   //! Returns an all-zero Sketch<bool> of same size as parent @a sketch
00030   Sketch<bool> zeros(const SketchRoot& sketch);
00031 
00032   //! Returns a deep copy of the sketch: actually copies the pixels
00033   template<class T>
00034   Sketch<T> copy(const Sketch<T>& other) {
00035     Sketch<T> result("copy("+other->getName()+")", other);  // will inherit from parent
00036     *result.pixels = *other.pixels;  // valarray assignment
00037     return result;
00038   }
00039 
00040   //@}
00041 
00042   //!@name Min/max functions
00043   //@{
00044 
00045   //! Max of each pixel with a constant
00046   template<class T>
00047   Sketch<T> max(const Sketch<T>& src, const T value) {
00048     Sketch<T> result("max(const)",src);
00049     for ( unsigned int i = 0; i < src.pixels->size()-1; i++ ) 
00050       (*result.pixels)[i] = std::max((*src.pixels)[i],value);
00051     return result;
00052   }
00053 
00054   //! Max of each pixel with a constant
00055   template<class T>
00056   Sketch<T> max(const Sketch<T>& src, const int value) {
00057     return visops::max(src, (T)(value));
00058   }
00059 
00060 
00061   //! Pixel-wise max of two sketches
00062   template<class T>
00063   Sketch<T> max(const Sketch<T>& arg1, const Sketch<T>& arg2) {
00064     Sketch<T> result("max("+arg1->getName()+","+arg2->getName()+")",arg1);
00065     for ( unsigned int i = 0; i < arg1.pixels->size()-1; i++ ) 
00066       (*result.pixels)[i] = std::max((*arg1.pixels)[i],(*arg2.pixels)[i]);
00067     return result;
00068   }
00069 
00070   //! Min of each pixel with a constant
00071   template<class T>
00072   Sketch<T> min(const Sketch<T>& src, const T value) {
00073     Sketch<T> result("min(const)",src);
00074     for ( unsigned int i = 0; i < src.pixels->size()-1; i++ ) 
00075       (*result.pixels)[i] = std::min((*src.pixels)[i],value);
00076     return result;
00077   }
00078 
00079   //! Min of each pixel with a constant
00080   template<class T>
00081   Sketch<T> min(const Sketch<T>& src, const int value) {
00082     return visops::min(src, (T)(value));
00083   }
00084 
00085   //! Pixel-wise min of two sketches
00086   template<class T>
00087   Sketch<T> min(const Sketch<T>& arg1, const Sketch<T>& arg2) {
00088     Sketch<T> result("min("+arg1->getName()+","+arg2->getName()+")",arg1);
00089     for ( unsigned int i = 0; i < arg1.pixels->size()-1; i++ ) 
00090       (*result.pixels)[i] = std::min((*arg1.pixels)[i],(*arg2.pixels)[i]);
00091     return result;
00092   }
00093 
00094   //@}
00095 
00096   //!@name Region filling
00097   //@{
00098 
00099   //! Fills a region bounded by borders, starting at position given by index
00100   Sketch<bool> seedfill(const Sketch<bool>& borders, size_t index);
00101 
00102   //! Fills the exterior of region bounded by borders, starting from the edges of the sketch; border pixels are not filled
00103   Sketch<bool> fillExterior(const Sketch<bool>& borders);
00104 
00105   //! Fills the interior of region bounded by borders, i.e., pixels not reachable from the edges of the sketch; border pixels are not filled
00106   Sketch<bool> fillInterior(const Sketch<bool>& borders);
00107 
00108   //@}
00109 
00110   //!@name Miscellaneous functions
00111   //@{
00112 
00113   //! Returns all the pixels of the named color.
00114   Sketch<bool> colormask(const Sketch<uchar>& src, const std::string &colorname);
00115 
00116   //! Returns all the pixels with the specified color index.
00117   Sketch<bool> colormask(const Sketch<uchar>& src, color_index cindex);
00118 
00119   //! For each pixel, calculate the sum of its neighbors.
00120   /*! @param im Sketch to use as input.
00121    *  @param connectivity the type of neighbor connectivity to use */
00122   Sketch<uchar> neighborSum(const Sketch<bool> &im, Connectivity_t connectivity=EightWayConnect);
00123   
00124   //! Produces a filled in image based on the Sketch, using 8-way connectivity.
00125   /*! @param im The sketch to which to apply the function.
00126    *  @param iter Number of times to perform the fillin operation.
00127    *  @param min_thresh Fill in pixel if has at least this many neighbors.
00128    *  @param max_thresh Fill in pixel if has fewer than this many neighbors.
00129    *  @param remove_only Set to true if you know you will only be deleting pixels for a speedup */
00130   Sketch<bool> fillin(const Sketch<bool> &im, int iter, 
00131           uchar min_thresh, uchar max_thresh,
00132           bool remove_only=false);
00133   
00134   //@}
00135 
00136   //!@name Wavefront algorithms: distance, connected components
00137   //@{
00138 
00139   /*! @brief Calculates the distance from each pixel in the image to the closest
00140     true pixel in destination @a dest, using the wavefront algorithm.
00141     Obstacles indicated by true values in pixels of @a obst.  Note: use maxdist=width+height
00142     if you want the result to be viewable with the jetMapScaled colormap.
00143   */
00144   Sketch<uint> bdist(const Sketch<bool> &dest, const Sketch<bool> &obst, 
00145           const uint maxdist=(uint)-1);
00146 
00147   //! Euclidean distance to the nearest true pixel in @a dest
00148   /*! Should calculate the Euclidean distance from each pixel in the image
00149    *  to the closest true pixel in dest, using a linear-time algorithm.
00150    *  Currently calculates Manhattan distance, which is good enough.
00151    *  Should be used instead of bdist if not concerned about obstacles. */
00152   Sketch<uint> edist(const Sketch<bool> &dest);
00153   
00154   //! Connected components labeling using CMVision.  Components numbered sequentially from 1.
00155   Sketch<uint> labelcc(const Sketch<bool>& source, int minarea=1);
00156   
00157   //! Old connected-components code written using pure sketch primitives.
00158   /*! Returns a connected-components labeling of the foreground.
00159       Each different foreground region will contain a unique positive integer. 
00160       No guarantees on the integer values. */
00161   Sketch<uint> oldlabelcc(const Sketch<bool>& source, 
00162           Connectivity_t connectivity=EightWayConnect);
00163 
00164   //! Each pixel of the result is the area of that connected component.
00165   Sketch<uint> areacc(const Sketch<bool>& source, Connectivity_t connectivity=EightWayConnect);
00166 
00167   //! Each pixel of the result is the area of that connected component.
00168   Sketch<uint> areacc(const Sketch<uint>& labels);
00169 
00170   //! Low-pass filter by eliminating small regions
00171   Sketch<bool> minArea(const Sketch<bool>& sketch, int minval=5);
00172 
00173   //@}
00174 
00175   //! @name Masking and conditional assignment
00176   //@{
00177   //! Returns pixels of @a A masked by bool sketch @a B
00178   template<typename T>
00179   Sketch<T> mask(const Sketch<T> &A, const Sketch<bool> &B) {
00180     Sketch<T> result("mask("+A->getName()+","+B->getName()+")", A);
00181     T* Aptr = &(*A.pixels)[0];
00182     T* Rptr = &(*result.pixels)[0];
00183     T* Rend = &(*result.pixels)[result->getNumPixels()];
00184     unsigned int idx = 0;
00185     while ( Rptr != Rend )
00186       *Rptr++ = B[idx++] * (*Aptr++);
00187     return result;
00188   }
00189 
00190   //! Result holds non-zero pixels of @a A, with zero pixels filled in by @a B.
00191   /*! Equivalent to writing maskedAssign(A,A==0,B) */
00192   template<typename T>
00193   Sketch<T> ifNot(const Sketch<T> &A, const Sketch<T> &B) {
00194     Sketch<T> result("ifNot("+A->getName()+","+B->getName()+")", A);
00195     T* Aptr = &(*A.pixels)[0];
00196     T* Bptr = &(*B.pixels)[0];
00197     T* Rptr = &(*result.pixels)[0];
00198     T* Rend = &(*result.pixels)[result->getNumPixels()];
00199     while ( Rptr != Rend ) {
00200       *Rptr++ = ( *Aptr != 0 ) ? *Aptr : * Bptr;
00201       *Aptr++; Bptr++;
00202     }
00203     return result;
00204   }
00205 
00206   //! Returns a result where pixels of @a sketch for which @a mask is true have been replaced by @a value.
00207   template<typename T, typename Tv>
00208   Sketch<T> maskedAssign(const Sketch<T> &sketch, const Sketch<bool> &mask, const Tv value) {
00209     Sketch<T> result("maskedAssign("+sketch->getName()+")",sketch);
00210     T* Psrc = &(*sketch.pixels)[0];
00211     T* Pdest = &(*result.pixels)[0];
00212     T* Edest = &(*result.pixels)[sketch->getNumPixels()];
00213     bool* Pmask =&(*mask.pixels)[0];
00214     const T val = (T)value;
00215     while ( Pdest != Edest ) {
00216       *Pdest++ = *Pmask++ ? val : *Psrc;
00217       Psrc++;
00218     }
00219     return result;
00220   }
00221 
00222   //! Returns a result where pixels of @a sketch for which @a mask is true have been replaced by corresponding pixels of @a value.
00223   template<typename T>
00224   Sketch<T> maskedAssign(const Sketch<T> &sketch, const Sketch<bool> &mask, const Sketch<T> &value) {
00225     Sketch<T> result("maskedAssign("+sketch->getName()+")",sketch);
00226     T* Psrc = &(*sketch.pixels)[0];
00227     T* Pdest = &(*result.pixels)[0];
00228     T* Edest = &(*result.pixels)[sketch->getNumPixels()];
00229     bool* Pmask = &(*mask.pixels)[0];
00230     T* Pval = &(*value.pixels)[0];
00231     while ( Pdest != Edest ) {
00232       *Pdest++ = *Pmask++ ? *Pval : *Psrc;
00233       Pval++;
00234       Psrc++;
00235     }
00236     return result;
00237   }
00238   //@}
00239 
00240   //!@name Edge and symmetry detection
00241   //@{
00242 
00243   //! Simple edge finding.  Use SUSAN for more sophisticated edge detection.
00244   /*! This edge-finding algorithm is inefficient, and produces offset results
00245    *  for top and left edges.  Should replace it with something better. */
00246   Sketch<bool> edge(const Sketch<bool> &im); 
00247   
00248   //! Horizontal symmetry points.
00249   /*! @brief Returns non-zero values along points of horizontal symmetry, with
00250    *  each of these values equal to the distance to the symmetric points.
00251    *  @param sketch The sketch to which to apply the function.
00252    *  @param minskip The min accepted distance between pixels for symmetry.
00253    *  @param maxskip The max accepted distance between pixels for symmetry. */
00254   Sketch<bool> horsym(const Sketch<bool>& sketch, 
00255          int minskip=3, int maxskip=80);
00256 
00257   //! Vertical symmetry points.
00258   /*! @brief Returns non-zero values along points of vertical symmetry, with
00259    *  each of these values equal to the distance to the symmetric points.
00260    *  @param sketch The sketch to which to apply the function.
00261    *  @param minskip The min accepted distance between pixels for symmetry.
00262    *  @param maxskip The max accepted distance between pixels for symmetry. */
00263   Sketch<bool> versym(const Sketch<bool>& sketch, 
00264          int minskip=3, int maxskip=80);
00265   
00266   /*! @brief returns a skeleton of @a sketch, with pixel values corresponding to 
00267    *  distance of symmetry */
00268   Sketch<bool> skel(const Sketch<bool>& sketch); 
00269 
00270   //@}
00271   
00272   //!@name Sketch dissection
00273   //@{
00274   //! Half-plane functions fill in the half plane on one side of a line.
00275   //@{
00276   Sketch<bool> leftHalfPlane(const Shape<LineData> &ln);
00277 
00278   Sketch<bool> rightHalfPlane(const Shape<LineData> &ln);
00279 
00280   Sketch<bool> topHalfPlane(const Shape<LineData> &ln);
00281 
00282   Sketch<bool> bottomHalfPlane(const Shape<LineData> &ln);
00283   //@}
00284 
00285   //! Returns a copy of im except that its pixels within offset from boundaries are removed
00286   Sketch<bool> non_bounds(const Sketch<bool>& im, int offset);
00287   //@}
00288 
00289   //!@name Image manipulation primitives
00290   //@{
00291   //! Runs the SUSAN edge detector on a grayscale image
00292   Sketch<uchar> susan_edges(const Sketch<uchar>& im, int brightness);
00293 
00294   //! Returns a Sketch<bool> indicating edge points found by SUSAN
00295   Sketch<bool> susan_edge_points(const Sketch<uchar>& im, int brightness);
00296   
00297   //! Convolves a kernel with an image.
00298   Sketch<uint> convolve(const Sketch<uchar> &sketch, Sketch<uchar> &kernel, 
00299        int i, int j, int width, int height);
00300 
00301   //! Convolves a kernel with an image, normalizing the kernel to zero mean.
00302   Sketch<uint> templateMatch(const Sketch<uchar> &sketch, Sketch<uchar> &kernel, 
00303        int i, int j, int width, int height);
00304 
00305   //@}
00306 
00307 } // namespace
00308 
00309 #endif

DualCoding 4.0
Generated Thu Nov 22 00:52:37 2007 by Doxygen 1.5.4