![]() |
TerraLib 4.1
|
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 TEPDIPARASEGREGGROWSTRATEGY_HPP 00026 #define TEPDIPARASEGREGGROWSTRATEGY_HPP 00027 00028 #include "TePDIParaSegStrategy.hpp" 00029 #include "TePDIParaSegSegment.hpp" 00030 #include "TePDIRegGrowSeg.hpp" 00031 00032 #include <vector> 00033 00048 class PDI_DLL TePDIParaSegRegGrowStrategy : public TePDIParaSegStrategy 00049 { 00050 public : 00051 00058 class PDI_DLL Segment : public TePDIParaSegSegment 00059 { 00060 public : 00061 00064 std::vector< double > bandsMeansVec_; 00065 00067 double pixelsNumber_; 00068 00071 bool wasMerged_; 00072 00073 Segment(); 00074 00075 ~Segment(); 00076 }; 00077 00078 // This method has the same parameters as the 00079 // TePDIParaSegStrategy constructor 00080 TePDIParaSegRegGrowStrategy( const TePDIParaSegStrategyParams& params ); 00081 00082 ~TePDIParaSegRegGrowStrategy(); 00083 00084 // overloaded 00085 bool execute( const RasterDataVecT& rasterDataVector, 00086 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT& outSegsMatrix ); 00087 00088 //overloaded 00089 bool setParameters( const TePDIParameters& params ); 00090 00091 //overloaded 00092 bool mergeSegments( 00093 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT& centerMatrix, 00094 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT* topMatrixPtr, 00095 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT* leftMatrixPtr ); 00096 00097 // just as the mergeSegments above 00098 static bool staticMergeSegments( 00099 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT& centerMatrix, 00100 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT* topMatrixPtr, 00101 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT* leftMatrixPtr, 00102 double eucTreshold ); 00103 00115 template< class StrategyTemplateT > 00116 static bool createSegsBlkFromLImg( const RasterDataVecT& rasterDataVector, 00117 TeRaster& labelRaster, StrategyTemplateT* stratPtr, 00118 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT& outSegsMatrix ); 00119 00120 //overloaded 00121 double getMemUsageFactor() const { return 1.0; }; 00122 00123 protected : 00124 00130 class PDI_DLL MergingSegmentInfo 00131 { 00132 public: 00133 00135 TePDIParaSegSegment const* segPtr_; 00136 00138 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT const* mtxPtr_; 00139 00141 unsigned int jointBorderLenght_; 00142 00143 MergingSegmentInfo(); 00144 00145 ~MergingSegmentInfo(); 00146 00147 }; 00148 00153 typedef std::vector< MergingSegmentInfo > MergingSegmentInfoVecT; 00154 00156 TePDITypes::TePDIRasterPtrType inputRasterPtr_; 00157 00159 TePDITypes::TePDIRasterPtrType outputRasterPtr_; 00160 00162 TePDIParameters segParams_; 00163 00165 TePDIRegGrowSeg segmenter_; 00166 00168 double eucTreshold_; 00169 00185 static bool staticLocateMergingSegments( Segment const* inputSegPtr, 00186 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT const* inputSegMtxPtr, 00187 const MergingSegmentInfoVecT& candidateSegmentsVec, 00188 std::vector< unsigned int >& locatedSegsIndexes, double eucTreshold ); 00189 00190 }; 00191 00192 template< class StrategyTemplateT > 00193 bool TePDIParaSegRegGrowStrategy::createSegsBlkFromLImg( 00194 const RasterDataVecT& rasterDataVector, TeRaster& labelRaster, 00195 StrategyTemplateT* stratPtr, 00196 TePDIParaSegSegmentsBlock::SegmentsPointersMatrixT& outSegsMatrix ) 00197 { 00198 00199 const unsigned int nLines = (unsigned int)labelRaster.params().nlines_; 00200 const unsigned int nCols = (unsigned int)labelRaster.params().ncols_; 00201 const unsigned int nBands = (unsigned int)labelRaster.params().nBands(); 00202 00203 // Initiating the output matrix 00204 00205 TEAGN_TRUE_OR_RETURN( outSegsMatrix.Reset( rasterDataVector[ 0 ].GetLines(), 00206 rasterDataVector[ 0 ].GetColumns() ), 00207 "output segments matrix reset error " ); 00208 00209 unsigned int band = 0; 00210 unsigned int line = 0; 00211 unsigned int col = 0; 00212 TePDIParaSegSegment** segsMtxLinePtr = 0; 00213 TeDecoder& labelRasterDecoder = *(labelRaster.decoder()); 00214 double value = 0; 00215 Segment* segPtr = 0; 00216 Segment* borderSegPtr = 0; 00217 const unsigned int lastLineIdx = nLines - 1; 00218 const unsigned int lastColIdx = nCols - 1; 00219 double oldSegPixelsNmb = 0; 00220 double newBandMeanValue = 0; 00221 std::map< double, Segment* > allSegsMap; 00222 std::map< double, Segment* > borderSegsMap; 00223 bool blockMergingEnabled = stratPtr->isBlockMergingEnabled(); 00224 00225 for( line = 0 ; line < nLines ; ++line ) 00226 { 00227 segsMtxLinePtr = outSegsMatrix[ line ]; 00228 00229 for( col = 0 ; col < nCols ; ++col ) 00230 { 00231 TEAGN_TRUE_OR_THROW( labelRasterDecoder.getElement( col, line, 00232 value, 0 ), "Error writing input raster" ) 00233 00234 // Does this segment already was created ?? 00235 00236 segPtr = allSegsMap[ value ]; 00237 if( segPtr == 0 ) 00238 { 00239 segPtr = new Segment; 00240 segPtr->id_ = stratPtr->acquireNewSegID(); 00241 00242 allSegsMap[ value ] = segPtr; 00243 } 00244 00245 segsMtxLinePtr[ col ] = segPtr; 00246 00247 // Is this a border segment ?? 00248 00249 if( blockMergingEnabled && ( ( line == 0 ) || ( line == lastLineIdx ) || ( col == 0 ) || 00250 ( col == lastColIdx ) ) ) 00251 { 00252 borderSegPtr = borderSegsMap[ value ]; 00253 00254 if( borderSegPtr == 0 ) 00255 { 00256 borderSegsMap[ value ] = segPtr; 00257 borderSegPtr = segPtr; 00258 00259 borderSegPtr->pixelsNumber_ = 1.0; 00260 00261 borderSegPtr->bandsMeansVec_.resize( nBands ); 00262 for( band = 0 ; band < nBands ; ++band ) 00263 { 00264 borderSegPtr->bandsMeansVec_[ band ] = rasterDataVector[ 00265 band ][ line ][ col ]; 00266 } 00267 } 00268 else 00269 { 00270 oldSegPixelsNmb = borderSegPtr->pixelsNumber_; 00271 ++(borderSegPtr->pixelsNumber_); 00272 00273 for( band = 0 ; band < nBands ; ++band ) 00274 { 00275 newBandMeanValue = ( borderSegPtr->bandsMeansVec_[ band ] * 00276 oldSegPixelsNmb ); 00277 newBandMeanValue += rasterDataVector[ band ][ line ][ col ]; 00278 newBandMeanValue /= borderSegPtr->pixelsNumber_; 00279 00280 borderSegPtr->bandsMeansVec_[ band ] = newBandMeanValue; 00281 } 00282 } 00283 } 00284 } 00285 } 00286 00287 return true; 00288 } 00289 00290 #endif