00001 /*!@file MBARI/BitObject.H class that holds an object defined by a shape in a 00002 bit image */ 00003 00004 // //////////////////////////////////////////////////////////////////// // 00005 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00006 // by the University of Southern California (USC) and the iLab at USC. // 00007 // See http://iLab.usc.edu for information about this project. // 00008 // //////////////////////////////////////////////////////////////////// // 00009 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00010 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00011 // in Visual Environments, and Applications'' by Christof Koch and // 00012 // Laurent Itti, California Institute of Technology, 2001 (patent // 00013 // pending; application number 09/912,225 filed July 23, 2001; see // 00014 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00015 // //////////////////////////////////////////////////////////////////// // 00016 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00017 // // 00018 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00019 // redistribute it and/or modify it under the terms of the GNU General // 00020 // Public License as published by the Free Software Foundation; either // 00021 // version 2 of the License, or (at your option) any later version. // 00022 // // 00023 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00024 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00025 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00026 // PURPOSE. See the GNU General Public License for more details. // 00027 // // 00028 // You should have received a copy of the GNU General Public License // 00029 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00030 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00031 // Boston, MA 02111-1307 USA. // 00032 // //////////////////////////////////////////////////////////////////// // 00033 // 00034 // Primary maintainer for this file: Dirk Walther <walther@caltech.edu> 00035 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/MBARI/BitObject.H $ 00036 // $Id: BitObject.H 9412 2008-03-10 23:10:15Z farhan $ 00037 // 00038 00039 #ifndef BITOBJECT_H_DEFINED 00040 #define BITOBJECT_H_DEFINED 00041 00042 #include "Image/Image.H" 00043 #include "Image/Rectangle.H" 00044 #include "MBARI/BitObjectDrawModes.H" 00045 #include "MBARI/Geometry2D.H" 00046 #include "Util/Types.H" 00047 00048 00049 //! Object defined by a connected binary pixel region 00050 /*! This class extracts a connected binary pixel region from a 00051 grayscale image and analyzes a few of its properties*/ 00052 00053 class BitObject 00054 { 00055 public: 00056 00057 //! default Constructor 00058 BitObject(); 00059 00060 //! Constructor from a byte Image and a location 00061 /*!@param img Image from which the object is extracted 00062 @param location a point that is inside the object - the object 00063 is flooded starting from this seed 00064 @param threshold for flooding*/ 00065 BitObject(const Image<byte>& img, const Point2D<int> location, 00066 const byte threshold = 1); 00067 00068 //! Construct from a byte Image 00069 /*! @param img image containing only the object 00070 the object pixels are 1, all other pixels are 0*/ 00071 BitObject(const Image<byte>& img); 00072 00073 //! Construct from the data contained in the input stream is 00074 BitObject(std::istream& is); 00075 00076 // default copy constructor, operator= and destructor okay 00077 00078 //! Reset to a new object 00079 /*!@param img Image from which the object is extracted 00080 @param location a point that is inside the object - the object 00081 is flooded starting from this seed 00082 @param threshold for flooding 00083 @return a mask of the extracted object (in IMAGE coordinates) */ 00084 Image<byte> reset(const Image<byte>& img, const Point2D<int> location, 00085 const byte threshold = 1); 00086 00087 //! Reset to a new object 00088 /*! @param img image containing only the object 00089 the object pixels are 1, all other pixels are 0; 00090 @return the area of the extracted object; -1 if no object could 00091 be extracted - in this case the BitObject is invalid */ 00092 int reset(const Image<byte>& img); 00093 00094 //! delete all stored data, makes the object invalid 00095 void freeMem(); 00096 00097 //! write the entire BitObject to the output stream os 00098 void writeToStream(std::ostream& os) const; 00099 00100 //! read the BitObject from the input stream is 00101 void readFromStream(std::istream& is); 00102 00103 //! Coordinate system for return values 00104 /*! These values are used to specify whether return values should be 00105 given in coordinates of the extracted object or in coordinates 00106 of the image from which the object was extracted.*/ 00107 enum Coords { OBJECT, IMAGE }; 00108 00109 //! Extract the maximum, minimum and average intensity over the object shape in img 00110 /*! The results are stored in BitObject and can be accessed 00111 using getMaxMinAvgIntensity*/ 00112 template <class T> 00113 void setMaxMinAvgIntensity(const Image<T>& img); 00114 00115 //! Return the maximum, minimum and average intensity 00116 /*! See setMinMaxAvgIntensity for details*/ 00117 void getMaxMinAvgIntensity(float& maxIntensity, float& minIntensity, 00118 float& avgIntensity); 00119 00120 //! Returns the bounding box of the object 00121 Rectangle getBoundingBox(const Coords coords = IMAGE) const; 00122 00123 //! Returns the object as a binary mask 00124 /*!@param value the value that is used for the object*/ 00125 Image<byte> getObjectMask(const byte value = byte(1), 00126 const Coords coords = IMAGE) const; 00127 00128 //! The dimensions of the bounding box of the object 00129 Dims getObjectDims() const; 00130 00131 //! The location of the upper left corner of the boundary box in the original image 00132 Point2D<int> getObjectOrigin() const; 00133 00134 //! The centroid of the object rounded to integer coordinates 00135 Point2D<int> getCentroid(const Coords coords = IMAGE) const; 00136 00137 //! The centroid, not rounded 00138 Vector2D getCentroidXY(const Coords coords = IMAGE) const; 00139 00140 //! The area (number of pixels) of the object 00141 int getArea() const; 00142 00143 //! Returns the second moments of the object (normalized by the area) 00144 void getSecondMoments(float& uxx, float& uyy, float& uxy); 00145 00146 //! Returns the length of the major axis of the object 00147 float getMajorAxis(); 00148 00149 //! Returns the length of the minor axis of the object 00150 float getMinorAxis(); 00151 00152 //! Returns the elongation of the object (major axis / minor axis) 00153 float getElongation(); 00154 00155 //! Returns the angle between the major axis and the x axis 00156 /* @return "--" is 0; "\" is 45; "|" is 90; "/" is 135 */ 00157 float getOriAngle(); 00158 00159 //! whether the object is valid 00160 /*! This is going to be false if no object could be extracted 00161 from the image that was given at the constructor or at reset */ 00162 bool isValid() const; 00163 00164 //! returns the intersection of this BitObject with other 00165 /*! if the intersection is empty, an invalid BitObject is returned*/ 00166 //BitObject intersection(const BitObject& other); 00167 bool doesIntersect(const BitObject& other) const; 00168 00169 //! draw the shape of this BitObject into img with color 00170 template <class T_or_RGB> 00171 void drawShape(Image<T_or_RGB>&, const T_or_RGB& color, 00172 float opacity = 1.0F); 00173 00174 //! draw the outline of this BitObject into img with color 00175 template <class T_or_RGB> 00176 void drawOutline(Image<T_or_RGB>&, const T_or_RGB& color, 00177 float opacity = 1.0F); 00178 00179 //! draw the bounding box of this BitObject into img with color 00180 template <class T_or_RGB> 00181 void drawBoundingBox(Image<T_or_RGB>&, 00182 const T_or_RGB& color, 00183 float opacity = 1.0F); 00184 00185 //! draw this BitObject according to mode 00186 template <class T_or_RGB> 00187 void draw(BitObjectDrawMode mode, 00188 Image<T_or_RGB>&, 00189 const T_or_RGB& color, 00190 float opacity = 1.0F); 00191 00192 private: 00193 // compute the second moments and values derived from them 00194 void computeSecondMoments(); 00195 00196 Image<byte> itsObjectMask; 00197 Rectangle itsBoundingBox; // in image coordinates 00198 Vector2D itsCentroidXY; // in image coordinates 00199 int itsArea; 00200 float itsUxx, itsUyy, itsUxy; 00201 float itsMajorAxis, itsMinorAxis, itsElongation, itsOriAngle; 00202 Dims itsImageDims; 00203 float itsMaxIntensity, itsMinIntensity, itsAvgIntensity; 00204 bool haveSecondMoments; 00205 }; 00206 00207 #endif 00208 00209 // ###################################################################### 00210 /* So things look consistent in everyone's emacs... */ 00211 /* Local Variables: */ 00212 /* indent-tabs-mode: nil */ 00213 /* End: */