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> 48 segmenterRegionGrowingMeanStrategyFactoryInstance;
106 throw( te::rp::Exception )
119 "Invalid segmenter strategy parameter m_minSegmentSize" )
123 "Invalid segmenter strategy parameter m_segmentsSimilarityThreshold" )
149 const std::vector< unsigned int >& inputRasterBands,
150 const std::vector< std::complex< double > >& inputRasterNoDataValues,
151 const std::vector< std::complex< double > >& inputRasterBandMinValues,
152 const std::vector< std::complex< double > >& inputRasterBandMaxValues,
154 const unsigned int outputRasterBand,
155 const bool enableProgressInterface )
156 throw( te::rp::Exception )
159 "Instance not initialized" )
163 std::unique_ptr< SegmenterRegionGrowingMeanMerger >
168 const unsigned int segmentFeaturesSize = mergerPtr->getSegmentFeaturesSize();
172 block2ProcessInfo.m_height * block2ProcessInfo.m_width ),
173 segmentFeaturesSize ),
"Segments pool initiation error" );
220 block2ProcessInfo.m_width,
222 "Error allocating segments Ids matrix" );
230 block2ProcessInfo, inputRaster, inputRasterBands, inputRasterNoDataValues,
231 inputRasterBandMinValues, inputRasterBandMaxValues,
232 &actSegsListHeadPtr ),
233 "Segments initalization error" );
235 if( actSegsListHeadPtr ==
nullptr )
243 std::unique_ptr< te::common::TaskProgress > progressPtr;
244 if( enableProgressInterface )
248 progressPtr->setMessage(
"Segmentation" );
255 unsigned int totalMergesNumber = 0;
260 if( enableProgressInterface )
262 if( ! progressPtr->isActive() )
266 progressPtr->pulse();
273 rg::mergeSegments< rg::MeanFeatureType >(
284 minFoundDissimilarity,
285 maxFoundDissimilarity,
287 globalMergeIterationsCounter,
288 &actSegsListHeadPtr );
294 for(
unsigned int segmentsSimIncreaseStep = 1 ; segmentsSimIncreaseStep <=
304 rg::mergeSegments< rg::MeanFeatureType >(
306 disimilarityThreshold,
315 minFoundDissimilarity,
316 maxFoundDissimilarity,
318 globalMergeIterationsCounter,
319 &actSegsListHeadPtr );
323 if( enableProgressInterface )
325 if( ! progressPtr->isActive() )
329 progressPtr->pulse();
337 rg::mergeSegments< rg::MeanFeatureType >(
339 std::numeric_limits< rg::MeanFeatureType >::max(),
348 minFoundDissimilarity,
349 maxFoundDissimilarity,
351 globalMergeIterationsCounter,
352 &actSegsListHeadPtr );
357 if( enableProgressInterface )
359 if( ! progressPtr->isActive() )
363 progressPtr->pulse();
369 unsigned int blkCol = 0;
372 for(
unsigned int blkLine = 0 ; blkLine < block2ProcessInfo.m_height ; ++blkLine )
374 segmentsIdsLinePtr = m_segmentsIdsMatrix[ blkLine ];
376 for( blkCol = 0 ; blkCol < block2ProcessInfo.m_width ; ++blkCol )
378 if( segmentsIdsLinePtr[ blkCol ] )
380 outputRaster.setValue( blkCol + block2ProcessInfo.m_startX, blkLine
381 + block2ProcessInfo.m_startY, segmentsIdsLinePtr[ blkCol ],
392 const unsigned int bandsToProcess,
393 const unsigned int pixelsNumber )
const 398 double featuresSizeBytes = (double)
454 const std::vector< unsigned int >& inputRasterBands,
455 const std::vector< std::complex< double > >& inputRasterNoDataValues,
456 const std::vector< std::complex< double > >& inputRasterBandMinValues,
457 const std::vector< std::complex< double > >& inputRasterBandMaxValues,
460 const unsigned int inputRasterBandsSize = (
unsigned int)
461 inputRasterBands.size();
463 (*actSegsListHeadPtr) =
nullptr;
467 std::vector< rg::MeanFeatureType > inputRasterBandGains(
468 inputRasterBandMinValues.size() );
471 for(
unsigned int inputRasterBandMinValuesIdx = 0 ; inputRasterBandMinValuesIdx
472 < inputRasterBandMinValues.size() ; ++inputRasterBandMinValuesIdx )
475 ( inputRasterBandMaxValues[ inputRasterBandMinValuesIdx ].real() -
476 inputRasterBandMinValues[ inputRasterBandMinValuesIdx ].real() );
478 if( inputRasterBandGains[ inputRasterBandMinValuesIdx ] != 0.0 )
481 inputRasterBandGains[ inputRasterBandMinValuesIdx ] );
488 unsigned int blkLine = 0;
489 unsigned int blkCol = 0;
492 bool rasterValuesAreValid =
true;
493 unsigned int inputRasterBandsIdx = 0;
495 const std::vector< double > dummyZeroesVector( inputRasterBandsSize, 0 );
497 std::list< SegmenterSegmentsBlock::SegmentIdDataType >
498 unusedLineSegmentIds;
500 std::vector< SegmenterSegmentsBlock::SegmentIdDataType >
502 lineSegmentIds.reserve( block2ProcessInfo.
m_width );
504 std::vector< rg::MeanFeatureType > rasterValues;
505 std::vector< rg::MeanFeatureType > rasterSquareValues;
506 rasterValues.resize( inputRasterBandsSize, 0 );
507 rasterSquareValues.resize( inputRasterBandsSize, 0 );
508 std::vector< SegmenterRegionGrowingSegment< rg::MeanFeatureType >* > usedSegPointers1( block2ProcessInfo.
m_width,
nullptr );
509 std::vector< SegmenterRegionGrowingSegment< rg::MeanFeatureType >* > usedSegPointers2( block2ProcessInfo.
m_width,
nullptr );
510 std::vector< SegmenterRegionGrowingSegment< rg::MeanFeatureType >* >* lastLineSegsPtrs = &usedSegPointers1;
511 std::vector< SegmenterRegionGrowingSegment< rg::MeanFeatureType >* >* currLineSegsPtrs = &usedSegPointers2;
515 unsigned int rasterValuesIdx = 0;
517 for( blkLine = 0 ; blkLine < block2ProcessInfo.
m_height ; ++blkLine )
521 for( blkCol = 0 ; blkCol < block2ProcessInfo.
m_width ; ++blkCol )
533 rasterValuesAreValid =
true;
535 for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
536 inputRasterBandsSize ; ++inputRasterBandsIdx )
540 inputRasterBands[ inputRasterBandsIdx ] );
542 if( value == inputRasterNoDataValues[ inputRasterBandsIdx ] )
544 rasterValuesAreValid =
false;
549 value -= inputRasterBandMinValues[ inputRasterBandsIdx ].real();
550 value *= inputRasterBandGains[ inputRasterBandsIdx ];
552 rasterValues[ inputRasterBandsIdx ] =
554 rasterSquareValues[ inputRasterBandsIdx ] =
561 rasterValuesAreValid =
false;
566 if( rasterValuesAreValid )
569 assert( segmentPtr );
571 for( rasterValuesIdx = 0 ; rasterValuesIdx < inputRasterBandsSize ;
574 segmentPtr->
m_features[ rasterValuesIdx ] = rasterValues[
578 currLineSegsPtrs->operator[]( blkCol ) = segmentPtr;
580 segmentPtr->
m_id = lineSegmentIds[ blkCol ];
598 neighborSegmentPtr = lastLineSegsPtrs->operator[]( blkCol );
600 if( neighborSegmentPtr )
610 neighborSegmentPtr = currLineSegsPtrs->operator[]( blkCol - 1 );
612 if( neighborSegmentPtr )
622 if( (*actSegsListHeadPtr) ==
nullptr )
624 (*actSegsListHeadPtr) = segmentPtr;
634 prevActSegPtr = segmentPtr;
639 unusedLineSegmentIds.push_back( lineSegmentIds[ blkCol ] );
640 currLineSegsPtrs->operator[]( blkCol ) =
nullptr;
646 if( ! unusedLineSegmentIds.empty() )
648 segmenterIdsManager.
addFreeIDs( unusedLineSegmentIds );
649 unusedLineSegmentIds.clear();
654 if( lastLineSegsPtrs == ( &usedSegPointers1 ) )
656 lastLineSegsPtrs = &usedSegPointers2;
657 currLineSegsPtrs = &usedSegPointers1;
661 lastLineSegsPtrs = &usedSegPointers1;
662 currLineSegsPtrs = &usedSegPointers2;
Segmenter segments IDs manager.
unsigned int getOptimalBlocksOverlapSize() const
Returns a optimal blocks overlap size (number of border pixels overlapped between blocks...
bool initialize(const SegmenterSegmentsBlock::SegmentIdDataType segsNumber, const unsigned int featuresNumber)
Pool initialization.
BlocksMergingMethod getBlocksMergingMethod() const
Return the strategy blocks merging method.
unsigned int m_xStart
Segment left X coordinate box over the label image.
double DissimilarityTypeT
Type for dissimilarity.
bool m_isInitialized
true if this instance is initialized.
SegmenterRegionGrowingMeanStrategyFactory()
Base exception class for plugin module.
unsigned short int IterationCounterType
Iteration counter type.
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.
~SegmenterRegionGrowingMeanStrategy()
bool m_enableSameIterationMerges
If enabled, a merged segment could be merged with another within the same iteration (default:false)...
bool shouldComputeMinMaxValues() const
Returns if the min and max pixel values should be computed.
~SegmenterRegionGrowingMeanStrategyFactory()
SegmenterRegionGrowingSegmentsPool< rg::MeanFeatureType > m_segmentsPool
A pool of segments that can be reused on each strategy execution.
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
bool m_enableLocalMutualBestFitting
If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fu...
SegmenterRegionGrowingMeanStrategy::Parameters m_parameters
Internal execution parameters.
Segmenter Strategy Parameters.
Raster segmenter strategy factory base class.
unsigned int SegmentIdDataType
SegmenterRegionGrowingSegment< FeatureDataTypeT > * m_prevActiveSegment
A pointer to the previous active segment.
IterationCounterType m_mergetIteration
The current merge iteration.
std::vector< unsigned int > m_rightCutOffProfile
SegmenterRegionGrowingSegment< FeatureDataTypeT > * m_nextActiveSegment
A pointer to the next active segment.
const Parameters & operator=(const Parameters ¶ms)
An abstract class for raster data strucutures.
unsigned int getColumnsNumber() const
The number of current matrix columns.
double getMemUsageEstimation(const unsigned int bandsToProcess, const unsigned int pixelsNumber) const
Returns a memory estimation (bytes).
void removeAllNeighborSegmentsPtrs()
Remove all neighbor segments.
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.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
bool initialize(SegmenterStrategyParameters const *const strategyParams)
Initialize the segmentation strategy.
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
void clear()
Clear all stored data.
SegmenterRegionGrowingMeanStrategy()
Raster region growing segmenter strategy.
Segmenter mean merger used in region growing process.
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.
AbstractParameters * clone() const
Create a clone copy of this instance.
Raster region growing segmenter Mean strategy.
FeatureType * m_features
A pionter to a fixed size vector of segment features.
SegmenterRegionGrowingSegment< FeatureDataTypeT > * getNextSegment()
Retrive a stored segment.
Abstract parameters base interface.
A generic template matrix.
unsigned int m_xBound
Segment lower bound X coordinate box over the label image.
float MeanFeatureType
Mean Strategy feature type.
unsigned int m_yBound
Segment lower bound Y coordinate box over the label image.
unsigned int m_size
Segment area (pixels number).
te::rp::SegmenterStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
BlocksMergingMethod
Blocks merging method.
Segmenter segments block description class.
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::MeanFeatureType > **actSegsListHeadPtr)
Initialize the segment objects container and the segment IDs container.
Raster region growing segmenter strategy factory.
void addFreeIDs(const std::vector< SegmenterSegmentsBlock::SegmentIdDataType > &ids)
Stores free unique IDs for later use.
std::vector< unsigned int > m_topCutOffProfile
#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...
void reset()
Clear all internal allocated resources and go back to the initial not-initialized state...
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.
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
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.
Mean based Segments merger.
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