29 #include "../raster/Band.h"
30 #include "../raster/BandProperty.h"
31 #include "../raster/RasterFactory.h"
32 #include "../raster/Grid.h"
33 #include "../datatype/Enums.h"
34 #include "../common/progress/TaskProgress.h"
42 #include <boost/lexical_cast.hpp>
45 #define BAATZ_EL( featPtr ) featPtr[ 0 ]
48 #define BAATZ_CO( featPtr ) featPtr[ 1 ]
51 #define BAATZ_SM( featPtr ) featPtr[ 2 ]
54 #define BAATZ_SU( featPtr, band ) featPtr[ 3 + band ]
57 #define BAATZ_SS( featPtr, bandsNmb, band ) featPtr[ 3 + bandsNmb + band ]
60 #define BAATZ_ST( featPtr, bandsNmb, band ) featPtr[ 3 + ( 2 * bandsNmb ) + band ]
63 #define BAATZ_STSTARTPTR( featPtr, bandsNmb ) ( featPtr + 3 + ( 2 * bandsNmb ) )
68 segmenterRegionGrowingStrategyFactoryInstance;
106 throw( te::rp::Exception )
108 m_minSegmentSize = 100;
109 m_segmentsSimilarityThreshold = 0.03;
110 m_segmentFeatures = InvalidFeaturesType;
111 m_bandsWeights.clear();
113 m_compactnessWeight = 0.5;
114 m_segmentsSimIncreaseSteps = 2;
115 m_enableLocalMutualBestFitting =
false;
116 m_enableSameIterationMerges =
false;
138 m_featuresNumber = featuresNumber;
140 2.0 * std::sqrt( (
double)featuresNumber ) );
152 assert( segment1Ptr );
154 assert( segment2Ptr );
157 m_getDissimilarity_dissValue = 0.0;
159 for( m_getDissimilarity_meansIdx = 0 ; m_getDissimilarity_meansIdx < m_featuresNumber ;
160 ++m_getDissimilarity_meansIdx )
162 m_getDissimilarity_diffValue = segment1Ptr->
m_features[ m_getDissimilarity_meansIdx ] -
163 segment2Ptr->
m_features[ m_getDissimilarity_meansIdx ];
165 m_getDissimilarity_dissValue += ( m_getDissimilarity_diffValue * m_getDissimilarity_diffValue );
168 m_getDissimilarity_dissValue = std::sqrt( m_getDissimilarity_dissValue );
169 m_getDissimilarity_dissValue /= m_dissimilarityNormFactor;
171 return m_getDissimilarity_dissValue;
179 assert( segment1Ptr );
181 assert( segment2Ptr );
204 for(
unsigned int meansIdx = 0 ; meansIdx < m_featuresNumber ; ++meansIdx )
235 const double& colorWeight,
const double& compactnessWeight,
236 const std::vector< double >& bandsWeights,
239 m_segmentsIds( segmentsIds ),
240 m_allSegsCompactnessOffset( 0 ),
241 m_allSegsCompactnessGain( 1.0 ),
242 m_allSegsSmoothnessOffset( 0 ),
243 m_allSegsSmoothnessGain( 0 ),
254 bandsWeights[ band ];
267 assert( segment1Ptr );
269 assert( segment2Ptr );
271 assert( mergePreviewSegPtr );
275 m_getDissimilarity_sizeSeg1D =
278 m_getDissimilarity_sizeSeg2D =
281 m_getDissimilarity_sizeUnionD = m_getDissimilarity_sizeSeg1D + m_getDissimilarity_sizeSeg2D;
283 mergePreviewSegPtr->
m_size = (
unsigned int)m_getDissimilarity_sizeUnionD;
299 if( m_colorWeight != 1.0 )
302 m_segmentsIds, mergePreviewSegPtr->
m_xStart,
308 m_getDissimilarity_touchingEdgeLength1,
309 m_getDissimilarity_touchingEdgeLength2 );
318 std::sqrt( m_getDissimilarity_sizeUnionD ) );
333 m_getDissimilarity_hCompact =
339 m_allSegsCompactnessOffset
342 m_allSegsCompactnessGain
352 m_allSegsCompactnessOffset
355 m_allSegsCompactnessGain
358 m_getDissimilarity_sizeSeg1D
366 m_allSegsCompactnessOffset
369 m_allSegsCompactnessGain
372 m_getDissimilarity_sizeSeg2D
376 m_getDissimilarity_sizeUnionD
380 m_getDissimilarity_hSmooth =
386 m_allSegsSmoothnessOffset
389 m_allSegsSmoothnessGain
399 m_allSegsSmoothnessOffset
402 m_allSegsSmoothnessGain
405 m_getDissimilarity_sizeSeg1D
413 m_allSegsSmoothnessOffset
416 m_allSegsSmoothnessGain
419 m_getDissimilarity_sizeSeg2D
423 m_getDissimilarity_sizeUnionD
427 m_getDissimilarity_hForm =
432 m_getDissimilarity_hCompact
436 ( 1.0f - m_compactnessWeight )
438 m_getDissimilarity_hSmooth
445 if( m_colorWeight != 0.0 )
447 m_getDissimilarity_hColor = 0;
449 for( m_getDissimilarity_sumsIdx = 0 ; m_getDissimilarity_sumsIdx < m_bandsNumber ; ++m_getDissimilarity_sumsIdx )
451 m_getDissimilarity_sumUnion =
456 m_getDissimilarity_sumUnion;
458 m_getDissimilarity_squaresSumUnion =
462 BAATZ_SS( mergePreviewSegPtr->
m_features, m_bandsNumber, m_getDissimilarity_sumsIdx ) = m_getDissimilarity_squaresSumUnion;
464 m_getDissimilarity_meanUnion = m_getDissimilarity_sumUnion / m_getDissimilarity_sizeUnionD;
466 m_getDissimilarity_stdDevUnion =
473 m_getDissimilarity_squaresSumUnion
480 m_getDissimilarity_sizeUnionD * m_getDissimilarity_meanUnion * m_getDissimilarity_meanUnion
484 m_getDissimilarity_sizeUnionD
488 BAATZ_ST( mergePreviewSegPtr->
m_features, m_bandsNumber, m_getDissimilarity_sumsIdx ) =
489 m_getDissimilarity_stdDevUnion;
491 m_getDissimilarity_hColor +=
493 m_bandsWeights[ m_getDissimilarity_sumsIdx ]
498 m_getDissimilarity_stdDevUnion
500 m_allSegsStdDevOffset
512 m_allSegsStdDevOffset
522 m_allSegsStdDevOffset
529 m_getDissimilarity_sizeUnionD
536 m_getDissimilarity_hColor =
539 m_getDissimilarity_hColor
545 ( 1.0f - m_colorWeight )
547 m_getDissimilarity_hForm
559 assert( segment1Ptr );
561 assert( segment2Ptr );
563 assert( mergePreviewSegPtr );
582 m_update_compactnessMin =
583 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
584 m_update_compactnessMax = -1.0f *
585 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
587 m_update_smoothnessMin =
588 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
589 m_update_smoothnessMax = -1.0f *
590 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
593 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
594 m_update_stdDevMax = -1.0f *
595 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
597 m_update_currentActSegPtr = actSegsListHeadPtr;
599 while( m_update_currentActSegPtr )
601 m_update_featuresPtr = m_update_currentActSegPtr->
m_features;
603 if( m_colorWeight != 1.0 )
605 if( m_update_compactnessMin >
BAATZ_CO( m_update_featuresPtr ) )
607 m_update_compactnessMin =
BAATZ_CO( m_update_featuresPtr );
609 if( m_update_compactnessMax <
BAATZ_CO( m_update_featuresPtr ) )
611 m_update_compactnessMax =
BAATZ_CO( m_update_featuresPtr );
614 if( m_update_smoothnessMin >
BAATZ_SM( m_update_featuresPtr ) )
616 m_update_smoothnessMin =
BAATZ_SM( m_update_featuresPtr );
618 if( m_update_smoothnessMax <
BAATZ_SM( m_update_featuresPtr ) )
620 m_update_smoothnessMax =
BAATZ_SM( m_update_featuresPtr );
624 if( m_colorWeight != 0.0 )
626 m_update_stdDevPtr =
BAATZ_STSTARTPTR( m_update_featuresPtr, m_bandsNumber );
628 for( m_update_band = 0 ; m_update_band < m_bandsNumber ; ++m_update_band, ++m_update_stdDevPtr )
630 if( m_update_stdDevMin > *m_update_stdDevPtr )
632 m_update_stdDevMin = *m_update_stdDevPtr;
634 if( m_update_stdDevMax < *m_update_stdDevPtr )
636 m_update_stdDevMax = *m_update_stdDevPtr;
641 m_update_currentActSegPtr = m_update_currentActSegPtr->m_nextActiveSegment;
644 if( m_update_compactnessMax == m_update_compactnessMin )
646 m_allSegsCompactnessOffset = 0.0;
648 if( m_update_compactnessMax == 0.0 )
649 m_allSegsCompactnessGain = 1.0;
651 m_allSegsCompactnessGain = 1.0f / m_update_compactnessMax;
655 m_allSegsCompactnessOffset = -1.0f * m_update_compactnessMin;
656 m_allSegsCompactnessGain = 1.0f / ( m_update_compactnessMax - m_update_compactnessMin );
659 if( m_update_smoothnessMax == m_update_smoothnessMin )
661 m_allSegsSmoothnessOffset = 0.0;
663 if( m_update_smoothnessMax == 0.0 )
664 m_allSegsSmoothnessGain = 1.0;
666 m_allSegsSmoothnessGain = 1.0f / m_update_smoothnessMax;
670 m_allSegsSmoothnessOffset = -1.0f * m_update_smoothnessMin;
671 m_allSegsSmoothnessGain = 1.0f / ( m_update_smoothnessMax - m_update_smoothnessMin );
674 if( m_update_stdDevMax == m_update_stdDevMin )
676 m_allSegsStdDevOffset = 0.0;
678 if( m_update_stdDevMax == 0.0 )
679 m_allSegsStdDevGain = 1.0;
681 m_allSegsStdDevGain = 1.0f / m_update_stdDevMax;
685 m_allSegsStdDevOffset = -1.0f * m_update_stdDevMin;
686 m_allSegsStdDevGain = 1.0f / ( m_update_stdDevMax - m_update_stdDevMin );
704 throw( te::rp::Exception )
717 "Invalid segmenter strategy parameter m_minSegmentSize" )
721 "Invalid segmenter strategy parameter m_segmentsSimilarityThreshold" )
725 "Invalid segmenter strategy parameter m_segmentFeatures" )
730 "Invalid segmenter strategy parameter m_bandsWeights" );
732 double bandsWeightsSum = 0;
733 unsigned int bandsWeightsIdx = 0 ;
734 for( bandsWeightsIdx = 0 ; bandsWeightsIdx <
739 "Invalid segmenter strategy parameter m_bandsWeights" );
743 "Invalid segmenter strategy parameter m_bandsWeights" );
744 for( bandsWeightsIdx = 0 ; bandsWeightsIdx <
774 const std::vector< unsigned int >& inputRasterBands,
775 const std::vector< double >& inputRasterNoDataValues,
776 const std::vector< double >& inputRasterBandMinValues,
777 const std::vector< double >& inputRasterBandMaxValues,
779 const unsigned int outputRasterBand,
780 const bool enableProgressInterface )
781 throw( te::rp::Exception )
784 "Instance not initialized" )
790 ((double)inputRasterBands.size()) );
794 std::auto_ptr< Merger > mergerPtr;
800 mergerPtr.reset(
new MeanMerger( inputRasterBands.size() ) );
819 const unsigned int segmentFeaturesSize = mergerPtr->getSegmentFeaturesSize();
823 block2ProcessInfo.m_height * block2ProcessInfo.m_width ),
824 segmentFeaturesSize ),
"Segments pool initiation error" );
865 block2ProcessInfo.m_width,
867 "Error allocating segments Ids matrix" );
875 block2ProcessInfo, inputRaster, inputRasterBands, inputRasterNoDataValues,
876 inputRasterBandMinValues, inputRasterBandMaxValues,
877 &actSegsListHeadPtr ),
878 "Segments initalization error" );
881 "Invalid active segments list header" );
885 std::auto_ptr< te::common::TaskProgress > progressPtr;
886 if( enableProgressInterface )
890 progressPtr->setMessage(
"Segmentation" );
897 unsigned int totalMergesNumber = 0;
902 if( enableProgressInterface )
904 if( ! progressPtr->isActive() )
908 progressPtr->pulse();
925 minFoundDissimilarity,
926 maxFoundDissimilarity,
928 globalMergeIterationsCounter,
929 &actSegsListHeadPtr );
935 for(
unsigned int segmentsSimIncreaseStep = 1 ; segmentsSimIncreaseStep <=
946 disimilarityThreshold,
955 minFoundDissimilarity,
956 maxFoundDissimilarity,
958 globalMergeIterationsCounter,
959 &actSegsListHeadPtr );
963 if( enableProgressInterface )
965 if( ! progressPtr->isActive() )
969 progressPtr->pulse();
978 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max(),
987 minFoundDissimilarity,
988 maxFoundDissimilarity,
990 globalMergeIterationsCounter,
991 &actSegsListHeadPtr );
996 if( enableProgressInterface )
998 if( ! progressPtr->isActive() )
1002 progressPtr->pulse();
1008 unsigned int blkCol = 0;
1011 for(
unsigned int blkLine = 0 ; blkLine < block2ProcessInfo.m_height ; ++blkLine )
1015 for( blkCol = 0 ; blkCol < block2ProcessInfo.m_width ; ++blkCol )
1017 if( segmentsIdsLinePtr[ blkCol ] )
1019 outputRaster.setValue( blkCol + block2ProcessInfo.m_startX, blkLine
1020 + block2ProcessInfo.m_startY, segmentsIdsLinePtr[ blkCol ],
1031 const unsigned int bandsToProcess,
1032 const unsigned int pixelsNumber )
const
1037 double featuresSizeBytes = 0.0;
1042 featuresSizeBytes = (double)
1054 featuresSizeBytes = (double)
1119 const std::vector< unsigned int >& inputRasterBands,
1120 const std::vector< double >& inputRasterNoDataValues,
1121 const std::vector< double >& inputRasterBandMinValues,
1122 const std::vector< double >& inputRasterBandMaxValues,
1125 const unsigned int inputRasterBandsSize = (
unsigned int)
1126 inputRasterBands.size();
1128 (*actSegsListHeadPtr) = 0;
1132 std::vector< SegmenterRegionGrowingSegment::FeatureType > inputRasterBandGains(
1133 inputRasterBandMinValues.size() );
1136 for(
unsigned int inputRasterBandMinValuesIdx = 0 ; inputRasterBandMinValuesIdx
1137 < inputRasterBandMinValues.size() ; ++inputRasterBandMinValuesIdx )
1139 inputRasterBandGains[ inputRasterBandMinValuesIdx ] =
1140 inputRasterBandMaxValues[ inputRasterBandMinValuesIdx ] -
1141 inputRasterBandMinValues[ inputRasterBandMinValuesIdx ];
1143 if( inputRasterBandGains[ inputRasterBandMinValuesIdx ] != 0.0 )
1145 inputRasterBandGains[ inputRasterBandMinValuesIdx ] = 1.0 /
1146 inputRasterBandGains[ inputRasterBandMinValuesIdx ];
1153 unsigned int blkLine = 0;
1154 unsigned int blkCol = 0;
1157 bool rasterValuesAreValid =
true;
1158 unsigned int inputRasterBandsIdx = 0;
1160 const std::vector< double > dummyZeroesVector( inputRasterBandsSize, 0 );
1162 std::list< SegmenterSegmentsBlock::SegmentIdDataType >
1163 unusedLineSegmentIds;
1165 std::vector< SegmenterSegmentsBlock::SegmentIdDataType >
1167 lineSegmentIds.reserve( block2ProcessInfo.
m_width );
1169 std::vector< SegmenterRegionGrowingSegment::FeatureType > rasterValues;
1170 std::vector< SegmenterRegionGrowingSegment::FeatureType > rasterSquareValues;
1171 rasterValues.resize( inputRasterBandsSize, 0 );
1172 rasterSquareValues.resize( inputRasterBandsSize, 0 );
1173 std::vector< SegmenterRegionGrowingSegment* > usedSegPointers1( block2ProcessInfo.
m_width, 0 );
1174 std::vector< SegmenterRegionGrowingSegment* > usedSegPointers2( block2ProcessInfo.
m_width, 0 );
1175 std::vector< SegmenterRegionGrowingSegment* >* lastLineSegsPtrs = &usedSegPointers1;
1176 std::vector< SegmenterRegionGrowingSegment* >* currLineSegsPtrs = &usedSegPointers2;
1180 unsigned int rasterValuesIdx = 0;
1182 for( blkLine = 0 ; blkLine < block2ProcessInfo.
m_height ; ++blkLine )
1184 segmenterIdsManager.
getNewIDs( block2ProcessInfo.
m_width, lineSegmentIds );
1186 for( blkCol = 0 ; blkCol < block2ProcessInfo.
m_width ; ++blkCol )
1198 rasterValuesAreValid =
true;
1200 for( inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
1201 inputRasterBandsSize ; ++inputRasterBandsIdx )
1205 inputRasterBands[ inputRasterBandsIdx ] );
1207 if( value == inputRasterNoDataValues[ inputRasterBandsIdx ] )
1209 rasterValuesAreValid =
false;
1214 value -= inputRasterBandMinValues[ inputRasterBandsIdx ];
1215 value *= inputRasterBandGains[ inputRasterBandsIdx ];
1217 rasterValues[ inputRasterBandsIdx ] =
1219 rasterSquareValues[ inputRasterBandsIdx ] =
1226 rasterValuesAreValid =
false;
1231 if( rasterValuesAreValid )
1238 assert( segmentPtr );
1240 for( rasterValuesIdx = 0 ; rasterValuesIdx < inputRasterBandsSize ;
1243 segmentPtr->
m_features[ rasterValuesIdx ] = rasterValues[
1252 assert( segmentPtr );
1254 for( rasterValuesIdx = 0 ; rasterValuesIdx < inputRasterBandsSize ;
1258 rasterValues[ rasterValuesIdx ];
1260 rasterSquareValues[ rasterValuesIdx ];
1278 currLineSegsPtrs->operator[]( blkCol ) = segmentPtr;
1280 segmentPtr->
m_id = lineSegmentIds[ blkCol ];
1285 segmentPtr->
m_yBound = blkLine + 1;
1298 neighborSegmentPtr = lastLineSegsPtrs->operator[]( blkCol );
1300 if( neighborSegmentPtr )
1310 neighborSegmentPtr = currLineSegsPtrs->operator[]( blkCol - 1 );
1312 if( neighborSegmentPtr )
1322 if( (*actSegsListHeadPtr) == 0 )
1324 (*actSegsListHeadPtr) = segmentPtr;
1334 prevActSegPtr = segmentPtr;
1339 unusedLineSegmentIds.push_back( lineSegmentIds[ blkCol ] );
1340 currLineSegsPtrs->operator[]( blkCol ) = 0;
1346 if( ! unusedLineSegmentIds.empty() )
1348 segmenterIdsManager.
addFreeIDs( unusedLineSegmentIds );
1349 unusedLineSegmentIds.clear();
1354 if( lastLineSegsPtrs == ( &usedSegPointers1 ) )
1356 lastLineSegsPtrs = &usedSegPointers2;
1357 currLineSegsPtrs = &usedSegPointers1;
1361 lastLineSegsPtrs = &usedSegPointers1;
1362 currLineSegsPtrs = &usedSegPointers2;
1371 const unsigned int maxSegSizeThreshold,
1374 const bool enablelocalMutualBestFitting,
1375 const bool enableSameIterationMerges,
1381 unsigned int& totalMergesNumber,
1385 TERP_TRUE_OR_THROW( actSegsListHeadPtrPtr != 0,
"Invalid active segments list header pointer" );
1386 TERP_TRUE_OR_THROW( (*actSegsListHeadPtrPtr) != 0,
"Invalid active segments list header pointer" );
1388 minFoundDissimilarity =
1389 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
1390 maxFoundDissimilarity = -1.0 * minFoundDissimilarity;
1391 totalMergesNumber = 0;
1396 disimilarityThreshold;
1397 unsigned int internalMaxSegSizeThreshold = maxSegSizeThreshold;
1398 if( maxSegSizeThreshold )
1400 internalDisimilarityThreshold = std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
1404 internalMaxSegSizeThreshold = std::numeric_limits< unsigned int >::max();
1406 unsigned int iterationMergedSegmentsNumber = 0;
1407 unsigned int segmentsLine = 0;
1408 unsigned int segmentsLineBound = 0;
1409 unsigned int segmentCol = 0;
1410 unsigned int segmentColStart = 0;
1411 unsigned int segmentColBound = 0;
1420 std::list< SegmenterSegmentsBlock::SegmentIdDataType > freeSegmentIds;
1421 unsigned int neighborSegIdx = 0;
1428 iterationMergedSegmentsNumber = 0;
1429 currActSegPtr = (*actSegsListHeadPtrPtr);
1433 merger.
update( *actSegsListHeadPtrPtr );
1441 enableSameIterationMerges
1446 ( currActSegPtr->
m_size <= internalMaxSegSizeThreshold )
1452 minForwardDissimilaritySegmentPtr = 0;
1453 minForwardDissimilarityValue =
1454 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
1465 ( forwardDissimilarityValue <= internalDisimilarityThreshold )
1467 ( forwardDissimilarityValue < minForwardDissimilarityValue )
1470 minForwardDissimilarityValue = forwardDissimilarityValue;
1471 minForwardDissimilaritySegmentPtr = currActSegPtr->
m_neighborSegments[ neighborSegIdx ];
1472 auxSeg3Ptr->operator=( *auxSeg1Ptr );
1479 if( enablelocalMutualBestFitting && ( minForwardDissimilaritySegmentPtr != 0 ) )
1483 minBackwardDissimilaritySegmentPtr = 0;
1484 backwardDissimilarityValue = 0;
1485 minBackwardDissimilarityValue =
1486 std::numeric_limits< SegmenterRegionGrowingSegment::FeatureType >::max();
1493 backwardDissimilarityValue =
1495 minForwardDissimilaritySegmentPtr->
m_neighborSegments[ neighborSegIdx ], auxSeg2Ptr );
1497 if( backwardDissimilarityValue < minBackwardDissimilarityValue )
1499 minBackwardDissimilarityValue = backwardDissimilarityValue;
1500 minBackwardDissimilaritySegmentPtr =
1506 if( minBackwardDissimilaritySegmentPtr != currActSegPtr )
1508 minForwardDissimilaritySegmentPtr = 0;
1515 ( minForwardDissimilaritySegmentPtr != 0 )
1518 enableSameIterationMerges
1523 globalMergeIterationsCounter
1528 if( minFoundDissimilarity > minForwardDissimilarityValue )
1530 minFoundDissimilarity = minForwardDissimilarityValue;
1532 if( maxFoundDissimilarity < minForwardDissimilarityValue )
1534 maxFoundDissimilarity = minForwardDissimilarityValue;
1541 merger.
mergeFeatures( currActSegPtr, minForwardDissimilaritySegmentPtr,
1557 ( minForwardDissimilaritySegmentPtr->
m_neighborSegments[ neighborSegIdx ] != currActSegPtr )
1576 minForwardDissimilaritySegmentPtr );
1582 segmentsLineBound = minForwardDissimilaritySegmentPtr->
m_yBound;
1583 segmentColStart = minForwardDissimilaritySegmentPtr->
m_xStart;
1584 segmentColBound = minForwardDissimilaritySegmentPtr->
m_xBound;
1585 currentSegmentId = currActSegPtr->
m_id;
1587 for( segmentsLine = minForwardDissimilaritySegmentPtr->
m_yStart ;
1588 segmentsLine < segmentsLineBound ; ++segmentsLine )
1592 for( segmentCol = segmentColStart ; segmentCol <
1593 segmentColBound ; ++segmentCol )
1595 if( segmentsIdsLinePtr[ segmentCol ] ==
1596 minForwardDissimilaritySegmentPtr->
m_id )
1598 segmentsIdsLinePtr[ segmentCol ] = currentSegmentId;
1606 if( minForwardDissimilaritySegmentPtr == (*actSegsListHeadPtrPtr) )
1608 (*actSegsListHeadPtrPtr) = (*actSegsListHeadPtrPtr)->m_nextActiveSegment;
1623 minForwardDissimilaritySegmentPtr->
disable();
1627 freeSegmentIds.push_back( minForwardDissimilaritySegmentPtr->
m_id );
1629 ++iterationMergedSegmentsNumber;
1637 }
while( currActSegPtr );
1641 if( ! freeSegmentIds.empty() )
1643 segmenterIdsManager.
addFreeIDs( freeSegmentIds );
1644 freeSegmentIds.clear();
1647 totalMergesNumber += iterationMergedSegmentsNumber;
1652 ++globalMergeIterationsCounter;
1654 while( iterationMergedSegmentsNumber && ( globalMergeIterationsCounter <
1655 std::numeric_limits< SegmenterRegionGrowingSegment::IterationCounterType>::max() ) );
1660 const std::string& fileName )
1662 std::map<std::string, std::string> rinfo;
1663 rinfo[
"SOURCE"] = fileName;
1670 std::vector< te::rst::BandProperty* > bandsProps;
1675 gridPtr, bandsProps, rinfo );
1678 unsigned int col = 0;
1679 unsigned int line = 0 ;
1681 double offset = 0.0;
1686 double minValue = DBL_MAX;
1687 double maxValue = -1.0 * DBL_MAX;
1690 for( line = 0 ; line < linesNmb ; ++line )
1692 for( col = 0 ; col < colsNmb ; ++col )
1694 value = (double)segmentsIds( line, col );
1696 if( value > maxValue ) maxValue = value;
1697 if( value < minValue ) minValue = value;
1702 scale = 254.0 / ( maxValue - minValue );
1707 for( line = 0 ; line < linesNmb ; ++line )
1709 for( col = 0 ; col < colsNmb ; ++col )
1711 value = ( ((double)segmentsIds( line, col )) - offset ) * scale;
1713 boost::lexical_cast< std::string >( value ) )
1715 rasterPtr->
setValue( col, line, value , 0 );
1724 const unsigned int& xStart,
const unsigned int& yStart,
1725 const unsigned int& xBound,
const unsigned int& yBound,
1728 unsigned int& edgeLength1,
1729 unsigned int& edgeLength2 )
1750 unsigned int xIdx = 0;
1751 const unsigned int lastColIdx = colsNumber - 1;
1752 const unsigned int lastLineIdx = linesNumber - 1;
1754 for(
unsigned int yIdx = yStart ; yIdx < yBound ; ++yIdx )
1756 for( xIdx = xStart; xIdx < xBound ; ++xIdx )
1758 if( segsIds[ yIdx ][ xIdx ] == id1 )
1761 if( segsIds[ yIdx - 1 ][ xIdx ] == id2 )
1767 if( segsIds[ yIdx ][ xIdx - 1 ] == id2 )
1772 if( yIdx < lastLineIdx)
1773 if( segsIds[ yIdx + 1 ][ xIdx ] == id2 )
1778 if( xIdx < lastColIdx )
1779 if( segsIds[ yIdx ][ xIdx + 1 ] == id2 )
1785 else if( segsIds[ yIdx ][ xIdx ] == id2 )
1788 if( segsIds[ yIdx - 1 ][ xIdx ] == id1 )
1794 if( segsIds[ yIdx ][ xIdx - 1 ] == id1 )
1799 if( yIdx < lastLineIdx)
1800 if( segsIds[ yIdx + 1 ][ xIdx ] == id1 )
1805 if( xIdx < lastColIdx )
1806 if( segsIds[ yIdx ][ xIdx + 1 ] == id1 )
1819 unsigned int returnValue = 0;
SegmenterRegionGrowingSegment::FeatureType getDissimilarity(SegmenterRegionGrowingSegment const *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment *const mergePreviewSegPtr) const
Returns a dimilarity index between this and the other segment.
Segmenter segments IDs manager.
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
void mergeFeatures(SegmenterRegionGrowingSegment *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment const *const mergePreviewSegPtr) const
Merge specific segment features from both segments into the first segment.
The Baatz based features will be used - Reference: Baatz, M.; Schape, A. Multiresolution segmentation...
#define BAATZ_SM(featPtr)
Baatz based Segments merger.
bool m_isInitialized
true if this instance is initialized.
Raster region growing segmenter strategy.
unsigned int m_neighborSegmentsSize
The current size of m_neighborSegments.
void disable()
Disable this segment ( same as m_mergetIteration = std::numeric_limits< SegmenterRegionGrowingSegment...
std::vector< SegmenterRegionGrowingSegment::FeatureType > m_bandsWeights
A vector where each bands weight are stored.
A raster band description.
BaatzMerger(const double &colorWeight, const double &compactnessWeight, const std::vector< double > &bandsWeights, const SegmentsIdsMatrixT &segmentsIds)
Default constructor.
#define BAATZ_SS(featPtr, bandsNmb, band)
virtual void update(SegmenterRegionGrowingSegment *const actSegsListHeadPtr)=0
Update the internal state.
SegmenterRegionGrowingStrategy::Parameters m_parameters
Internal execution parameters.
#define BAATZ_CO(featPtr)
unsigned int m_yBound
Segment lower bound Y coordinate box over the label image.
unsigned int m_xStart
Segment left X coordinate box over the label image.
SegmentsIdsMatrixT m_segmentsIdsMatrix
A internal segments IDs matrix that can be reused on each strategy execution.
SegmenterRegionGrowingSegment::FeatureType getDissimilarity(SegmenterRegionGrowingSegment const *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment *const mergePreviewSegPtr) const
Returns a dimilarity index between this and the other segment.
This class can be used to inform the progress of a task.
bool m_enableSameIterationMerges
If enabled, a merged segment could be merged with another within the same iteration (default:false)...
IterationCounterType m_mergetIteration
The current merge iteration.
void mergeFeatures(SegmenterRegionGrowingSegment *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment const *const mergePreviewSegPtr) const
Merge specific segment features from both segments into the first segment.
virtual SegmenterRegionGrowingSegment::FeatureType getDissimilarity(SegmenterRegionGrowingSegment const *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment *const mergePreviewSegPtr) const =0
Returns a dimilarity index between this and the other segment.
unsigned int m_xBound
Segment lower bound X coordinate box over the label image.
void update(SegmenterRegionGrowingSegment *const actSegsListHeadPtr)
Update the internal state.
SegmenterRegionGrowingSegment * m_nextActiveSegment
A pointer to the next active segment.
Raster region growing segmenter strategy factory.
void addNeighborSegment(SegmenterRegionGrowingSegment *const nSegPtr)
Add a pointer of a neighbor segment (if it is not already there).
#define BAATZ_ST(featPtr, bandsNmb, band)
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
bool initializeSegments(SegmenterIdsManager &segmenterIdsManager, const te::rp::SegmenterSegmentsBlock &block2ProcessInfo, const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< double > &inputRasterNoDataValues, const std::vector< double > &inputRasterBandMinValues, const std::vector< double > &inputRasterBandMaxValues, SegmenterRegionGrowingSegment **actSegsListHeadPtr)
Initialize the segment objects container and the segment IDs container.
Segmenter Strategy Parameters.
Raster segmenter strategy factory base class.
void clear()
Clear all stored segments.
unsigned int SegmentIdDataType
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...
std::vector< unsigned int > m_rightCutOffProfile
SegmenterRegionGrowingSegmentsPool m_segmentsPool
A pool of segments that can be reused on each strategy execution.
MeanMerger(const unsigned int featuresNumber)
void mergeSegments(const SegmenterRegionGrowingSegment::FeatureType disimilarityThreshold, const unsigned int maxSegSizeThreshold, SegmenterIdsManager &segmenterIdsManager, Merger &merger, const bool enablelocalMutualBestFitting, const bool enableSameIterationMerges, SegmenterRegionGrowingSegment *auxSeg1Ptr, SegmenterRegionGrowingSegment *auxSeg2Ptr, SegmenterRegionGrowingSegment *auxSeg3Ptr, SegmenterRegionGrowingSegment::FeatureType &minFoundDissimilarity, SegmenterRegionGrowingSegment::FeatureType &maxFoundDissimilarity, unsigned int &totalMergesNumber, SegmenterRegionGrowingSegment::IterationCounterType &globalMergeIterationsCounter, SegmenterRegionGrowingSegment **const actSegsListHeadPtrPtr)
Merge closest segments.
The mean of segments pixel values will be used - Reference: S. A. Bins, L. M. G. Fonseca, G. J. Erthal e F. M. Ii, "Satellite Imagery segmentation: a region growing approach", VIII Simposio Brasileiro de Sensoriamento Remoto, Salvador, BA, 14-19 abril 1996.
An abstract class for raster data strucutures.
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
unsigned int getColumnsNumber() const
The number of current matrix columns.
SegmenterRegionGrowingSegment * m_prevActiveSegment
A pointer to the previous active segment.
unsigned short int IterationCounterType
Feature type definition.
Raster segmenter strategy base class.
SegmentFeaturesType m_segmentFeatures
What segment features will be used on the segmentation process (default:InvalidFeaturesType).
double getMemUsageEstimation(const unsigned int bandsToProcess, const unsigned int pixelsNumber) const
Returns a memory estimation (bytes).
void reset()
Clear all internal allocated resources and go back to the initial not-initialized state...
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
SegmenterRegionGrowingSegment * getNextSegment()
Retrive a stored segment.
bool initialize(const SegmenterSegmentsBlock::SegmentIdDataType segsNumber, const unsigned int featuresNumber)
Pool initialization.
te::rp::SegmenterStrategy * build()
Concrete factories (derived from this one) must implement this method in order to create objects...
unsigned int getOptimalBlocksOverlapSize() const
Returns a optimal blocks overlap size (number of border pixels overlapped between blocks...
FeatureType * m_features
A pionter to a fixed size vector of segment features.
SegmenterRegionGrowingSegment ** m_neighborSegments
Neighborhood segments pointers (some pointers can be null) or a null pointer if there is no neighborh...
unsigned int m_size
Segment area (pixels number).
bool initialize(SegmenterStrategyParameters const *const strategyParams)
Initialize the segmentation strategy.
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
unsigned int getActiveSegmentsNumber(SegmenterRegionGrowingSegment *const actSegsListHeadPtr) const
Returns the number of active segments.
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.
Abstract parameters base interface.
#define BAATZ_EL(featPtr)
unsigned int m_yStart
Segment upper Y coordinate box over the label image.
unsigned int m_bandsNumber
The number of features (bands).
static Raster * make()
It creates and returns an empty raster with default raster driver.
SegmenterRegionGrowingStrategyFactory()
Mean based Segments merger.
float FeatureType
Feature type definition.
double m_colorWeight
The weight given to the color component, deafult:0.9, valid range: [0,1].
SegmenterRegionGrowingStrategy()
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
#define BAATZ_STSTARTPTR(featPtr, bandsNmb)
const Parameters & operator=(const Parameters ¶ms)
virtual void mergeFeatures(SegmenterRegionGrowingSegment *const segment1Ptr, SegmenterRegionGrowingSegment const *const segment2Ptr, SegmenterRegionGrowingSegment const *const mergePreviewSegPtr) const =0
Merge specific segment features from both segments into the first segment.
Segmenter segments block description class.
void exportSegs2Tif(const SegmentsIdsMatrixT &segmentsIds, bool normto8bits, const std::string &fileName)
Export the segments IDs to a tif file.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
AbstractParameters * clone() const
Create a clone copy of this instance.
void removeNeighborSegment(SegmenterRegionGrowingSegment *const nSegPtr)
Remove all occurrences of a neighbor segment.
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
static void getTouchingEdgeLength(const SegmentsIdsMatrixT &segsIds, const unsigned int &xStart, const unsigned int &yStart, const unsigned int &xBound, const unsigned int &yBound, const SegmenterSegmentsBlock::SegmentIdDataType &id1, const SegmenterSegmentsBlock::SegmentIdDataType &id2, unsigned int &edgeLength1, unsigned int &edgeLength2)
Returns the count of points from region 1 (with ID1) touching the region 2 (with ID2).
void addFreeIDs(const std::vector< SegmenterSegmentsBlock::SegmentIdDataType > &ids)
Stores free unique IDs for later use.
A rectified grid is the spatial support for raster data.
std::vector< unsigned int > m_topCutOffProfile
bool m_enableLocalMutualBestFitting
If enabled, a merge only occurs between two segments if the minimum dissimilarity criteria is best fu...
unsigned int getLinesNumber() const
The number of current matrix lines.
~SegmenterRegionGrowingStrategy()
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Raster region growing segmenter strategy.
void clearNeighborSegments()
Remove all neighbor segments.
bool getNewIDs(const unsigned int &idsNumber, std::vector< SegmenterSegmentsBlock::SegmentIdDataType > &ids)
Returns new segment unique IDs.
bool execute(SegmenterIdsManager &segmenterIdsManager, const te::rp::SegmenterSegmentsBlock &block2ProcessInfo, const te::rst::Raster &inputRaster, const std::vector< unsigned int > &inputRasterBands, const std::vector< double > &inputRasterNoDataValues, const std::vector< double > &inputRasterBandMinValues, const std::vector< double > &inputRasterBandMaxValues, te::rst::Raster &outputRaster, const unsigned int outputRasterBand, const bool enableProgressInterface)
Executes the segmentation strategy over region delimited by the given block.
std::vector< unsigned int > m_leftCutOffProfile
SegmenterSegmentsBlock::SegmentIdDataType m_id
Segment ID.
#define BAATZ_SU(featPtr, band)
~SegmenterRegionGrowingStrategyFactory()
std::vector< unsigned int > m_bottomCutOffProfile