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

DualCoding 3.0beta
Generated Wed Oct 4 00:01:54 2006 by Doxygen 1.4.7