31 #include "../raster/RasterFactory.h"
32 #include "../raster/Band.h"
33 #include "../raster/BandProperty.h"
34 #include "../raster/Raster.h"
35 #include "../raster/Enums.h"
36 #include "../raster/Grid.h"
37 #include "../raster/Utils.h"
38 #include "../raster/SynchronizedRaster.h"
39 #include "../memory/CachedRaster.h"
40 #include "../dataaccess/datasource/DataSource.h"
41 #include "../common/PlatformUtils.h"
42 #include "../common/StringUtils.h"
43 #include "../geometry/Coord2D.h"
44 #include "../datatype/Enums.h"
52 #include <boost/thread.hpp>
53 #include <boost/lexical_cast.hpp>
67 m_segStratParamsPtr = 0;
80 m_inputRasterBands.clear();
81 m_inputRasterNoDataValues.clear();
82 m_enableThreadedProcessing =
true;
84 m_enableBlockProcessing =
true;
86 m_blocksOverlapPercent = 10;
87 m_strategyName.clear();
88 m_enableProgress =
false;
89 m_enableRasterCache =
true;
91 if( m_segStratParamsPtr )
93 delete m_segStratParamsPtr;
94 m_segStratParamsPtr = 0;
130 if( m_segStratParamsPtr )
132 delete m_segStratParamsPtr;
133 m_segStratParamsPtr = 0;
142 return m_segStratParamsPtr;
165 m_outputRasterPtr.reset();
187 m_outputParametersPtr = 0;
188 m_segsBlocksMatrixPtr = 0;
189 m_generalMutexPtr = 0;
190 m_inputRasterSyncPtr = 0;
191 m_outputRasterSyncPtr = 0;
192 m_blockProcessedSignalMutexPtr = 0;
193 m_abortSegmentationFlagPtr = 0;
194 m_segmentsIdsManagerPtr = 0;
195 m_blockProcessedSignalPtr = 0;
196 m_runningThreadsCounterPtr = 0;
197 m_inputRasterBandMinValues.clear();
198 m_inputRasterBandMaxValues.clear();
199 m_inputRasterNoDataValues.clear();
200 m_enableStrategyProgress =
false;
201 m_maxInputRasterCachedBlocks = 0;
218 throw( te::rp::Exception )
229 std::vector< te::rst::BandProperty* > bandsProperties;
234 bandsProperties[ 0 ]->m_noDataValue = 0;
246 "Output raster creation error" );
252 const unsigned int nRows =outRaster.getNumberOfRows();
253 const unsigned int nCols = outRaster.getNumberOfColumns();
254 unsigned int row = 0;
255 unsigned int col = 0;
257 for( row = 0 ; row < nRows ; ++row )
259 for( col = 0 ; col < nCols ; ++col )
268 std::auto_ptr< SegmenterStrategy > strategyPtr(
271 "Unable to create an segmentation strategy" );
274 "Unable to initialize the segmentation strategy" );
276 const double stratMemUsageEstimation = strategyPtr->getMemUsageEstimation(
281 "Invalid strategy memory usage factorMemUsageFactor" );
285 const unsigned int totalRasterPixels =
295 const double freeVMem =
MIN( totalPhysMem, ( totalVMem - usedVMem ) );
296 const double pixelRequiredRam = stratMemUsageEstimation / ((double)totalRasterPixels);
297 const double maxSimultaneousMemoryPixels = std::min( ((
double)totalRasterPixels),
298 0.75 * freeVMem / pixelRequiredRam );
302 std::auto_ptr< te::rst::Raster > cachedRasterHandler;
309 cachedRasterPtr = cachedRasterHandler.get();
314 std::vector< double > inputRasterBandMinValues(
316 std::vector< double > inputRasterBandMaxValues(
320 const unsigned int nRows =
322 const unsigned int nCols =
324 unsigned int row = 0;
325 unsigned int col = 0;
326 double bandMin = DBL_MAX;
327 double bandMax = -1.0 * DBL_MAX;
330 std::vector< double > noDataValues;
333 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
336 noDataValues.push_back(
347 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
354 bandMax = -1.0 * DBL_MAX;
356 for( row = 0 ; row < nRows ; ++row )
358 for( col = 0 ; col < nCols ; ++col )
362 if( value != noDataValues[ inputRasterBandsIdx ] )
364 if( bandMin > value ) bandMin = value;
365 if( bandMax < value ) bandMax = value;
370 inputRasterBandMinValues[ inputRasterBandsIdx ] = bandMin;
371 inputRasterBandMaxValues[ inputRasterBandsIdx ] = bandMax;
377 unsigned int maxSegThreads = 0;
389 if( maxSegThreads == 1 )
398 unsigned int maxNonExpandedBlockWidth = 0;
399 unsigned int maxNonExpandedBlockHeight = 0;
400 unsigned int maxExpandedBlockWidth = 0;
401 unsigned int maxExpandedBlockHeight = 0;
402 unsigned int blocksHOverlapSize = 0;
403 unsigned int blocksVOverlapSize = 0;
408 ( maxSegThreads > 0 )
410 ( maxSimultaneousMemoryPixels < ((
double)totalRasterPixels ) )
427 unsigned int maxBlockPixels = 0;
436 maxBlockPixels =
static_cast<unsigned int>(
437 ( maxSimultaneousMemoryPixels /
438 (
static_cast<double>( maxSegThreads ? maxSegThreads : 1 ) ) ) );
448 maxNonExpandedBlockWidth,
449 maxNonExpandedBlockHeight,
450 maxExpandedBlockWidth,
451 maxExpandedBlockHeight ),
452 "Error calculating best block size" );
456 maxNonExpandedBlockWidth = maxExpandedBlockWidth =
458 maxNonExpandedBlockHeight = maxExpandedBlockHeight =
464 const unsigned int hBlocksNumber = (
unsigned int)ceil(
466 ((
double)maxNonExpandedBlockWidth) );
467 const unsigned int vBlocksNumber = (
unsigned int)ceil(
469 ((
double)maxNonExpandedBlockHeight ) );
474 std::vector< std::vector< unsigned int> > imageHorizontalProfiles;
475 std::vector< std::vector< unsigned int> > imageVerticalProfiles;
482 const unsigned int verticalProfilesNeighborhoodSize = blocksHOverlapSize / 2;
483 const unsigned int horizontalProfilesNeighborhoodSize = blocksVOverlapSize / 2;
484 const unsigned int pixelNeighborhoodSize = 5;
485 const unsigned int profileAntiSmoothingFactor = 3;
486 std::vector< unsigned int > imageHorizontalProfilesCenterLines;
487 std::vector< unsigned int > imageVerticalProfilesCenterLines;
488 std::vector< unsigned int> profile;
489 unsigned int profileIdx = 0;
491 for( profileIdx = 1 ; profileIdx < hBlocksNumber ;
496 const unsigned int centerLine = std::min(
497 ( profileIdx * maxNonExpandedBlockHeight ) - 1,
502 pixelNeighborhoodSize, horizontalProfilesNeighborhoodSize,
503 profileAntiSmoothingFactor, profile ),
504 "Horizontal profile generation error" );
506 imageHorizontalProfiles.push_back( profile );
507 imageHorizontalProfilesCenterLines.push_back( centerLine );
510 for( profileIdx = 1 ; profileIdx < vBlocksNumber ;
515 const unsigned int centerLine = std::min(
516 ( profileIdx * maxNonExpandedBlockWidth ) - 1,
521 pixelNeighborhoodSize, verticalProfilesNeighborhoodSize,
522 profileAntiSmoothingFactor, profile ),
523 "Horizontal profile generation error" );
525 imageVerticalProfiles.push_back( profile );
526 imageVerticalProfilesCenterLines.push_back( centerLine );
545 hBlocksNumber ),
"Blocks matrix reset error" );
547 const int linesBound = (int)
549 const int colsBound = (int)
551 int expandedBlockXBound = 0;
552 int expandedBlockYBound = 0;
555 int expandedBlockXStart = 0;
556 int expandedBlockYStart = 0;
558 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
561 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
564 blockXStart = (int)( segmentsMatrixCol * maxNonExpandedBlockWidth );
565 blockYStart = (int)( segmentsMatrixLine * maxNonExpandedBlockHeight );
567 expandedBlockXStart = std::max( 0, blockXStart - ((
int)blocksHOverlapSize) );
568 expandedBlockYStart = std::max( 0, blockYStart - ((
int)blocksVOverlapSize) );
570 expandedBlockXBound = std::min( blockXStart + ((
int)maxNonExpandedBlockWidth)
571 + ((
int)blocksHOverlapSize), colsBound );
572 expandedBlockYBound = std::min( blockYStart + ((
int)maxNonExpandedBlockHeight)
573 + ((
int)blocksVOverlapSize), linesBound );
576 segmentsMatrixLine, segmentsMatrixCol );
579 segmentsBlock.
m_startX = (
unsigned int)expandedBlockXStart;
580 segmentsBlock.
m_startY = (
unsigned int)expandedBlockYStart;
581 segmentsBlock.
m_width = (
unsigned int)( expandedBlockXBound -
582 expandedBlockXStart );
583 segmentsBlock.
m_height = (
unsigned int)( expandedBlockYBound -
584 expandedBlockYStart );
594 imageHorizontalProfiles, imageVerticalProfiles,
595 segmentsBlock ),
"Block cutOff profiles update error" );
615 cachedRasterHandler.reset();
621 ( ( vBlocksNumber * hBlocksNumber ) == 1 ) && ( maxSegThreads == 0 );
623 std::auto_ptr< te::common::TaskProgress > progressPtr;
627 progressPtr->setTotalSteps( 1 + ( vBlocksNumber * hBlocksNumber ) );
628 progressPtr->setMessage(
"Segmentation" );
629 progressPtr->pulse();
634 boost::mutex generalMutex;
635 boost::mutex blockProcessedSignalMutex;
643 volatile bool abortSegmentationFlag =
false;
647 boost::condition_variable blockProcessedSignal;
649 volatile unsigned int runningThreadsCounter = 0;
661 &blockProcessedSignalMutex;
666 &runningThreadsCounter;
673 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
689 int bandBlockSizeBytes =
692 double maxSimultaneousCacheBlocks = ( 0.05 * freeVMem ) / ((
double)bandBlockSizeBytes);
697 1u, (
unsigned int)( maxSimultaneousCacheBlocks / ((
double)maxSegThreads) ) );
702 maxSimultaneousCacheBlocks;
710 if( maxSegThreads && ( ( vBlocksNumber * hBlocksNumber ) > 1 ) )
715 runningThreadsCounter = maxSegThreads;
717 boost::thread_group threads;
718 std::vector< SegmenterThreadEntryParams > threadsParams( maxSegThreads );
720 for(
unsigned int threadIdx = 0 ; threadIdx < maxSegThreads ;
723 threadsParams[ threadIdx ] = baseSegThreadParams;
726 &( threadsParams[ threadIdx ] ) ) );
731 int prevSegmentedBlocksNmb = 0;
733 while( (!abortSegmentationFlag) && (runningThreadsCounter > 0 ) )
735 boost::unique_lock<boost::mutex> lock( blockProcessedSignalMutex );
736 blockProcessedSignal.timed_wait( lock,
737 boost::posix_time::seconds( 1 ) );
741 if( progressPtr.get() )
743 int segmentedBlocksNmb = 0;
744 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
747 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
750 if( segmentsblocksMatrix[ segmentsMatrixLine ][ segmentsMatrixCol ].m_status
753 ++segmentedBlocksNmb;
758 if( segmentedBlocksNmb != prevSegmentedBlocksNmb )
760 progressPtr->pulse();
761 prevSegmentedBlocksNmb = segmentedBlocksNmb;
764 if( ! progressPtr->isActive() )
766 abortSegmentationFlag =
true;
784 runningThreadsCounter = 1;
788 return (!abortSegmentationFlag);
803 throw( te::rp::Exception )
812 "Invalid raster pointer" );
818 "Invalid raster bands number" );
819 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
824 "Invalid raster band number" );
830 "Invalid no-data values" );
833 "Invalid blocks overlapped area percentage" );
847 const unsigned int minExapandedBlockPixels,
848 const unsigned int maxExapandedBlockPixels,
849 unsigned int& blocksHOverlapSize,
850 unsigned int& blocksVOverlapSize,
851 unsigned int& nonExpandedBlockWidth,
852 unsigned int& nonExpandedBlockHeight,
853 unsigned int& expandedBlockWidth,
854 unsigned int& expandedBlockHeight )
const
857 "Invalid min and mas block pixels number" );
862 const double maxScaleFactor = (totalImageLines * totalImageCols) /
863 ((
double)minExapandedBlockPixels);
865 unsigned int rescaledAndExpandedBlockPixelsNmb = 0;
867 for(
double scaleFactor = 1.0 ; scaleFactor <= maxScaleFactor ;
870 nonExpandedBlockHeight = (
unsigned int)std::ceil( ((
double)totalImageLines) / scaleFactor );
871 nonExpandedBlockWidth = (
unsigned int)std::ceil( ((
double)totalImageCols) / scaleFactor );
874 ((
double)nonExpandedBlockWidth) / 100.0 );
876 ((
double)nonExpandedBlockHeight) / 100.0 );
878 expandedBlockHeight = nonExpandedBlockHeight + blocksVOverlapSize +
880 expandedBlockWidth = nonExpandedBlockWidth + blocksHOverlapSize +
883 rescaledAndExpandedBlockPixelsNmb = expandedBlockHeight * expandedBlockWidth;
885 if( rescaledAndExpandedBlockPixelsNmb <= maxExapandedBlockPixels )
898 "Invalid parameter" );
900 "Invalid parameter" );
902 "Invalid parameter" );
904 "Invalid parameter" );
906 "Invalid parameter" );
908 "Invalid parameter" );
910 "Invalid parameter" );
912 "Invalid parameter" );
914 "Invalid parameter" );
916 "Invalid parameter" );
918 "Invalid parameter" );
920 "Invalid parameter" );
922 "Invalid parameter" );
933 boost::shared_ptr< SegmenterStrategy > strategyPtr(
937 "Unable to create an segmentation strategy" );
938 if( ! strategyPtr->initialize(
959 for(
unsigned int sBMLine = 0 ; sBMLine <
962 for(
unsigned int sBMCol = 0 ; sBMCol <
1008 if( ! strategyPtr->execute(
1049 boost::lock_guard<boost::mutex> blockProcessedSignalLockGuard(
1065 strategyPtr.reset();
1082 const std::vector< unsigned int >& inRasterBands,
1083 const unsigned int pixelNeighborhoodSize,
1084 const unsigned int tileNeighborhoodSize,
1085 const unsigned int profileAntiSmoothingFactor,
1086 std::vector< unsigned int>& profile )
const
1090 if( profileAntiSmoothingFactor == 0 )
1098 if( tileNeighborhoodSize < pixelNeighborhoodSize )
1105 const int inRasterBandsSize = (int)inRasterBands.size();
1109 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterRowsNmb - 1,
1110 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1111 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterRowsNmb,
1112 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1113 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1114 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1117 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1118 (int)pixelNeighborhoodSize;
1119 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1120 (int)pixelNeighborhoodSize;
1122 int crossProfileIdx = 0;
1123 int crossProfileStartIdx = 0;
1124 int crossProfileBoundIdx = 0;
1126 int pixelNBStartIdx = 0;
1127 int pixelNBEndIdx = 0;
1128 int pixel1NBIdx = 0;
1129 int pixel2NBIdx = 0;
1130 int pixelNBOffset = 0;
1133 double currBandDiffSum = 0;
1134 double higherDiffSum = 0;
1135 int higherDiffSumIdx = 0;
1137 int inRasterBandsIdx = 0;
1138 unsigned int bandIdx = 0;
1140 double pixel1Value = 0;
1141 double pixel2Value = 0;
1143 profile.resize( inRasterColsNmb, 0 );
1145 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterColsNmb ;
1146 ++profileElementIdx )
1148 if( profileElementIdx )
1150 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1151 (int)profileAntiSmoothingFactor;
1152 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1153 minCrossProfileStartIdx );
1155 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1156 ((int)profileAntiSmoothingFactor);
1157 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1158 maxCrossProfileBoundIdx );
1162 crossProfileStartIdx = minCrossProfileStartIdx;
1163 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1167 higherDiffSumIdx = crossProfileStartIdx;
1169 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1170 crossProfileBoundIdx ; ++crossProfileIdx )
1177 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1178 ++inRasterBandsIdx )
1180 bandIdx = inRasterBands[ inRasterBandsIdx ];
1184 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1185 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1186 currBandDiffSum = 0;
1188 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1191 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1192 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1195 ( pixel1NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1197 ( pixel2NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1199 inRaster.
getValue( profileElementIdx,
1200 pixel1NBIdx, pixel1Value, bandIdx );
1201 inRaster.
getValue( profileElementIdx,
1202 pixel2NBIdx, pixel2Value, bandIdx );
1204 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1207 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1208 pixelNBStartIdx + 1 ) ) );
1211 if( diffSum > higherDiffSum )
1213 higherDiffSum = diffSum;
1214 higherDiffSumIdx = crossProfileIdx;
1218 profile[ profileElementIdx ] = higherDiffSumIdx;
1226 const std::vector< unsigned int >& inRasterBands,
1227 const unsigned int pixelNeighborhoodSize,
1228 const unsigned int tileNeighborhoodSize,
1229 const unsigned int profileAntiSmoothingFactor,
1230 std::vector< unsigned int>& profile )
const
1234 if( profileAntiSmoothingFactor == 0 )
1242 if( tileNeighborhoodSize < pixelNeighborhoodSize )
1249 const int inRasterBandsSize = (int)inRasterBands.size();
1253 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterColsNmb - 1,
1254 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1255 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterColsNmb,
1256 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1257 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1258 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1261 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1262 (int)pixelNeighborhoodSize;
1263 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1264 (int)pixelNeighborhoodSize;
1266 int crossProfileIdx = 0;
1267 int crossProfileStartIdx = 0;
1268 int crossProfileBoundIdx = 0;
1270 int pixelNBStartIdx = 0;
1271 int pixelNBEndIdx = 0;
1272 int pixel1NBIdx = 0;
1273 int pixel2NBIdx = 0;
1274 int pixelNBOffset = 0;
1277 double currBandDiffSum = 0;
1278 double higherDiffSum = 0;
1279 int higherDiffSumIdx = 0;
1281 int inRasterBandsIdx = 0;
1282 unsigned int bandIdx = 0;
1284 double pixel1Value = 0;
1285 double pixel2Value = 0;
1287 profile.resize( inRasterRowsNmb, 0 );
1289 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterRowsNmb ;
1290 ++profileElementIdx )
1292 if( profileElementIdx )
1294 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1295 (int)profileAntiSmoothingFactor;
1296 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1297 minCrossProfileStartIdx );
1299 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1300 ((int)profileAntiSmoothingFactor);
1301 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1302 maxCrossProfileBoundIdx );
1306 crossProfileStartIdx = minCrossProfileStartIdx;
1307 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1311 higherDiffSumIdx = crossProfileStartIdx;
1313 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1314 crossProfileBoundIdx ; ++crossProfileIdx )
1321 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1322 ++inRasterBandsIdx )
1324 bandIdx = inRasterBands[ inRasterBandsIdx ];
1328 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1329 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1330 currBandDiffSum = 0;
1332 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1335 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1336 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1339 ( pixel1NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1341 ( pixel2NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1343 inRaster.
getValue( pixel1NBIdx, profileElementIdx,
1344 pixel1Value, bandIdx );
1345 inRaster.
getValue( pixel2NBIdx, profileElementIdx,
1346 pixel2Value, bandIdx );
1348 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1351 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1352 pixelNBStartIdx + 1 ) ) );
1355 if( diffSum > higherDiffSum )
1357 higherDiffSum = diffSum;
1358 higherDiffSumIdx = crossProfileIdx;
1362 profile[ profileElementIdx ] = higherDiffSumIdx;
1369 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1370 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1388 imageHorizontalProfiles.size() ) )
1390 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1393 if( profile.empty() )
1400 int profElementValue = 0;
1402 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1405 if( profEIdx + segmentsBlock.
m_startX >= profile.size() )
1411 ((int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1413 profElementValue =
MAX( 0, profElementValue );
1414 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1430 imageHorizontalProfiles.size() )
1432 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1435 if( profile.empty() )
1442 int profElementValue = 0;
1444 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1447 if( profEIdx + segmentsBlock.
m_startX >= profile.size() )
1453 ((int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1455 profElementValue =
MAX( 0, profElementValue );
1456 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1474 imageVerticalProfiles.size() ) )
1476 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1479 if( profile.empty() )
1486 int profElementValue = 0;
1488 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1491 if( profEIdx + segmentsBlock.
m_startY >= profile.size() )
1497 ((int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1499 profElementValue =
MAX( 0, profElementValue );
1500 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1516 imageVerticalProfiles.size() )
1518 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1521 if( profile.empty() )
1528 int profElementValue = 0;
1530 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1533 if( profEIdx + segmentsBlock.
m_startY >= profile.size() )
1539 ((int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1541 profElementValue =
MAX( 0, profElementValue );
1542 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1563 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1564 const std::vector< unsigned int >& imageHorizontalProfilesCenterLines,
1565 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1566 const std::vector< unsigned int >& imageVerticalProfilesCenterLines,
1567 const std::string& filename )
1570 imageHorizontalProfilesCenterLines.size(),
"Internal error" )
1572 imageVerticalProfilesCenterLines.size(),
"Internal error" )
1574 std::vector< te::rst::BandProperty* > bandsProperties;
1579 bandsProperties[ 0 ]->m_noDataValue = 0;
1583 std::map< std::string, std::string > rInfo;
1584 rInfo[
"URI" ] =
"cutofflines.tif";
1586 std::auto_ptr< te::rst::Raster > outRasterPtr(
1589 bandsProperties, rInfo ) );
1591 "Tiff creation error" )
1594 for(
unsigned int profIdx = 0 ; profIdx < imageHorizontalProfiles.size() ;
1597 const std::vector< unsigned int>& profile =
1598 imageHorizontalProfiles[ profIdx ];
1600 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1602 const unsigned int centerLineIdx =
1603 imageHorizontalProfilesCenterLines[ profIdx ];
1605 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1608 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1612 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1614 outRasterPtr->setValue( eIdx, centerLineIdx, 255, 0 );
1615 outRasterPtr->setValue( eIdx, profile[ eIdx ] , 255, 0 );
1621 for(
unsigned int profIdx = 0 ; profIdx < imageVerticalProfiles.size() ;
1624 const std::vector< unsigned int>& profile =
1625 imageVerticalProfiles[ profIdx ];
1627 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1629 const unsigned int centerLineIdx =
1630 imageVerticalProfilesCenterLines[ profIdx ];
1632 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1634 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1638 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1640 outRasterPtr->setValue( centerLineIdx, eIdx, 255, 0 );
1641 outRasterPtr->setValue( profile[ eIdx ], eIdx, 255, 0 );
bool m_instanceInitialized
Segmenter segments IDs manager.
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
Raster segmenter strategy factory base class.
AbstractParameters * clone() const
Create a clone copy of this instance.
An adapter class to allow concurrent access to raster data by multiple threads.
Segmenter Output Parameters.
#define ABS(x)
Absolute value.
Index into a lookup table.
bool updateBlockCutOffProfiles(const std::vector< std::vector< unsigned int > > &imageHorizontalProfiles, const std::vector< std::vector< unsigned int > > &imageVerticalProfiles, SegmenterSegmentsBlock &segmentsBlock) const
Update the block cutOff profiles using the full image profiles.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0).
A raster band description.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
std::vector< double > m_inputRasterBandMaxValues
A vector of input raster bands maximum values.
boost::mutex * m_blockProcessedSignalMutexPtr
Pointer to the mutex used by the block processed signal (default:0).
bool createCutOffLinesTiff(const std::vector< std::vector< unsigned int > > &imageHorizontalProfiles, const std::vector< unsigned int > &imageHorizontalProfilesCenterLines, const std::vector< std::vector< unsigned int > > &imageVerticalProfiles, const std::vector< unsigned int > &imageVerticalProfilesCenterLines, const std::string &filename)
This class can be used to inform the progress of a task.
std::vector< double > m_inputRasterBandMinValues
A vector of input raster bands minimum values.
Raster Processing algorithm output parameters base interface.
#define MIN(a, b)
Macro that returns min between two values.
SegmenterThreadEntryParams()
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
The parameters passed to the Segmenter::segmenterthreadEntry method.
An access synchronizer to be used in SynchronizedRaster raster instances.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
bool calcBestBlockSize(const unsigned int minExapandedBlockPixels, const unsigned int maxExapandedBlockPixels, unsigned int &blocksHOverlapSize, unsigned int &blocksVOverlapSize, unsigned int &nonExpandedBlockWidth, unsigned int &nonExpandedBlockHeight, unsigned int &expandedBlockWidth, unsigned int &expandedBlockHeight) const
Calc the best sub-image block size for each thread to process.
#define MAX(a, b)
Macro that returns max between two values.
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0).
static void segmenterThreadEntry(SegmenterThreadEntryParams *paramsPtr)
Segmenter thread entry.
Raster Processing functions.
#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...
const Algorithm & operator=(const Algorithm &)
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Segmenter Strategy Parameters.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
TECOMMONEXPORT unsigned int GetPhysProcNumber()
Returns the number of physical processors.
std::vector< unsigned int > m_rightCutOffProfile
Segmenter::InputParameters m_inputParameters
Segmenter execution parameters.
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
unsigned int getColumnsNumber() const
The number of current matrix columns.
BandProperty * getProperty()
Returns the band property.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
A RAM cache adaptor to an external existent raster that must always be avaliable. ...
te::rst::RasterSynchronizer * m_inputRasterSyncPtr
Pointer to the input raster synchronizer (default:0).
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0).
unsigned int m_maxInputRasterCachedBlocks
The maximum number of input raster cached blocks per-thread.
A raster band description.
std::vector< double > m_inputRasterNoDataValues
A vector of values to be used as input raster no-data values.
Grid * getGrid()
It returns the raster grid.
unsigned int m_segmentsMatrixYIndex
static te::rp::SegmenterStrategy * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
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.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
~SegmenterThreadEntryParams()
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Abstract parameters base interface.
bool genImageHCutOffProfile(const unsigned int profileCenter, const te::rst::Raster &inRaster, const std::vector< unsigned int > &inRasterBands, const unsigned int pixelNeighborhoodSize, const unsigned int tileNeighborhoodSize, const unsigned int profileAntiSmoothingFactor, std::vector< unsigned int > &profile) const
Generate the horizontal cutOff prifles for the entire image..
bool genImageVCutOffProfile(const unsigned int profileCenter, const te::rst::Raster &inRaster, const std::vector< unsigned int > &inRasterBands, const unsigned int pixelNeighborhoodSize, const unsigned int tileNeighborhoodSize, const unsigned int profileAntiSmoothingFactor, std::vector< unsigned int > &profile) const
Generate the vertical cutOff prifles for the entire image..
bool m_enableStrategyProgress
Enable/Disable the segmentation strategy to use its own progress interface (default:false).
A generic template matrix.
unsigned int m_segmentsMatrixXIndex
static Raster * make()
It creates and returns an empty raster with default raster driver.
SegmentsBlocksMatrixT * m_segsBlocksMatrixPtr
Pointer to the segments blocks matrix (default:0).
Segmenter segments block description class.
boost::condition_variable * m_blockProcessedSignalPtr
Pointer to a signal to be emited when a segments block was processed (default:0). ...
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
boost::mutex * m_generalMutexPtr
Pointer to a general global mutex (default:0).
te::rst::RasterSynchronizer * m_outputRasterSyncPtr
Pointer to the output raster synchronizer (default:0).
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
A rectified grid is the spatial support for raster data.
const Segmenter::OutputParameters & operator=(const Segmenter::OutputParameters ¶ms)
std::vector< unsigned int > m_topCutOffProfile
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
unsigned int getLinesNumber() const
The number of current matrix lines.
unsigned int volatile * m_runningThreadsCounterPtr
Pointer to the running threads counter - default 0).
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Segmenter::InputParameters m_inputParameters
The global segmenter input execution parameters.
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...
std::vector< unsigned int > m_leftCutOffProfile
std::vector< unsigned int > m_bottomCutOffProfile