30 #include "../raster/Band.h" 31 #include "../raster/BandProperty.h" 32 #include "../raster/RasterFactory.h" 33 #include "../raster/Grid.h" 34 #include "../datatype/Enums.h" 35 #include "../common/progress/TaskProgress.h" 43 #include <boost/lexical_cast.hpp> 46 #define BAATZ_EL( featPtr ) featPtr[ 0 ] 49 #define BAATZ_CO( featPtr ) featPtr[ 1 ] 52 #define BAATZ_SM( featPtr ) featPtr[ 2 ] 55 #define BAATZ_SU( featPtr, band ) featPtr[ 3 + band ] 58 #define BAATZ_SS( featPtr, bandsNmb, band ) featPtr[ 3 + bandsNmb + band ] 61 #define BAATZ_ST( featPtr, bandsNmb, band ) featPtr[ 3 + ( 2 * bandsNmb ) + band ] 64 #define BAATZ_STSTARTPTR( featPtr, bandsNmb ) ( featPtr + 3 + ( 2 * bandsNmb ) ) 69 segmenterRegionGrowingBaatzStrategyFactoryInstance;
133 throw( te::rp::Exception )
146 "Invalid segmenter strategy parameter m_minSegmentSize" )
150 "Invalid segmenter strategy parameter m_segmentsSimilarityThreshold" )
154 double bandsWeightsSum = 0;
155 unsigned int bandsWeightsIdx = 0 ;
157 for( bandsWeightsIdx = 0 ; bandsWeightsIdx <
162 "Invalid segmenter strategy parameter m_bandsWeights" );
167 "Invalid segmenter strategy parameter m_bandsWeights" );
194 const std::vector< unsigned int >& inputRasterBands,
195 const std::vector< std::complex< double > >& inputRasterNoDataValues,
196 const std::vector< std::complex< double > >& inputRasterBandMinValues,
197 const std::vector< std::complex< double > >& inputRasterBandMaxValues,
199 const unsigned int outputRasterBand,
200 const bool enableProgressInterface )
201 throw( te::rp::Exception )
204 "Instance not initialized" )
216 inputRasterBands.size(),
"Invalid band weights" );
220 std::unique_ptr< SegmenterRegionGrowingBaatzMerger >
227 const unsigned int segmentFeaturesSize = mergerPtr->getSegmentFeaturesSize();
231 block2ProcessInfo.m_height * block2ProcessInfo.m_width ),
232 segmentFeaturesSize ),
"Segments pool initiation error" );
279 block2ProcessInfo.m_width,
281 "Error allocating segments Ids matrix" );
289 block2ProcessInfo, inputRaster, inputRasterBands, inputRasterNoDataValues,
290 inputRasterBandMinValues, inputRasterBandMaxValues,
291 &actSegsListHeadPtr ),
292 "Segments initalization error" );
294 if( actSegsListHeadPtr ==
nullptr )
302 std::unique_ptr< te::common::TaskProgress > progressPtr;
303 if( enableProgressInterface )
307 progressPtr->setMessage(
"Segmentation" );
314 unsigned int totalMergesNumber = 0;
319 if( enableProgressInterface )
321 if( ! progressPtr->isActive() )
325 progressPtr->pulse();
332 rg::mergeSegments< rg::BaatzFeatureType >(
343 minFoundDissimilarity,
344 maxFoundDissimilarity,
346 globalMergeIterationsCounter,
347 &actSegsListHeadPtr );
353 for(
unsigned int segmentsSimIncreaseStep = 1 ; segmentsSimIncreaseStep <=
363 rg::mergeSegments< rg::BaatzFeatureType >(
365 disimilarityThreshold,
374 minFoundDissimilarity,
375 maxFoundDissimilarity,
377 globalMergeIterationsCounter,
378 &actSegsListHeadPtr );
382 if( enableProgressInterface )
384 if( ! progressPtr->isActive() )
388 progressPtr->pulse();
396 rg::mergeSegments< rg::BaatzFeatureType >(
398 std::numeric_limits< rg::BaatzFeatureType >::max(),
407 minFoundDissimilarity,
408 maxFoundDissimilarity,
410 globalMergeIterationsCounter,
411 &actSegsListHeadPtr );
416 if( enableProgressInterface )
418 if( ! progressPtr->isActive() )
422 progressPtr->pulse();
428 unsigned int blkCol = 0;
431 for(
unsigned int blkLine = 0 ; blkLine < block2ProcessInfo.m_height ; ++blkLine )
433 segmentsIdsLinePtr = m_segmentsIdsMatrix[ blkLine ];
435 for( blkCol = 0 ; blkCol < block2ProcessInfo.m_width ; ++blkCol )
437 if( segmentsIdsLinePtr[ blkCol ] )
439 outputRaster.setValue( blkCol + block2ProcessInfo.m_startX, blkLine
440 + block2ProcessInfo.m_startY, segmentsIdsLinePtr[ blkCol ],
451 const unsigned int bandsToProcess,
452 const unsigned int pixelsNumber )
const 457 double featuresSizeBytes = (double)
525 const std::vector< unsigned int >& inputRasterBands,
526 const std::vector< std::complex< double > >& inputRasterNoDataValues,
527 const std::vector< std::complex< double > >& inputRasterBandMinValues,
528 const std::vector< std::complex< double > >& inputRasterBandMaxValues,
531 const unsigned int inputRasterBandsSize = (
unsigned int)
532 inputRasterBands.size();
534 (*actSegsListHeadPtr) =
nullptr;
538 std::vector< rg::BaatzFeatureType > inputRasterBandGains(
539 inputRasterBandMinValues.size() );
542 for(
unsigned int inputRasterBandMinValuesIdx = 0 ; inputRasterBandMinValuesIdx
543 < inputRasterBandMinValues.size() ; ++inputRasterBandMinValuesIdx )
546 ( inputRasterBandMaxValues[ inputRasterBandMinValuesIdx ].real() -
547 inputRasterBandMinValues[ inputRasterBandMinValuesIdx ].real() );
549 if( inputRasterBandGains[ inputRasterBandMinValuesIdx ] != 0.0 )
552 inputRasterBandGains[ inputRasterBandMinValuesIdx ] );
559 unsigned int blkLine = 0;
560 unsigned int blkCol = 0;
563 bool rasterValuesAreValid =
true;
564 unsigned int inputRasterBandsIdx = 0;
566 const std::vector< double > dummyZeroesVector( inputRasterBandsSize, 0 );
568 std::list< SegmenterSegmentsBlock::SegmentIdDataType >
569 unusedLineSegmentIds;
571 std::vector< SegmenterSegmentsBlock::SegmentIdDataType >
573 lineSegmentIds.reserve( block2ProcessInfo.
m_width );
575 std::vector< rg::BaatzFeatureType > rasterValues;
576 std::vector< rg::BaatzFeatureType > rasterSquareValues;
577 rasterValues.resize( inputRasterBandsSize, 0 );
578 rasterSquareValues.resize( inputRasterBandsSize, 0 );
579 std::vector< SegmenterRegionGrowingSegment< rg::BaatzFeatureType >* > usedSegPointers1( block2ProcessInfo.
m_width,
nullptr );
580 std::vector< SegmenterRegionGrowingSegment< rg::BaatzFeatureType >* > usedSegPointers2( block2ProcessInfo.
m_width,
nullptr );
581 std::vector< SegmenterRegionGrowingSegment< rg::BaatzFeatureType >* >* lastLineSegsPtrs = &usedSegPointers1;
582 std::vector< SegmenterRegionGrowingSegment< rg::BaatzFeatureType >* >* currLineSegsPtrs = &usedSegPointers2;
586 unsigned int rasterValuesIdx = 0;
588 for( blkLine = 0 ; blkLine < block2ProcessInfo.
m_height ; ++blkLine )
592 for( blkCol = 0 ; blkCol < block2ProcessInfo.
m_width ; ++blkCol )
604 rasterValuesAreValid =
true;
606 for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
607 inputRasterBandsSize ; ++inputRasterBandsIdx )
611 inputRasterBands[ inputRasterBandsIdx ] );
613 if( value == inputRasterNoDataValues[ inputRasterBandsIdx ] )
615 rasterValuesAreValid =
false;
620 value -= inputRasterBandMinValues[ inputRasterBandsIdx ].real();
621 value *= inputRasterBandGains[ inputRasterBandsIdx ];
623 rasterValues[ inputRasterBandsIdx ] =
625 rasterSquareValues[ inputRasterBandsIdx ] =
632 rasterValuesAreValid =
false;
637 if( rasterValuesAreValid )
640 assert( segmentPtr );
642 for( rasterValuesIdx = 0 ; rasterValuesIdx < inputRasterBandsSize ;
646 rasterValues[ rasterValuesIdx ];
648 rasterSquareValues[ rasterValuesIdx ];
657 currLineSegsPtrs->operator[]( blkCol ) = segmentPtr;
659 segmentPtr->
m_id = lineSegmentIds[ blkCol ];
677 neighborSegmentPtr = lastLineSegsPtrs->operator[]( blkCol );
679 if( neighborSegmentPtr )
689 neighborSegmentPtr = currLineSegsPtrs->operator[]( blkCol - 1 );
691 if( neighborSegmentPtr )
701 if( (*actSegsListHeadPtr) ==
nullptr )
703 (*actSegsListHeadPtr) = segmentPtr;
713 prevActSegPtr = segmentPtr;
718 unusedLineSegmentIds.push_back( lineSegmentIds[ blkCol ] );
719 currLineSegsPtrs->operator[]( blkCol ) =
nullptr;
725 if( ! unusedLineSegmentIds.empty() )
727 segmenterIdsManager.
addFreeIDs( unusedLineSegmentIds );
728 unusedLineSegmentIds.clear();
733 if( lastLineSegsPtrs == ( &usedSegPointers1 ) )
735 lastLineSegsPtrs = &usedSegPointers2;
736 currLineSegsPtrs = &usedSegPointers1;
740 lastLineSegsPtrs = &usedSegPointers1;
741 currLineSegsPtrs = &usedSegPointers2;
Segmenter segments IDs manager.
bool initialize(const SegmenterSegmentsBlock::SegmentIdDataType segsNumber, const unsigned int featuresNumber)
Pool initialization.
unsigned int m_xStart
Segment left X coordinate box over the label image.
~SegmenterRegionGrowingBaatzStrategyFactory()
double DissimilarityTypeT
Type for dissimilarity.
unsigned int getOptimalBlocksOverlapSize() const
Returns a optimal blocks overlap size (number of border pixels overlapped between blocks...
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
bool initializeSegments(SegmenterIdsManager &segmenterIdsManager, const te::rp::SegmenterSegmentsBlock &block2ProcessInfo, const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< std::complex< double > > &inputRasterNoDataValues, const std::vector< std::complex< double > > &inputRasterBandMinValues, const std::vector< std::complex< double > > &inputRasterBandMaxValues, SegmenterRegionGrowingSegment< rg::BaatzFeatureType > **actSegsListHeadPtr)
Initialize the segment objects container and the segment IDs container.
#define BAATZ_SM(featPtr)
AbstractParameters * clone() const
Create a clone copy of this instance.
const Parameters & operator=(const Parameters ¶ms)
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
Base exception class for plugin module.
std::vector< double > m_bandsWeights
The weight given to each band, when applicable (note: the bands weights sum must always be 1) or an e...
unsigned short int IterationCounterType
Iteration counter type.
#define BAATZ_SU(featPtr, band)
void disable()
Disable this segment ( same as m_mergetIteration = std::numeric_limits< SegmenterRegionGrowingSegment...
This class can be used to inform the progress of a task.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
float BaatzFeatureType
Baatz Strategy feature type.
double m_colorWeight
The weight given to the color component, deafult:0.9, valid range: [0,1].
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
bool m_enableSameIterationMerges
If enabled, a merged segment could be merged with another within the same iteration (default:false)...
BlocksMergingMethod getBlocksMergingMethod() const
Return the strategy blocks merging method.
SegmenterRegionGrowingBaatzStrategyFactory()
bool execute(SegmenterIdsManager &segmenterIdsManager, const te::rp::SegmenterSegmentsBlock &block2ProcessInfo, const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< std::complex< double > > &inputRasterNoDataValues, const std::vector< std::complex< double > > &inputRasterBandMinValues, const std::vector< std::complex< double > > &inputRasterBandMaxValues, te::rst::Raster &outputRaster, const unsigned int outputRasterBand, const bool enableProgressInterface)
Executes the segmentation strategy over region delimited by the given block.
Segmenter Strategy Parameters.
Raster segmenter strategy factory base class.
unsigned int SegmentIdDataType
bool shouldComputeMinMaxValues() const
Returns if the min and max pixel values should be computed.
SegmenterRegionGrowingSegment< FeatureDataTypeT > * m_prevActiveSegment
A pointer to the previous active segment.
IterationCounterType m_mergetIteration
The current merge iteration.
std::vector< unsigned int > m_rightCutOffProfile
bool initialize(SegmenterStrategyParameters const *const strategyParams)
Initialize the segmentation strategy.
SegmenterRegionGrowingSegment< FeatureDataTypeT > * m_nextActiveSegment
A pointer to the next active segment.
#define BAATZ_SS(featPtr, bandsNmb, band)
An abstract class for raster data strucutures.
unsigned int getColumnsNumber() const
The number of current matrix columns.
void removeAllNeighborSegmentsPtrs()
Remove all neighbor segments.
SegmenterRegionGrowingBaatzStrategy()
Raster region growing segmenter Baatz strategy.
Raster segmenter strategy base class.
void addNeighborSegmentPtr(SegmenterRegionGrowingSegment< FeatureDataTypeT > *const nSegPtr)
Add a pointer of a neighbor segment (if it is not already there).
SegmenterSegmentsBlock::SegmentIdDataType m_id
Segment ID.
unsigned int m_yStart
Segment upper Y coordinate box over the label image.
#define BAATZ_CO(featPtr)
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
void reset()
Clear all internal allocated resources and go back to the initial not-initialized state...
void clear()
Clear all stored data.
void reset()
Reset (clear) the active instance data.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
SegmenterRegionGrowingBaatzStrategy::Parameters m_parameters
Internal execution parameters.
FeatureType * m_features
A pionter to a fixed size vector of segment features.
SegmenterRegionGrowingSegment< FeatureDataTypeT > * getNextSegment()
Retrive a stored segment.
Baatz based Segments merger.
Abstract parameters base interface.
Segmenter Baatz merger used in region growing process.
A generic template matrix.
unsigned int m_xBound
Segment lower bound X coordinate box over the label image.
unsigned int m_yBound
Segment lower bound Y coordinate box over the label image.
#define BAATZ_ST(featPtr, bandsNmb, band)
unsigned int m_size
Segment area (pixels number).
BlocksMergingMethod
Blocks merging method.
Segmenter segments block description class.
~SegmenterRegionGrowingBaatzStrategy()
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
void addFreeIDs(const std::vector< SegmenterSegmentsBlock::SegmentIdDataType > &ids)
Stores free unique IDs for later use.
bool m_enableLocalMutualBestFitting
If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fu...
double getMemUsageEstimation(const unsigned int bandsToProcess, const unsigned int pixelsNumber) const
Returns a memory estimation (bytes).
std::vector< unsigned int > m_topCutOffProfile
Raster region growing segmenter strategy.
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
#define BAATZ_EL(featPtr)
virtual void reset()
Clear all internal allocated resources and go back to the initial not-initialized state...
unsigned int getLinesNumber() const
The number of current matrix lines.
bool m_isInitialized
true if this instance is initialized.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Raster region growing segmenter strategy factory.
te::rp::SegmenterStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
bool getNewIDs(const unsigned int &idsNumber, std::vector< SegmenterSegmentsBlock::SegmentIdDataType > &ids)
Returns new segment unique IDs.
std::vector< unsigned int > m_leftCutOffProfile
std::vector< unsigned int > m_bottomCutOffProfile
SegmenterRegionGrowingSegmentsPool< rg::BaatzFeatureType > m_segmentsPool
A pool of segments that can be reused on each strategy execution.