32 #include "../raster/RasterFactory.h" 33 #include "../raster/Band.h" 34 #include "../raster/BandProperty.h" 35 #include "../raster/Raster.h" 36 #include "../raster/Enums.h" 37 #include "../raster/Grid.h" 38 #include "../raster/Utils.h" 39 #include "../raster/SynchronizedRaster.h" 40 #include "../memory/CachedRaster.h" 41 #include "../dataaccess/datasource/DataSource.h" 42 #include "../common/PlatformUtils.h" 43 #include "../common/StringUtils.h" 44 #include "../geometry/Coord2D.h" 45 #include "../datatype/Enums.h" 53 #include <boost/thread.hpp> 54 #include <boost/lexical_cast.hpp> 152 m_outputRasterPtr.reset();
174 m_outputParametersPtr =
nullptr;
175 m_segsBlocksMatrixPtr =
nullptr;
176 m_generalMutexPtr =
nullptr;
177 m_inputRasterSyncPtr =
nullptr;
178 m_outputRasterSyncPtr =
nullptr;
179 m_blockProcessedSignalMutexPtr =
nullptr;
180 m_abortSegmentationFlagPtr =
nullptr;
181 m_segmentsIdsManagerPtr =
nullptr;
182 m_blockProcessedSignalPtr =
nullptr;
183 m_runningThreadsCounterPtr =
nullptr;
184 m_inputRasterBandMinValues.clear();
185 m_inputRasterBandMaxValues.clear();
187 m_enableStrategyProgress =
false;
188 m_maxInputRasterCachedBlocks = 0;
202 throw( te::rp::Exception )
209 std::unique_ptr< SegmenterStrategy > strategyPtr(
212 "Unable to create an segmentation strategy");
215 "Unable to initialize the segmentation strategy: " +
216 strategyPtr->getErrorMessage());
218 const double stratMemUsageEstimation = strategyPtr->getMemUsageEstimation(
223 "Invalid strategy memory usage factorMemUsageFactor");
232 std::vector< te::rst::BandProperty* > bandsProperties;
237 bandsProperties[ 0 ]->m_noDataValue = 0;
249 "Output raster creation error" );
255 const unsigned int nRows =outRaster.getNumberOfRows();
256 const unsigned int nCols = outRaster.getNumberOfColumns();
257 unsigned int row = 0;
258 unsigned int col = 0;
260 for( row = 0 ; row < nRows ; ++row )
262 for( col = 0 ; col <
nCols ; ++
col )
271 const unsigned int totalRasterPixels =
281 const double freeVMem =
MIN( totalPhysMem, ( totalVMem - usedVMem ) );
282 const double pixelRequiredRam = stratMemUsageEstimation / ((double)totalRasterPixels);
283 const double maxSimultaneousMemoryPixels = std::min( ((
double)totalRasterPixels),
284 0.75 * freeVMem / pixelRequiredRam );
288 std::unique_ptr< te::rst::Raster > cachedRasterHandler;
295 cachedRasterPtr = cachedRasterHandler.get();
300 std::vector< std::complex< double > > inputRasterBandMinValues(
302 std::vector< std::complex< double > > inputRasterBandMaxValues(
305 if( strategyPtr->shouldComputeMinMaxValues() )
307 const unsigned int nRows =
309 const unsigned int nCols =
311 unsigned int row = 0;
312 unsigned int col = 0;
313 double bandMin = std::numeric_limits< double >::max();
314 double bandMax = -1.0 * std::numeric_limits< double >::max();
317 std::vector< std::complex< double > > noDataValues;
320 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
323 noDataValues.push_back(
334 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
340 bandMin = std::numeric_limits< double >::max();
341 bandMax = -1.0 * std::numeric_limits< double >::max();
343 for( row = 0 ; row < nRows ; ++row )
345 for( col = 0 ; col <
nCols ; ++
col )
349 if( value != noDataValues[ inputRasterBandsIdx ] )
351 if( bandMin > value ) bandMin = value;
352 if( bandMax < value ) bandMax = value;
359 ( bandMin != std::numeric_limits< double >::max() )
361 ( bandMax != ( -1.0 * std::numeric_limits< double >::max() ) )
362 ),
"No valid pixels found");
364 inputRasterBandMinValues[ inputRasterBandsIdx ] = bandMin;
365 inputRasterBandMaxValues[ inputRasterBandsIdx ] = bandMax;
371 unsigned int maxSegThreads = 0;
383 if( maxSegThreads == 1 )
392 unsigned int maxNonExpandedBlockWidth = 0;
393 unsigned int maxNonExpandedBlockHeight = 0;
394 unsigned int maxExpandedBlockWidth = 0;
395 unsigned int maxExpandedBlockHeight = 0;
396 unsigned int blocksHOverlapSize = 0;
397 unsigned int blocksVOverlapSize = 0;
402 ( maxSegThreads > 0 )
404 ( maxSimultaneousMemoryPixels < ((
double)totalRasterPixels ) )
421 unsigned int maxBlockPixels = 0;
430 maxBlockPixels =
static_cast<unsigned int>(
431 ( maxSimultaneousMemoryPixels /
432 (
static_cast<double>( maxSegThreads ? maxSegThreads : 1 ) ) ) );
442 maxNonExpandedBlockWidth,
443 maxNonExpandedBlockHeight,
444 maxExpandedBlockWidth,
445 maxExpandedBlockHeight ),
446 "Error calculating best block size" );
450 maxNonExpandedBlockWidth = maxExpandedBlockWidth =
452 maxNonExpandedBlockHeight = maxExpandedBlockHeight =
458 const unsigned int hBlocksNumber = (
unsigned int)ceil(
460 ((
double)maxNonExpandedBlockWidth) );
461 const unsigned int vBlocksNumber = (
unsigned int)ceil(
463 ((
double)maxNonExpandedBlockHeight ) );
468 std::vector< std::vector< unsigned int> > imageHorizontalProfiles;
469 std::vector< std::vector< unsigned int> > imageVerticalProfiles;
477 const unsigned int verticalProfilesNeighborhoodSize = blocksHOverlapSize / 2;
478 const unsigned int horizontalProfilesNeighborhoodSize = blocksVOverlapSize / 2;
479 const unsigned int pixelNeighborhoodSize = 5;
480 const unsigned int profileAntiSmoothingFactor = 3;
481 std::vector< unsigned int > imageHorizontalProfilesCenterLines;
482 std::vector< unsigned int > imageVerticalProfilesCenterLines;
483 std::vector< unsigned int> profile;
484 unsigned int profileIdx = 0;
486 for( profileIdx = 1 ; profileIdx < hBlocksNumber ;
491 const unsigned int centerLine = std::min(
492 ( profileIdx * maxNonExpandedBlockHeight ) - 1,
497 pixelNeighborhoodSize, horizontalProfilesNeighborhoodSize,
498 profileAntiSmoothingFactor, profile ),
499 "Horizontal profile generation error" );
501 imageHorizontalProfiles.push_back( profile );
502 imageHorizontalProfilesCenterLines.push_back( centerLine );
505 for( profileIdx = 1 ; profileIdx < vBlocksNumber ;
510 const unsigned int centerLine = std::min(
511 ( profileIdx * maxNonExpandedBlockWidth ) - 1,
516 pixelNeighborhoodSize, verticalProfilesNeighborhoodSize,
517 profileAntiSmoothingFactor, profile ),
518 "Horizontal profile generation error" );
520 imageVerticalProfiles.push_back( profile );
521 imageVerticalProfilesCenterLines.push_back( centerLine );
540 hBlocksNumber ),
"Blocks matrix reset error" );
542 const int linesBound = (
int)
544 const int colsBound = (
int)
546 int expandedBlockXBound = 0;
547 int expandedBlockYBound = 0;
550 int expandedBlockXStart = 0;
551 int expandedBlockYStart = 0;
553 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
556 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
559 blockXStart = (
int)( segmentsMatrixCol * maxNonExpandedBlockWidth );
560 blockYStart = (
int)( segmentsMatrixLine * maxNonExpandedBlockHeight );
562 expandedBlockXStart = std::max( 0, blockXStart - ((
int)blocksHOverlapSize) );
563 expandedBlockYStart = std::max( 0, blockYStart - ((
int)blocksVOverlapSize) );
565 expandedBlockXBound = std::min( blockXStart + ((
int)maxNonExpandedBlockWidth)
566 + ((
int)blocksHOverlapSize), colsBound );
567 expandedBlockYBound = std::min( blockYStart + ((
int)maxNonExpandedBlockHeight)
568 + ((
int)blocksVOverlapSize), linesBound );
571 segmentsMatrixLine, segmentsMatrixCol );
574 segmentsBlock.
m_startX = (
unsigned int)expandedBlockXStart;
575 segmentsBlock.
m_startY = (
unsigned int)expandedBlockYStart;
576 segmentsBlock.
m_width = (
unsigned int)( expandedBlockXBound -
577 expandedBlockXStart );
578 segmentsBlock.
m_height = (
unsigned int)( expandedBlockYBound -
579 expandedBlockYStart );
589 imageHorizontalProfiles, imageVerticalProfiles,
590 segmentsBlock ),
"Block cutOff profiles update error" );
610 cachedRasterHandler.reset();
611 cachedRasterPtr =
nullptr;
616 ( ( vBlocksNumber * hBlocksNumber ) == 1 ) && ( maxSegThreads == 0 );
618 std::unique_ptr< te::common::TaskProgress > progressPtr;
622 progressPtr->setTotalSteps( 1 + ( vBlocksNumber * hBlocksNumber ) );
623 progressPtr->setMessage(
"Segmentation" );
624 progressPtr->pulse();
629 boost::mutex generalMutex;
630 boost::mutex blockProcessedSignalMutex;
638 volatile bool abortSegmentationFlag =
false;
640 boost::condition_variable blockProcessedSignal;
641 volatile unsigned int runningThreadsCounter = 0;
642 std::string threadErrorMessage;
654 &blockProcessedSignalMutex;
659 &runningThreadsCounter;
667 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
683 int bandBlockSizeBytes =
686 double maxSimultaneousCacheBlocks = ( 0.05 * freeVMem ) / ((
double)bandBlockSizeBytes);
691 1u, (
unsigned int)( maxSimultaneousCacheBlocks / ((
double)maxSegThreads) ) );
696 maxSimultaneousCacheBlocks;
704 if( maxSegThreads && ( ( vBlocksNumber * hBlocksNumber ) > 1 ) )
709 runningThreadsCounter = maxSegThreads;
711 boost::thread_group threads;
712 std::vector< SegmenterThreadEntryParams > threadsParams( maxSegThreads );
714 for(
unsigned int threadIdx = 0 ; threadIdx < maxSegThreads ;
717 threadsParams[ threadIdx ] = baseSegThreadParams;
720 &( threadsParams[ threadIdx ] ) ) );
725 int prevSegmentedBlocksNmb = 0;
727 while( (!abortSegmentationFlag) && (runningThreadsCounter > 0 ) )
729 boost::unique_lock<boost::mutex> lock( blockProcessedSignalMutex );
730 blockProcessedSignal.timed_wait( lock,
731 boost::posix_time::seconds( 1 ) );
735 if( progressPtr.get() )
737 int segmentedBlocksNmb = 0;
738 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
741 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
744 if( segmentsblocksMatrix[ segmentsMatrixLine ][ segmentsMatrixCol ].m_status
747 ++segmentedBlocksNmb;
752 if( segmentedBlocksNmb != prevSegmentedBlocksNmb )
754 progressPtr->pulse();
755 prevSegmentedBlocksNmb = segmentedBlocksNmb;
758 if( ! progressPtr->isActive() )
760 abortSegmentationFlag =
true;
778 runningThreadsCounter = 1;
783 "Segmentation error: " + threadErrorMessage );
802 throw( te::rp::Exception )
811 "Invalid raster pointer" );
817 "Invalid raster bands number" );
818 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
823 "Invalid raster band number" );
829 "Invalid no-data values" );
832 "Invalid blocks overlapped area percentage" );
846 const unsigned int minExapandedBlockPixels,
847 const unsigned int maxExapandedBlockPixels,
848 unsigned int& blocksHOverlapSize,
849 unsigned int& blocksVOverlapSize,
850 unsigned int& nonExpandedBlockWidth,
851 unsigned int& nonExpandedBlockHeight,
852 unsigned int& expandedBlockWidth,
853 unsigned int& expandedBlockHeight )
const 856 "Invalid min and mas block pixels number" );
861 const double maxScaleFactor = (totalImageLines * totalImageCols) /
862 ((
double)minExapandedBlockPixels);
864 unsigned int rescaledAndExpandedBlockPixelsNmb = 0;
866 for(
double scaleFactor = 1.0 ; scaleFactor <= maxScaleFactor ;
869 nonExpandedBlockHeight = (
unsigned int)std::ceil( ((
double)totalImageLines) / scaleFactor );
870 nonExpandedBlockWidth = (
unsigned int)std::ceil( ((
double)totalImageCols) / scaleFactor );
873 ((
double)nonExpandedBlockWidth) / 100.0 );
875 ((
double)nonExpandedBlockHeight) / 100.0 );
877 expandedBlockHeight = nonExpandedBlockHeight + blocksVOverlapSize +
879 expandedBlockWidth = nonExpandedBlockWidth + blocksHOverlapSize +
882 rescaledAndExpandedBlockPixelsNmb = expandedBlockHeight * expandedBlockWidth;
884 if( rescaledAndExpandedBlockPixelsNmb <= maxExapandedBlockPixels )
897 "Invalid parameter" );
899 "Invalid parameter" );
901 "Invalid parameter" );
903 "Invalid parameter" );
905 "Invalid parameter" );
907 "Invalid parameter" );
909 "Invalid parameter" );
911 "Invalid parameter" );
913 "Invalid parameter" );
915 "Invalid parameter" );
917 "Invalid parameter" );
919 "Invalid parameter" );
921 "Invalid parameter" );
923 "Invalid parameter" );
934 boost::shared_ptr< SegmenterStrategy > strategyPtr(
938 "Unable to create an segmentation strategy" );
939 if( ! strategyPtr->initialize(
960 for(
unsigned int sBMLine = 0 ; sBMLine <
963 for(
unsigned int sBMCol = 0 ; sBMCol <
1009 if( ! strategyPtr->execute(
1051 boost::lock_guard<boost::mutex> blockProcessedSignalLockGuard(
1067 strategyPtr.reset();
1084 const std::vector< unsigned int >& inRasterBands,
1085 const unsigned int pixelNeighborhoodSize,
1086 const unsigned int tileNeighborhoodSize,
1087 const unsigned int profileAntiSmoothingFactor,
1088 std::vector< unsigned int>& profile )
const 1092 if( profileAntiSmoothingFactor == 0 )
1100 if( tileNeighborhoodSize < pixelNeighborhoodSize )
1107 const int inRasterBandsSize = (
int)inRasterBands.size();
1111 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterRowsNmb - 1,
1112 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1113 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterRowsNmb,
1114 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1115 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1116 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1119 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1120 (
int)pixelNeighborhoodSize;
1121 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1122 (
int)pixelNeighborhoodSize;
1124 int crossProfileIdx = 0;
1125 int crossProfileStartIdx = 0;
1126 int crossProfileBoundIdx = 0;
1128 int pixelNBStartIdx = 0;
1129 int pixelNBEndIdx = 0;
1130 int pixel1NBIdx = 0;
1131 int pixel2NBIdx = 0;
1132 int pixelNBOffset = 0;
1135 double currBandDiffSum = 0;
1136 double higherDiffSum = 0;
1137 int higherDiffSumIdx = 0;
1139 int inRasterBandsIdx = 0;
1140 unsigned int bandIdx = 0;
1142 double pixel1Value = 0;
1143 double pixel2Value = 0;
1145 profile.resize( inRasterColsNmb, 0 );
1147 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterColsNmb ;
1148 ++profileElementIdx )
1150 if( profileElementIdx )
1152 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1153 (
int)profileAntiSmoothingFactor;
1154 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1155 minCrossProfileStartIdx );
1157 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1158 ((
int)profileAntiSmoothingFactor);
1159 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1160 maxCrossProfileBoundIdx );
1164 crossProfileStartIdx = minCrossProfileStartIdx;
1165 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1169 higherDiffSumIdx = crossProfileStartIdx;
1171 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1172 crossProfileBoundIdx ; ++crossProfileIdx )
1179 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1180 ++inRasterBandsIdx )
1182 bandIdx = inRasterBands[ inRasterBandsIdx ];
1186 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1187 pixelNBEndIdx = crossProfileIdx + ( (
int)pixelNeighborhoodSize);
1188 currBandDiffSum = 0;
1190 for( pixelNBOffset = 0 ; pixelNBOffset < ((
int)pixelNeighborhoodSize) ;
1193 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1194 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1197 ( pixel1NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1199 ( pixel2NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1201 inRaster.
getValue( profileElementIdx,
1202 pixel1NBIdx, pixel1Value, bandIdx );
1203 inRaster.
getValue( profileElementIdx,
1204 pixel2NBIdx, pixel2Value, bandIdx );
1206 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1209 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1210 pixelNBStartIdx + 1 ) ) );
1213 if( diffSum > higherDiffSum )
1215 higherDiffSum = diffSum;
1216 higherDiffSumIdx = crossProfileIdx;
1220 profile[ profileElementIdx ] = higherDiffSumIdx;
1228 const std::vector< unsigned int >& inRasterBands,
1229 const unsigned int pixelNeighborhoodSize,
1230 const unsigned int tileNeighborhoodSize,
1231 const unsigned int profileAntiSmoothingFactor,
1232 std::vector< unsigned int>& profile )
const 1236 if( profileAntiSmoothingFactor == 0 )
1244 if( tileNeighborhoodSize < pixelNeighborhoodSize )
1251 const int inRasterBandsSize = (
int)inRasterBands.size();
1255 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterColsNmb - 1,
1256 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1257 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterColsNmb,
1258 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1259 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1260 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1263 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1264 (
int)pixelNeighborhoodSize;
1265 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1266 (
int)pixelNeighborhoodSize;
1268 int crossProfileIdx = 0;
1269 int crossProfileStartIdx = 0;
1270 int crossProfileBoundIdx = 0;
1272 int pixelNBStartIdx = 0;
1273 int pixelNBEndIdx = 0;
1274 int pixel1NBIdx = 0;
1275 int pixel2NBIdx = 0;
1276 int pixelNBOffset = 0;
1279 double currBandDiffSum = 0;
1280 double higherDiffSum = 0;
1281 int higherDiffSumIdx = 0;
1283 int inRasterBandsIdx = 0;
1284 unsigned int bandIdx = 0;
1286 double pixel1Value = 0;
1287 double pixel2Value = 0;
1289 profile.resize( inRasterRowsNmb, 0 );
1291 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterRowsNmb ;
1292 ++profileElementIdx )
1294 if( profileElementIdx )
1296 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1297 (
int)profileAntiSmoothingFactor;
1298 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1299 minCrossProfileStartIdx );
1301 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1302 ((
int)profileAntiSmoothingFactor);
1303 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1304 maxCrossProfileBoundIdx );
1308 crossProfileStartIdx = minCrossProfileStartIdx;
1309 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1313 higherDiffSumIdx = crossProfileStartIdx;
1315 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1316 crossProfileBoundIdx ; ++crossProfileIdx )
1323 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1324 ++inRasterBandsIdx )
1326 bandIdx = inRasterBands[ inRasterBandsIdx ];
1330 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1331 pixelNBEndIdx = crossProfileIdx + ( (
int)pixelNeighborhoodSize);
1332 currBandDiffSum = 0;
1334 for( pixelNBOffset = 0 ; pixelNBOffset < ((
int)pixelNeighborhoodSize) ;
1337 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1338 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1341 ( pixel1NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1343 ( pixel2NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1345 inRaster.
getValue( pixel1NBIdx, profileElementIdx,
1346 pixel1Value, bandIdx );
1347 inRaster.
getValue( pixel2NBIdx, profileElementIdx,
1348 pixel2Value, bandIdx );
1350 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1353 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1354 pixelNBStartIdx + 1 ) ) );
1357 if( diffSum > higherDiffSum )
1359 higherDiffSum = diffSum;
1360 higherDiffSumIdx = crossProfileIdx;
1364 profile[ profileElementIdx ] = higherDiffSumIdx;
1371 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1372 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1390 imageHorizontalProfiles.size() ) )
1392 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1395 if( profile.empty() )
1402 int profElementValue = 0;
1404 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1407 if( profEIdx + segmentsBlock.
m_startX >= profile.size() )
1413 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1415 profElementValue =
MAX( 0, profElementValue );
1416 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1434 imageHorizontalProfiles.size() )
1436 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1439 if( profile.empty() )
1446 int profElementValue = 0;
1448 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1451 if( profEIdx + segmentsBlock.
m_startX >= profile.size() )
1457 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1459 profElementValue =
MAX( 0, profElementValue );
1460 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1479 imageVerticalProfiles.size() ) )
1481 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1484 if( profile.empty() )
1491 int profElementValue = 0;
1493 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1496 if( profEIdx + segmentsBlock.
m_startY >= profile.size() )
1502 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1504 profElementValue =
MAX( 0, profElementValue );
1505 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1523 imageVerticalProfiles.size() )
1525 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1528 if( profile.empty() )
1535 int profElementValue = 0;
1537 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1540 if( profEIdx + segmentsBlock.
m_startY >= profile.size() )
1546 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1548 profElementValue =
MAX( 0, profElementValue );
1549 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1571 const std::vector<std::vector<unsigned int> >& imageHorizontalProfiles,
1572 const std::vector<unsigned int>& imageHorizontalProfilesCenterLines,
1573 const std::vector<std::vector<unsigned int> >& imageVerticalProfiles,
1574 const std::vector<unsigned int>& imageVerticalProfilesCenterLines,
1575 const std::string& )
1578 imageHorizontalProfilesCenterLines.size(),
"Internal error" )
1580 imageVerticalProfilesCenterLines.size(),
"Internal error" )
1582 std::vector< te::rst::BandProperty* > bandsProperties;
1587 bandsProperties[ 0 ]->m_noDataValue = 0;
1591 std::map< std::string, std::string > rInfo;
1592 rInfo[
"URI" ] =
"cutofflines.tif";
1594 std::unique_ptr< te::rst::Raster > outRasterPtr(
1597 bandsProperties, rInfo ) );
1599 "Tiff creation error" )
1602 for(
unsigned int profIdx = 0 ; profIdx < imageHorizontalProfiles.size() ;
1605 const std::vector< unsigned int>& profile =
1606 imageHorizontalProfiles[ profIdx ];
1608 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1610 const unsigned int centerLineIdx =
1611 imageHorizontalProfilesCenterLines[ profIdx ];
1613 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1616 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1620 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1622 outRasterPtr->setValue( eIdx, centerLineIdx, 255, 0 );
1623 outRasterPtr->setValue( eIdx, profile[ eIdx ] , 255, 0 );
1629 for(
unsigned int profIdx = 0 ; profIdx < imageVerticalProfiles.size() ;
1632 const std::vector< unsigned int>& profile =
1633 imageVerticalProfiles[ profIdx ];
1635 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1637 const unsigned int centerLineIdx =
1638 imageVerticalProfilesCenterLines[ profIdx ];
1640 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1642 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1646 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1648 outRasterPtr->setValue( centerLineIdx, eIdx, 255, 0 );
1649 outRasterPtr->setValue( profile[ eIdx ], eIdx, 255, 0 );
bool m_instanceInitialized
Segmenter segments IDs manager.
Raster segmenter strategy factory base class.
AbstractParameters * clone() const
Create a clone copy of this instance.
std::vector< std::complex< double > > m_inputRasterNoDataValues
A vector of values to be used as input raster no-data values.
std::string * m_threadErrorMessagePtr
A pointer to a string where the thread error message will be stored.
An adapter class to allow concurrent access to raster data by multiple threads.
TECOMMONEXPORT unsigned long long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...
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.
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0).
A raster band description.
Base exception class for plugin module.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
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.
Raster Processing algorithm output parameters base interface.
#define MIN(a, b)
Macro that returns min between two values.
SegmenterThreadEntryParams()
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<double>::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.
#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...
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Segmenter Strategy Parameters.
unsigned int unsigned int nCols
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.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
unsigned int getColumnsNumber() const
The number of current matrix columns.
BandProperty * getProperty()
Returns the band property.
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.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
std::vector< std::complex< double > > m_inputRasterBandMaxValues
A vector of input raster bands maximum 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.
Raster region growing segmenter Mean strategy.
~SegmenterThreadEntryParams()
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
TECOMMONEXPORT unsigned long long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
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). ...
Raster Processing functions.
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.
TECOMMONEXPORT unsigned long long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
A rectified grid is the spatial support for raster data.
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
const Segmenter::OutputParameters & operator=(const Segmenter::OutputParameters ¶ms)
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...
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).
std::vector< std::complex< double > > m_inputRasterBandMinValues
A vector of input raster bands minimum values.
#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.
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
std::vector< unsigned int > m_leftCutOffProfile
std::vector< unsigned int > m_bottomCutOffProfile