TePDIBaatz.hpp

Go to the documentation of this file.
00001 /*
00002 TerraLib - a library for developing GIS applications.
00003 Copyright  2001, 2002, 2003 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular
00017 purpose. The library provided hereunder is on an "as is" basis, and the
00018 authors have no obligation to provide maintenance, support, updates,
00019 enhancements, or modifications.
00020 In no event shall INPE be held liable to any party
00021 for direct, indirect, special, incidental, or consequential damages arising
00022 out of the use of this library and its documentation.
00023 */
00024 
00025 #ifndef TEPDIBAATZ_HPP
00026   #define TEPDIBAATZ_HPP
00027 
00028   // Internal defines
00029   #define MAX_STEPS 20 /* maximum number of segmentation steps */
00030   #define NUM_BANDS 10 /* number of image bands */
00031   #define POSITION_SEARCH 0.00 /* minimum percentage of unused segments for segment selection by pixel position */
00032   #define OUTLINE_COLOR_0 255 /* segments' outline color for output image */
00033   #define OUTLINE_COLOR_1 000
00034   #define OUTLINE_COLOR_2 000
00035 
00036   // TerraLib includes
00037   #include "TePDIAlgorithm.hpp"
00038   #include "TePDITypes.hpp"
00039   #include "TePDIPIManager.hpp"
00040   #include "TePDIUtils.hpp"
00041   #include "../kernel/TeSharedPtr.h"
00042 
00043   /**
00044     * @brief Region Growing Segmentation, Baatz implementation.
00045     * @author Thales Sehn Korting <tkorting@dpi.inpe.br>
00046     * @author Gilson Costa <gilson@ele.puc-rio.br>
00047     * @ingroup TePDISegmentationGroup
00048     *
00049     * @note Reference: Baatz, M.; Schape, A. Multiresolution segmentation: an 
00050     * optimization approach for high quality multi-scale image segmentation. 
00051     * In: XII Angewandte Geographische Informationsverarbeitung, 
00052     * Wichmann-Verlag, Heidelberg, 2000.
00053     *
00054     * @note The general required parameters :
00055     *
00056     * @param input_image (TePDITypes::TePDIRasterPtrType)
00057     * @param input_weights (vector<float>) one weight for each input image channel
00058     * @param input_channels (vector<unsigned>) used channels for input image
00059     * @param scale (float) Parameter scale is > 0 (Has direct influence
00060     * over the generated segments size).
00061     * @param compactness (float) Parameter compactness is > 0 and <= 1 (
00062     compactness = perimeter / sqrt( area ) ).
00063     * @param color (float) Parameter color is > 0 and <= 1 (the relative ratio
00064     * between spacial components and spectral components).
00065     * @param output_image (TePDITypes::TePDIRasterPtrType) - Output raster
00066     * pointer.
00067     *
00068     * @note The optional parameters (will be used only if present):
00069     *
00070     * @param output_polsets (TePDITypes::TePDIPolSetMapPtrType) - 
00071     * A pointer to the output polygon sets map.
00072     *
00073     */
00074 
00075 /**********************************************************************************/
00076 /* TerraLib main class for Baatz Segmentation                                     */
00077 /**********************************************************************************/
00078 
00079   class PDI_DLL TePDIBaatz : public TePDIAlgorithm {
00080     public :
00081       
00082       /**
00083        * @brief Default Constructor.
00084        *
00085        */
00086       TePDIBaatz();
00087 
00088       /**
00089        * @brief Default Destructor
00090        */
00091           ~TePDIBaatz() {};
00092       
00093       /**
00094        * @brief Checks if the supplied parameters fits the requirements of 
00095        * each PDI algorithm implementation.
00096        *
00097        * @note Error log messages must be generated. No exceptions generated.
00098        *
00099        * @param parameters The parameters to be checked.
00100        * @return true if the parameters are OK. false if not.
00101        */
00102       bool CheckParameters( const TePDIParameters& parameters ) const;      
00103 
00104   protected:    
00105       /**
00106        * @brief Reset the internal state to the initial state.
00107        *
00108        * @param params The new parameters referente at initial state.
00109        */
00110       void ResetState( const TePDIParameters& );    
00111      
00112       /**
00113        * @brief Runs the current algorithm implementation.
00114        *
00115        * @return true if OK. false on error.
00116        */
00117       bool RunImplementation();
00118       
00119   };
00120   
00121 /** @example TePDISegmentation_test.cpp
00122  *    Shows how to use this class.
00123  */    
00124 
00125 
00126 /**********************************************************************************/
00127 /* Internal definitions Baatz Segmentation                                        */
00128 /**********************************************************************************/
00129 
00130 /**********************************************************************************/
00131 /* type definitions                                                               */
00132 /**********************************************************************************/
00133 
00134 /* node of list of segments' pixels */
00135 struct segment_pixel 
00136 {
00137   unsigned long int pixel_id; /* pixel id */
00138   bool borderline; /* indicates if the pixel belongs to the border of the segment */
00139   struct segment_pixel *next_pixel; /* next pixel */
00140 };
00141 
00142 /* node of segment list */
00143 struct segment 
00144 {
00145   unsigned long int id; /* segment id */
00146   float area; /* number of pixels in segment */
00147   float perimeter; /*number of border pixels */
00148   float b_box[4]; /* bounding box of the segment, relative to rows and cols */
00149   float avg_color[NUM_BANDS]; /* average colors of pixels, one for each band */
00150   float std_color[NUM_BANDS]; /* std of pixel colors, one for each band */
00151   float avg_color_square[NUM_BANDS];
00152   float color_sum[NUM_BANDS];
00153   int used; /* indicate if segment has been used in segmentation step */
00154   struct segment_pixel *pixel_list; /* list of indexes of the segment's pixels */
00155   struct segment_pixel *last_pixel; /* pointer to the last pixel in the pixel list */
00156   struct segment_pixel *original_pixel; /* pointer to original pixel of the segment */
00157   struct segment *previous_segment; /* previous not deleted segment */
00158   struct segment *next_segment; /* next not deleted segment */
00159   struct segment *previous_unused_segment; /* previous unused segment */
00160   struct segment *next_unused_segment; /* next unused segment */
00161   struct segment *next_original_segment; /* next original segment */
00162 };
00163 
00164 /* node of segments' neighbours */
00165 struct segment_neighbor 
00166 {
00167   float area; /* number of pixels in merged segment, neighbour plus original */
00168   float perimeter; /*number of border pixels in merged segment, neighbour plus original */
00169   float b_box[4]; /* bounding box of the segment of merged segment, neighbour plus original */
00170   float avg_color[NUM_BANDS]; /* average colors of pixels of merged segment, one for each band */
00171   float std_color[NUM_BANDS]; /* std of pixel colors of merged segment, one for each band */
00172   float avg_color_square[NUM_BANDS];
00173   float color_sum[NUM_BANDS];
00174   unsigned long int neighbor_id; /* id of neighbor segment */
00175   struct segment *neighbor; /* neighbor segment */
00176   struct segment_neighbor *next_neighbor; /* next neighbor */
00177 };
00178 
00179 /* record of segmentation parameters */
00180 struct segmentation_parameters 
00181 {
00182   float sp; /* scale parameter */
00183   float wband[NUM_BANDS]; /* weight of image bands, between 0 and 1 */
00184   float wcolor; /* factor of color/shape, between 0 and 1 */
00185   float wcmpt; /* factor of compactness/smothness, between 0 and 1 */
00186   int bands; /* number of bands */
00187 };
00188 
00189 /**********************************************************************************/
00190 /* function prototypes                                                            */
00191 /**********************************************************************************/
00192 
00193 /* returns random number between low and high */
00194 float randval(float low, 
00195         float high); 
00196 
00197 /* read parameter file */
00198 int read_seg_parameters(char *parameter_file_name,
00199             struct segmentation_parameters *parameters); 
00200 
00201 /* creates one segment for each pixel of input image */
00202 int initialize_segments(struct segment ***segments_ptr_vector, 
00203             struct segment **first_segment, 
00204             struct segment **last_segment, 
00205             // float **input_image, 
00206             TePDITypes::TePDIRasterPtrType input_image,
00207             vector<unsigned> input_channels, 
00208             long int nrows, 
00209             long int ncols,
00210             bool progress_enabled_);  
00211 
00212 /* performs the segmentation, returns (num.segments + segmentation steps/100) */
00213 float segmentation(// float **input_image, 
00214            struct segment **segments_ptr_vector, 
00215            struct segment *initial_segment, 
00216            struct segment *final_segment, 
00217            long int nrows, 
00218            long int ncols, 
00219            struct segmentation_parameters parameters,
00220            bool progress_enabled_); 
00221 
00222 /* write output image */
00223 void write_segments(// float **input_image, 
00224           TePDITypes::TePDIRasterPtrType output_image,
00225           struct segment *initial_segment, 
00226           int write_type,
00227           bool progress_enabled_); 
00228 
00229 /* remove segments from memory */
00230 void free_segment_list(struct segment **initial_segment, 
00231              struct segment ***segments_ptr_vector);
00232 
00233 #endif

Generated on Sun Jul 29 04:01:23 2012 for TerraLib - Development Source by  doxygen 1.5.3