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

DualCoding 5.1CVS
Generated Mon May 9 04:56:28 2016 by Doxygen 1.6.3