contrib/gel/vdgl/vdgl_digital_region.h
Go to the documentation of this file.
00001 // This is gel/vdgl/vdgl_digital_region.h
00002 #ifndef vdgl_digital_region_h_
00003 #define vdgl_digital_region_h_
00004 //:
00005 // \file
00006 // \brief A representation of the digital interior of a region.
00007 //
00008 //  Maintains the discrete geometry and intensity data of a Face, a 2-d
00009 //  analog of EdgelChain. So far the class is a very simple group of 1-d
00010 //  arrays for holding the x and y pixel locations and the intensity.
00011 //  The class maintains 2d pixels in the style of TargetJr and could be
00012 //  used to represent 2-d intensity points
00013 //
00014 // \author
00015 //   Joe Mundy November 27, 1999
00016 //   GE Corporate Research and Development.
00017 //
00018 // \verbatim
00019 // Modifications
00020 //   8-May-2002 - Peter Vanroose - now inherits from vsol_region_2d
00021 //  15-May-2002 - Peter Vanroose - inconsistency Xi() versus Ix() removed
00022 //                (There were three pairs of data members both referring to
00023 //                 intensity information, but only one of them was updated.)
00024 //   8-Jan-2003 - Peter Vanroose - added is_convex() (virtual of vsol_region_2d)
00025 //  22-Sep-2004 - Peter Vanroose - removed 3D interface: class is intrinsically 2D
00026 //  22-Sep-2004 - Peter Vanroose - added transform() (projective transformation)
00027 //  22-Sep-2004 - Peter Vanroose - added histogram() and residual_histogram()
00028 // \endverbatim
00029 //-----------------------------------------------------------------------------
00030 
00031 #include <vcl_vector.h>
00032 #include <vcl_string.h>
00033 #include <vnl/vnl_double_3x3.h>
00034 #include <vnl/vnl_float_3x3.h>
00035 #include <vnl/vnl_float_2.h>
00036 #include <vsol/vsol_region_2d.h>
00037 #include <vsol/vsol_point_2d.h>
00038 class vdgl_digital_region : public vsol_region_2d
00039 {
00040  public:
00041 
00042   // Constructors/Initializers/Destructors---------------------------------
00043   vdgl_digital_region()
00044   : vsol_region_2d(),
00045     npts_(0), pixel_size_(1.f), xp_(0), yp_(0), pix_(0),
00046     max_(0), min_((unsigned short)(-1)), xo_(0.f), yo_(0.f),
00047     io_(0.f), io_stdev_(0.0f), pix_index_(0),
00048     fit_valid_(false), scatter_matrix_valid_(false),
00049     X2_(0), Y2_(0), I2_(0), XY_(0), XI_(0), YI_(0), error_(0), sigma_sq_(0) {}
00050 
00051   vdgl_digital_region(int npts, const float* xp, const float* yp, const unsigned short *pix);
00052   vdgl_digital_region(vdgl_digital_region const& r);
00053   ~vdgl_digital_region();
00054 
00055   // Data Access-----------------------------------------------------------
00056   // Data storage for the pixel arrays
00057   void ResetPixelData();
00058   void IncrementMeans(float x, float y, unsigned short pix);
00059   void InitPixelArrays();
00060   void InsertInPixelArrays(float x, float y, unsigned short pix);
00061 
00062   //: The region pixel coordinates and intensities
00063   float const* Xj() const {return xp_;}
00064   float const* Yj() const {return yp_;}
00065   unsigned short const* Ij() const {return pix_;}
00066   unsigned int Npix() const {return npts_;}
00067 
00068   //: The size of a region pixel in image pixel units
00069   //(due to expanded resolution processing)
00070   void set_pixel_size(float pixel_size){pixel_size_= pixel_size;}
00071   float get_pixel_size() const {return pixel_size_;}
00072 
00073   // Min and Max region intensities
00074   float get_min() const { return min_; }
00075   float get_max() const { return max_; }
00076   //: reset the iterator for accessing region pixels
00077   void reset() const { pix_index_ = -1; }
00078   //: increment to next pixel and check if iterator is finished
00079   bool next() const { ++pix_index_; return pix_index_<(int)npts_; }
00080   float X() const;    //!< The x pixel coordinate
00081   float Y() const;    //!< The y pixel coordinate
00082   unsigned short I() const;//!< The pixel intensity
00083 
00084   void set_X(float x);    //!< change x pixel coordinate
00085   void set_Y(float y);    //!< change y pixel coordinate
00086   void set_I(unsigned short I);    //!< change pixel intensity
00087 
00088   // The mean geometric and intensity values of the region
00089   float Xo() const; //!< The mean X value of the region
00090   float Yo() const; //!< The mean Y value of the region
00091   float Io() const; //!< The mean intensity value of the region
00092   float Io_sd() const; //!< The intensity standard deviation value of the region
00093   float ComputeIntensityStdev(); //!< Compute the intensity stdev for the region
00094 
00095   // Scatter Matrix Values
00096   double X2() const; //!< The second order X moment of the region
00097   double Y2() const; //!< The second order Y moment of the region
00098   double XY() const; //!< The second order X,Y moment of the region
00099   double I2() const; //!< The second order intensity moment of the region
00100   double XI() const; //!< The second order X,intensity moment of the region
00101   double YI() const; //!< The second order Y,intensity moment of the region
00102   // Quantities computable from the region scatter matrix
00103   float Diameter() const;
00104   float AspectRatio() const;
00105 
00106   // distinguish from vtol_face::area()
00107   virtual double area() const { return npts_*pixel_size_*pixel_size_; }
00108   
00109   //: The centroid of the pointset 
00110   virtual vsol_point_2d_sptr centroid() const
00111     {return new vsol_point_2d(this->Xo(), this->Yo());}
00112   
00113   //: transform this region using the given 3x3 projective transformation matrix
00114   bool transform(vnl_float_3x3 const& t);
00115 
00116   //: Compute the intensity histogram
00117   //  The intensity ranges from get_min() to get_max().
00118   vcl_vector<unsigned int> histogram(int nbins);
00119   //: Compute the residual intensity histogram
00120   //  The intensity range is returned as the last two arguments.
00121   vcl_vector<unsigned int> residual_histogram(int nbins, float* min=0, float* max=0);
00122 
00123   //: Return true if this region is convex
00124   virtual bool is_convex() const { return false; } // virtual of vsol_region_2d
00125 
00126   void PrincipalOrientation(vnl_float_2& major_axis);
00127 
00128   //Get Fitted Plane Coefficients
00129   double Ix() const;  //!< First derivative of intensity wrt x
00130   double Iy() const;  //!< First derivative of intensity wrt y
00131   double Var() const {return sigma_sq_;} //!< The plane fitting error.
00132   float Ir() const;   //!< The pixel intensity with the plane subtracted
00133 
00134   // Utility Methods
00135   void DoPlaneFit() const; //!< Fit a plane to the region intensities
00136   void PrintFit() const;
00137 
00138   virtual vsol_spatial_object_2d* clone() const;
00139 
00140   //: Return a platform independent string identifying the class
00141   vcl_string is_a() const { return vcl_string("vdgl_digital_region"); }
00142 
00143  protected:
00144   // Members
00145   unsigned int npts_;       //!< Number of pixels in the region
00146   float pixel_size_;        //!< Image pixel size in fractions of a pixel
00147   float *xp_, *yp_;         //!< The location of each pixel
00148   unsigned short *pix_;     //!< The pixel intensities
00149   float max_, min_;         //!< Upper and lower bounds
00150   float xo_, yo_, io_;      //!< Mean Values
00151   float io_stdev_;          //!< Intensity standard deviation for region
00152   mutable int pix_index_;   //!< Index in pixel array (iterator)
00153   void ComputeScatterMatrix() const; // mutable
00154   void IncrByXYI(double x, double y, int intens) const; // mutable
00155   double ComputeSampleResidual() const; // mutable
00156   //members
00157   mutable bool fit_valid_;           //!< Has a plane fit been done?
00158   mutable bool scatter_matrix_valid_;//!< Is the scatter matrix current?
00159   mutable double Ix_;                //!< dI/dx
00160   mutable double Iy_;                //!< dI/dy
00161   //: The sums of various monomials to form the scatter matrix
00162   mutable double X2_,Y2_,I2_,XY_,XI_,YI_;
00163   mutable double error_, sigma_sq_;  //!< fitting errors
00164   mutable vnl_double_3x3 Si_;        //!< scatter matrix
00165 };
00166 
00167 #endif // vdgl_digital_region_h_