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 "../dataaccess/datasource/DataSource.h"
38 #include "../common/PlatformUtils.h"
39 #include "../common/StringUtils.h"
40 #include "../geometry/Coord2D.h"
41 #include "../datatype/Enums.h"
61 m_segStratParamsPtr = 0;
74 m_inputRasterBands.clear();
75 m_enableThreadedProcessing =
true;
77 m_enableBlockProcessing =
true;
78 m_enableBlockMerging =
true;
80 m_strategyName.clear();
81 m_enableProgress =
false;
83 if( m_segStratParamsPtr )
85 delete m_segStratParamsPtr;
86 m_segStratParamsPtr = 0;
120 if( m_segStratParamsPtr )
122 delete m_segStratParamsPtr;
123 m_segStratParamsPtr = 0;
132 return m_segStratParamsPtr;
155 m_outputRasterPtr.reset();
176 m_inputParametersPtr = 0;
177 m_outputParametersPtr = 0;
178 m_segsBlocksMatrixPtr = 0;
179 m_generalMutexPtr = 0;
180 m_inputRasterIOMutexPtr = 0;
181 m_outputRasterIOMutexPtr = 0;
182 m_blockProcessedSignalMutexPtr = 0;
183 m_abortSegmentationFlagPtr = 0;
184 m_segmentsIdsManagerPtr = 0;
185 m_blockProcessedSignalPtr = 0;
186 m_runningThreadsCounterPtr = 0;
187 m_inputRasterGainsPtr = 0;
188 m_inputRasterOffsetsPtr = 0;
189 m_enableStrategyProgress =
false;
207 throw( te::rp::Exception )
218 std::vector< te::rst::BandProperty* > bandsProperties;
223 bandsProperties[ 0 ]->m_noDataValue = 0;
235 "Output raster creation error" );
240 std::vector< double > inputRasterGains(
242 std::vector< double > inputRasterOffsets(
246 const unsigned int nRows =
248 const unsigned int nCols =
250 unsigned int row = 0;
251 unsigned int col = 0;
252 double bandMin = DBL_MAX;
253 double bandMax = -1.0 * DBL_MAX;
256 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
263 bandMax = -1.0 * DBL_MAX;
265 for( row = 0 ; row < nRows ; ++row )
266 for( col = 0 ; col < nCols ; ++col )
270 if( bandMin > value ) bandMin = value;
271 if( bandMax < value ) bandMax = value;
274 if( bandMax != bandMin )
276 inputRasterGains[ inputRasterBandsIdx ] = 1.0 / ( bandMax - bandMin );
277 inputRasterOffsets[ inputRasterBandsIdx ] = -1.0 * bandMin;
284 std::auto_ptr< SegmenterStrategy > strategyPtr(
287 "Unable to create an segmentation strategy" );
290 "Unable to initialize the segmentation strategy" );
292 const double stratMemUsageEstimation = strategyPtr->getMemUsageEstimation(
297 "Invalid strategy memory usage factorMemUsageFactor" );
299 const unsigned stratBlocksOverlapSize =
300 strategyPtr->getOptimalBlocksOverlapSize();
304 unsigned int maxSegThreads = 0;
314 const unsigned int totalRasterPixels =
321 const double freeVMem =
MIN( totalPhysMem,
322 ( ( totalVMem <= usedVMem ) ? 0.0 : ( totalVMem - usedVMem ) ) );
323 const double pixelRequiredRam = stratMemUsageEstimation
324 / ((double)totalRasterPixels);
325 const double maxSimultaneousMemoryPixels = 0.7 *
MIN(
326 ((
double)totalRasterPixels),
327 freeVMem / pixelRequiredRam );
331 unsigned int maxNonExpandedBlockWidth = 0;
332 unsigned int maxNonExpandedBlockHeight = 0;
333 unsigned int maxExpandedBlockWidth = 0;
334 unsigned int maxExpandedBlockHeight = 0;
335 unsigned int blocksHOverlapSize = 0;
336 unsigned int blocksVOverlapSize = 0;
341 ( maxSegThreads > 0 )
343 ( maxSimultaneousMemoryPixels < ((
double)totalRasterPixels ) )
360 unsigned int maxBlockPixels = 0;
369 maxBlockPixels =
static_cast<unsigned int>(
370 ( maxSimultaneousMemoryPixels /
371 (
static_cast<double>( maxSegThreads ? maxSegThreads : 1 ) ) ) );
383 std::max( blocksHOverlapSize, blocksVOverlapSize ) *
384 std::max( blocksHOverlapSize, blocksVOverlapSize ),
388 maxNonExpandedBlockWidth,
389 maxNonExpandedBlockHeight,
391 blocksVOverlapSize ),
392 "Error calculating best block size" );
394 maxExpandedBlockWidth = maxNonExpandedBlockWidth +
395 blocksHOverlapSize + blocksHOverlapSize;
396 maxExpandedBlockHeight = maxNonExpandedBlockHeight +
397 blocksVOverlapSize + blocksVOverlapSize;
403 unsigned int blocksHOverlapSize = 0;
404 unsigned int blocksVOverlapSize = 0;
409 stratBlocksOverlapSize * stratBlocksOverlapSize,
413 maxNonExpandedBlockWidth,
414 maxNonExpandedBlockHeight,
416 blocksVOverlapSize ),
417 "Error calculating best block size" );
419 maxExpandedBlockWidth = maxNonExpandedBlockWidth;
420 maxExpandedBlockHeight = maxNonExpandedBlockHeight;
425 maxNonExpandedBlockWidth = maxExpandedBlockWidth =
427 maxNonExpandedBlockHeight = maxExpandedBlockHeight =
433 const unsigned int hBlocksNumber = (
unsigned int)ceil(
435 ((
double)maxNonExpandedBlockWidth) );
436 const unsigned int vBlocksNumber = (
unsigned int)ceil(
438 ((
double)maxNonExpandedBlockHeight ) );
443 std::vector< std::vector< unsigned int> > imageHorizontalProfiles;
444 std::vector< unsigned int > imageHorizontalProfilesCenterLines;
445 std::vector< std::vector< unsigned int> > imageVerticalProfiles;
446 std::vector< unsigned int > imageVerticalProfilesCenterLines;
453 const unsigned int tileHNeighborhoodSize = blocksHOverlapSize / 2;
454 const unsigned int tileVNeighborhoodSize = blocksVOverlapSize / 2;
455 const unsigned int pixelNeighborhoodSize = 5;
456 const unsigned int profileAntiSmoothingFactor = 3;
458 std::vector< unsigned int> profile;
459 unsigned int profileIdx = 0;
461 for( profileIdx = 1 ; profileIdx < hBlocksNumber ;
466 const unsigned int centerLine = ( ( profileIdx ) *
467 maxNonExpandedBlockHeight );
468 imageHorizontalProfilesCenterLines.push_back( centerLine );
472 pixelNeighborhoodSize, tileHNeighborhoodSize,
473 profileAntiSmoothingFactor, profile ) )
475 imageHorizontalProfiles.push_back( profile );
479 imageHorizontalProfiles.push_back( std::vector< unsigned int>() );
483 for( profileIdx = 1 ; profileIdx < vBlocksNumber ;
488 const unsigned int centerLine = ( ( profileIdx ) *
489 maxNonExpandedBlockWidth );
490 imageVerticalProfilesCenterLines.push_back( centerLine );
494 pixelNeighborhoodSize, tileVNeighborhoodSize,
495 profileAntiSmoothingFactor, profile ) )
497 imageVerticalProfiles.push_back( profile );
501 imageVerticalProfiles.push_back( std::vector< unsigned int>() );
521 hBlocksNumber ),
"Blocks matrix reset error" );
523 const int linesBound = (int)
525 const int colsBound = (int)
532 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
535 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
539 segmentsMatrixLine, segmentsMatrixCol );
541 blockXStart = ((int)( segmentsMatrixCol *
542 maxNonExpandedBlockWidth ) ) - ((
int)blocksHOverlapSize );
543 blockYStart = ((int)( segmentsMatrixLine *
544 maxNonExpandedBlockHeight ) ) - ((
int)blocksVOverlapSize );
545 blockXBound = blockXStart + ((int)maxExpandedBlockWidth);
546 blockYBound = blockYStart + ((int)maxExpandedBlockHeight);
550 segmentsBlock.
m_startX = (
unsigned int)
MAX( 0, blockXStart );
551 segmentsBlock.
m_startY = (
unsigned int)
MAX( 0, blockYStart );
553 segmentsBlock.
m_width = ((
unsigned int)
MIN( blockXBound,
554 colsBound ) ) - segmentsBlock.
m_startX;
555 segmentsBlock.
m_height = ((
unsigned int)
MIN( blockYBound,
556 linesBound ) ) - segmentsBlock.
m_startY;
567 imageHorizontalProfiles, imageVerticalProfiles,
568 segmentsBlock ),
"Block cutOff profiles update error" );
576 boost::mutex generalMutex;
577 boost::mutex inputRasterIOMutex;
578 boost::mutex outputRasterIOMutex;
579 boost::mutex blockProcessedSignalMutex;
581 volatile bool abortSegmentationFlag =
false;
585 boost::condition_variable blockProcessedSignal;
587 volatile unsigned int runningThreadsCounter = 0;
589 std::auto_ptr< te::common::TaskProgress > progressPtr;
591 ( ( vBlocksNumber * hBlocksNumber ) > 1 ) )
594 progressPtr->setTotalSteps( vBlocksNumber * hBlocksNumber );
595 progressPtr->setMessage(
"Segmentation" );
606 &outputRasterIOMutex;
608 &blockProcessedSignalMutex;
613 &runningThreadsCounter;
617 ( ( vBlocksNumber * hBlocksNumber ) == 1 );
618 segmenterThreadEntryParams.
m_progressPtr = maxSegThreads ? 0 : progressPtr.get();
625 runningThreadsCounter = maxSegThreads;
627 boost::thread_group threads;
629 for(
unsigned int threadIdx = 0 ; threadIdx < maxSegThreads ;
633 &segmenterThreadEntryParams ) );
638 while( (!abortSegmentationFlag) && (runningThreadsCounter > 0 ) )
640 boost::unique_lock<boost::mutex> lock( blockProcessedSignalMutex );
641 blockProcessedSignal.timed_wait( lock,
642 boost::posix_time::seconds( 1 ) );
644 if( progressPtr.get() )
646 int segmentedBlocksNmb = 0;
647 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
650 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
653 if( segmentsblocksMatrix[ segmentsMatrixLine ][ segmentsMatrixCol ].m_status
656 ++segmentedBlocksNmb;
661 if( segmentedBlocksNmb != progressPtr->getCurrentStep() )
663 progressPtr->pulse();
666 if( ! progressPtr->isActive() )
668 abortSegmentationFlag =
true;
688 runningThreadsCounter = 1;
692 return (!abortSegmentationFlag);
707 throw( te::rp::Exception )
716 "Invalid raster pointer" );
722 "Invalid raster bands number" );
724 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
730 "Invalid raster bands" );
745 const unsigned int totalImageLines,
746 const unsigned int totalImageCols,
747 const unsigned int minBlockPixels,
748 const unsigned int maxBlockPixels,
749 const double blocksHOverlapSizePercent,
750 const double blocksVOverlapSizePectent,
751 unsigned int& blockWidth,
752 unsigned int& blockHeight,
753 unsigned int& blocksHOverlapSize,
754 unsigned int& blocksVOverlapSize )
const
756 if( minBlockPixels > maxBlockPixels )
return false;
758 const double rasterRCFactor = ((double)totalImageLines) /
759 ((double)totalImageCols);
761 const double minRasterLinesNmb = sqrt( ((
double)minBlockPixels) *
764 const double maxScaleFactor = ((double)totalImageLines) /
767 unsigned int rescaledAndExtendedBlockPixelsNmb = 0;
769 for(
double scaleFactor = 1.0 ; scaleFactor <= maxScaleFactor ;
772 blockHeight = (
unsigned int)std::ceil( ((
double)totalImageLines) / scaleFactor );
773 blockWidth = (
unsigned int)std::ceil( ((
double)totalImageCols) / scaleFactor );
775 blocksHOverlapSize = (
unsigned int)( blocksHOverlapSizePercent * ((
double)blockWidth) );
776 blocksVOverlapSize = (
unsigned int)( blocksVOverlapSizePectent * ((
double)blockHeight) );
778 rescaledAndExtendedBlockPixelsNmb = ( blockHeight + blocksVOverlapSize +
779 blocksVOverlapSize ) * ( blockWidth + blocksHOverlapSize + blocksHOverlapSize );
781 if( rescaledAndExtendedBlockPixelsNmb <= maxBlockPixels )
822 boost::shared_ptr< SegmenterStrategy > strategyPtr(
826 "Unable to create an segmentation strategy" );
827 if( ! strategyPtr->initialize(
846 std::auto_ptr< te::rst::Raster > tempInRasterPtr;
847 std::auto_ptr< te::rst::Raster > tempOutRasterPtr;
852 std::vector< unsigned int > currentInRasterBands;
853 unsigned int currentOutRasterBand = 0;
855 for(
unsigned int sBMLine = 0 ; sBMLine <
858 for(
unsigned int sBMCol = 0 ; sBMCol <
899 if( ( currentInRasterPtr == 0 ) ||
907 std::vector< te::rst::BandProperty* > newInBandProperties;
909 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
915 inRasterBandsIdx ).getProperty() ) ) );
923 std::map< std::string, std::string > rInfo;
925 tempInRasterPtr.reset(
930 &newULC, oldGridPtr->
getSRID() ),
931 newInBandProperties, rInfo ) );
933 if( tempInRasterPtr.get() == 0 )
950 currentInRasterPtr = tempInRasterPtr.get();
952 currentInRasterBands.clear();
954 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
958 currentInRasterBands.push_back( inRasterBandsIdx );
969 unsigned int blkY = 0;
970 unsigned int blkX = 0;
971 unsigned int rasterY = 0;
976 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
983 inRasterBandsIdx ] );
985 tempInRasterPtr->getBand( inRasterBandsIdx );
987 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
991 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
995 outBandPtr->
setValue( blkX, blkY, value );
1005 if( ( currentOutRasterPtr == 0 ) ||
1016 std::vector< te::rst::BandProperty* > newBandProperties;
1017 newBandProperties.push_back( newBandPropertyPtr );
1024 std::map< std::string, std::string > rInfo;
1026 tempOutRasterPtr.reset(
1031 &newULC, oldGridPtr->
getSRID() ),
1032 newBandProperties, rInfo ) );
1034 if( tempOutRasterPtr.get() == 0 )
1053 currentOutRasterPtr = tempOutRasterPtr.get();
1054 currentOutRasterBand = 0;
1062 currentOutRasterBand = 0;
1068 if( ! strategyPtr->execute(
1070 *currentInRasterPtr, currentInRasterBands,
1073 *currentOutRasterPtr, currentOutRasterBand,
1096 unsigned int blkY = 0;
1097 unsigned int blkX = 0;
1098 unsigned int rasterY = 0;
1107 const bool hasTopCutOffProfile =
1109 const bool hasBottomCutOffProfile =
1111 const bool hasLeftCutOffProfile =
1113 const bool hasRightCutOffProfile =
1115 bool isDummyPixel =
false;
1118 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
1122 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
1124 isDummyPixel =
false;
1126 if( hasTopCutOffProfile )
1130 isDummyPixel =
true;
1133 if( hasBottomCutOffProfile )
1137 isDummyPixel =
true;
1140 if( hasLeftCutOffProfile )
1144 isDummyPixel =
true;
1147 if( hasRightCutOffProfile )
1151 isDummyPixel =
true;
1157 inBand.
getValue( blkX, blkY, value );
1166 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
1170 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
1172 inBand.
getValue( blkX, blkY, value );
1222 boost::lock_guard<boost::mutex> blockProcessedSignalLockGuard(
1251 const std::vector< unsigned int >& inRasterBands,
1252 const unsigned int pixelNeighborhoodSize,
1253 const unsigned int tileNeighborhoodSize,
1254 const unsigned int profileAntiSmoothingFactor,
1255 std::vector< unsigned int>& profile )
const
1258 "Invalid parameter" )
1262 if( tileNeighborhoodSize < pixelNeighborhoodSize )
return false;
1266 const int inRasterBandsSize = (int)inRasterBands.size();
1270 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterRowsNmb - 1,
1271 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1272 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterRowsNmb,
1273 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1274 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1275 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1278 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1279 (int)pixelNeighborhoodSize;
1280 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1281 (int)pixelNeighborhoodSize;
1283 int crossProfileIdx = 0;
1284 int crossProfileStartIdx = 0;
1285 int crossProfileBoundIdx = 0;
1287 int pixelNBStartIdx = 0;
1288 int pixelNBEndIdx = 0;
1289 int pixel1NBIdx = 0;
1290 int pixel2NBIdx = 0;
1291 int pixelNBOffset = 0;
1294 double currBandDiffSum = 0;
1295 double higherDiffSum = 0;
1296 int higherDiffSumIdx = 0;
1298 int inRasterBandsIdx = 0;
1299 unsigned int bandIdx = 0;
1301 double pixel1Value = 0;
1302 double pixel2Value = 0;
1304 profile.resize( inRasterColsNmb, 0 );
1306 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterColsNmb ;
1307 ++profileElementIdx )
1309 if( profileElementIdx )
1311 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1312 (int)profileAntiSmoothingFactor;
1313 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1314 minCrossProfileStartIdx );
1316 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1317 ((int)profileAntiSmoothingFactor);
1318 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1319 maxCrossProfileBoundIdx );
1323 crossProfileStartIdx = minCrossProfileStartIdx;
1324 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1328 higherDiffSumIdx = crossProfileStartIdx;
1330 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1331 crossProfileBoundIdx ; ++crossProfileIdx )
1338 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1339 ++inRasterBandsIdx )
1341 bandIdx = inRasterBands[ inRasterBandsIdx ];
1345 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1346 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1347 currBandDiffSum = 0;
1349 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1352 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1353 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1356 ( pixel1NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1358 ( pixel2NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1360 inRaster.
getValue( profileElementIdx,
1361 pixel1NBIdx, pixel1Value, bandIdx );
1362 inRaster.
getValue( profileElementIdx,
1363 pixel2NBIdx, pixel2Value, bandIdx );
1365 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1368 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1369 pixelNBStartIdx + 1 ) ) );
1372 if( diffSum > higherDiffSum )
1374 higherDiffSum = diffSum;
1375 higherDiffSumIdx = crossProfileIdx;
1379 profile[ profileElementIdx ] = higherDiffSumIdx;
1387 const std::vector< unsigned int >& inRasterBands,
1388 const unsigned int pixelNeighborhoodSize,
1389 const unsigned int tileNeighborhoodSize,
1390 const unsigned int profileAntiSmoothingFactor,
1391 std::vector< unsigned int>& profile )
const
1394 "Invalid parameter" )
1398 if( tileNeighborhoodSize < pixelNeighborhoodSize )
return false;
1402 const int inRasterBandsSize = (int)inRasterBands.size();
1406 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterColsNmb - 1,
1407 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1408 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterColsNmb,
1409 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1410 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1411 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1414 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1415 (int)pixelNeighborhoodSize;
1416 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1417 (int)pixelNeighborhoodSize;
1419 int crossProfileIdx = 0;
1420 int crossProfileStartIdx = 0;
1421 int crossProfileBoundIdx = 0;
1423 int pixelNBStartIdx = 0;
1424 int pixelNBEndIdx = 0;
1425 int pixel1NBIdx = 0;
1426 int pixel2NBIdx = 0;
1427 int pixelNBOffset = 0;
1430 double currBandDiffSum = 0;
1431 double higherDiffSum = 0;
1432 int higherDiffSumIdx = 0;
1434 int inRasterBandsIdx = 0;
1435 unsigned int bandIdx = 0;
1437 double pixel1Value = 0;
1438 double pixel2Value = 0;
1440 profile.resize( inRasterRowsNmb, 0 );
1442 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterRowsNmb ;
1443 ++profileElementIdx )
1445 if( profileElementIdx )
1447 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1448 (int)profileAntiSmoothingFactor;
1449 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1450 minCrossProfileStartIdx );
1452 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1453 ((int)profileAntiSmoothingFactor);
1454 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1455 maxCrossProfileBoundIdx );
1459 crossProfileStartIdx = minCrossProfileStartIdx;
1460 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1464 higherDiffSumIdx = crossProfileStartIdx;
1466 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1467 crossProfileBoundIdx ; ++crossProfileIdx )
1474 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1475 ++inRasterBandsIdx )
1477 bandIdx = inRasterBands[ inRasterBandsIdx ];
1481 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1482 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1483 currBandDiffSum = 0;
1485 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1488 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1489 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1492 ( pixel1NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1494 ( pixel2NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1496 inRaster.
getValue( pixel1NBIdx, profileElementIdx,
1497 pixel1Value, bandIdx );
1498 inRaster.
getValue( pixel2NBIdx, profileElementIdx,
1499 pixel2Value, bandIdx );
1501 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1504 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1505 pixelNBStartIdx + 1 ) ) );
1508 if( diffSum > higherDiffSum )
1510 higherDiffSum = diffSum;
1511 higherDiffSumIdx = crossProfileIdx;
1515 profile[ profileElementIdx ] = higherDiffSumIdx;
1522 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1523 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1532 imageHorizontalProfiles.size() ) )
1534 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1537 if( ! profile.empty() )
1540 int profElementValue = 0;
1542 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1546 profile.size(),
"Internal error" )
1548 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1550 profElementValue =
MAX( 0, profElementValue );
1551 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1563 imageHorizontalProfiles.size() )
1565 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1568 if( ! profile.empty() )
1571 int profElementValue = 0;
1573 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1577 profile.size(),
"Internal error" )
1579 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1581 profElementValue =
MAX( 0, profElementValue );
1582 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1595 imageVerticalProfiles.size() ) )
1597 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1600 if( ! profile.empty() )
1603 int profElementValue = 0;
1605 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1609 profile.size(),
"Internal error" )
1611 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1613 profElementValue =
MAX( 0, profElementValue );
1614 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1626 imageVerticalProfiles.size() )
1628 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1631 if( ! profile.empty() )
1634 int profElementValue = 0;
1636 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1640 profile.size(),
"Internal error" )
1642 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1644 profElementValue =
MAX( 0, profElementValue );
1645 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1656 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1657 const std::vector< unsigned int >& imageHorizontalProfilesCenterLines,
1658 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1659 const std::vector< unsigned int >& imageVerticalProfilesCenterLines,
1660 const std::string& filename )
1663 imageHorizontalProfilesCenterLines.size(),
"Internal error" )
1665 imageVerticalProfilesCenterLines.size(),
"Internal error" )
1667 std::vector< te::rst::BandProperty* > bandsProperties;
1672 bandsProperties[ 0 ]->m_noDataValue = 0;
1676 std::map< std::string, std::string > rInfo;
1677 rInfo[
"URI" ] =
"cutofflines.tif";
1679 std::auto_ptr< te::rst::Raster > outRasterPtr(
1682 bandsProperties, rInfo ) );
1684 "Tiff creation error" )
1687 for(
unsigned int profIdx = 0 ; profIdx < imageHorizontalProfiles.size() ;
1690 const std::vector< unsigned int>& profile =
1691 imageHorizontalProfiles[ profIdx ];
1693 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1695 const unsigned int centerLineIdx =
1696 imageHorizontalProfilesCenterLines[ profIdx ];
1698 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1701 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1705 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1707 outRasterPtr->setValue( eIdx, centerLineIdx, 255, 0 );
1708 outRasterPtr->setValue( eIdx, profile[ eIdx ] , 255, 0 );
1714 for(
unsigned int profIdx = 0 ; profIdx < imageVerticalProfiles.size() ;
1717 const std::vector< unsigned int>& profile =
1718 imageVerticalProfiles[ profIdx ];
1720 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1722 const unsigned int centerLineIdx =
1723 imageVerticalProfilesCenterLines[ profIdx ];
1725 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1727 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1731 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1733 outRasterPtr->setValue( centerLineIdx, eIdx, 255, 0 );
1734 outRasterPtr->setValue( profile[ eIdx ], eIdx, 255, 0 );
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
bool isActive() const
Verify if the task is active.
bool m_enableProgress
Enable/Disable the progress interface (default:false).
virtual AbstractParameters * clone() const =0
Create a clone copy of this instance.
std::vector< unsigned int > m_topCutOffProfile
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0)*/.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
static void segmenterThreadEntry(SegmenterThreadEntryParams *paramsPtr)
Segmenter thread entry.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
ColorInterp m_colorInterp
The color interpretation.
Raster Processing functions.
Segmenter segments block description class.
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)
Grid * getGrid()
It returns the raster grid.
Segmenter Input Parameters.
boost::mutex * m_outputRasterIOMutexPtr
Pointer to the mutex used when accessing the output raster (default:0)*/.
~SegmenterThreadEntryParams()
bool m_enableBlockMerging
If true, a block merging procedure will be performed (default:true).
The parameters passed to the Segmenter::segmenterthreadEntry method.
static Raster * make()
It creates and returns an empty raster with default raster driver.
te::rst::Raster const * m_inputRasterPtr
Input raster.
unsigned int getLinesNumber() const
The number of current matrix lines.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
Segmenter Output Parameters.
unsigned int GetPhysProcNumber()
Returns the number of physical processors.
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
std::vector< double > const * m_inputRasterGainsPtr
Pointer to a vector of input raster bands gain values */.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
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.
const Segmenter::InputParameters & operator=(const Segmenter::InputParameters ¶ms)
static te::rp::SegmenterStrategy * make(const std::string &factoryKey)
It creates an object with the appropriated factory.
An utility struct for representing 2D coordinates.
#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 m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Abstract parameters base interface.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
AbstractParameters * clone() const
Create a clone copy of this instance.
Segmenter::InputParameters m_inputParameters
Segmenter execution parameters.
A raster band description.
A rectified grid is the spatial support for raster data.
std::vector< double > const * m_inputRasterOffsetsPtr
Pointer to a vector of input raster bands offset values */.
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
#define ABS(x)
Absolute value.
virtual void setValue(unsigned int c, unsigned int r, const double value)=0
Sets the cell attribute value.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Segmenter segments IDs manager.
SegmenterIdsManager * m_segmentsIdsManagerPtr
Pointer to the segments Ids manager - (default 0) */.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
SegmenterStrategyParameters * m_segStratParamsPtr
A pointer to the internal specific segmenter strategy parameters or NULL if no parameters are present...
std::vector< unsigned int > m_rightCutOffProfile
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.
void reset()
Clear all internal allocated resources and reset the parameters instance to its initial state...
bool m_enableStrategyProgress
Enable/Disable the segmentation strategy to use its own progress interface (default:false). */.
const Segmenter::OutputParameters & operator=(const Segmenter::OutputParameters ¶ms)
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
unsigned int m_segmentsMatrixYIndex
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Segmenter Strategy Parameters.
unsigned int volatile * m_runningThreadsCounterPtr
Pointer to the running threads counter - default 0) */.
boost::mutex * m_inputRasterIOMutexPtr
Pointer to the mutex used when accessing the input raster (default:0)*/.
SegmentsBlocksMatrixT * m_segsBlocksMatrixPtr
Pointer to the segments blocks matrix (default:0)*/.
unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
const Algorithm & operator=(const Algorithm &)
Raster segmenter strategy factory base class.
boost::mutex * m_generalMutexPtr
Pointer to a general global mutex (default:0)*/.
A raster band description.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
#define MIN(a, b)
Macro that returns min between two values.
A generic template matrix.
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
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..
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
std::vector< unsigned int > m_bottomCutOffProfile
bool calcBestBlockSize(const unsigned int totalImageLines, const unsigned int totalImageCols, const unsigned int minBlockPixels, const unsigned int maxBlockPixels, const double blocksHOverlapSizePercent, const double blocksVOverlapSizePectent, unsigned int &blockWidth, unsigned int &blockHeight, unsigned int &blocksHOverlapSize, unsigned int &blocksVOverlapSize) const
Calc the best sub-image block size for each thread to process.
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..
unsigned int getColumnsNumber() const
The number of current matrix columns.
void reset()
Clear all internal allocated objects and reset the algorithm to its initial state.
Segmenter::InputParameters const * m_inputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
Raster Processing algorithm output parameters base interface.
boost::mutex * m_blockProcessedSignalMutexPtr
Pointer to the mutex used by the block processed signal (default:0)*/.
bool m_instanceInitialized
AbstractParameters * clone() const
Create a clone copy of this instance.
void reset()
Reset (clear) the active instance data.
An abstract class for raster data strucutures.
te::common::TaskProgress * m_progressPtr
A pointer to an active task progress tha should be pulsed for each processed block or a null pointer ...
BandProperty * getProperty()
Returns the band property.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
This class can be used to inform the progress of a task.
unsigned int m_segmentsMatrixXIndex
SegmenterThreadEntryParams()
unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
int getSRID() const
Returns the grid spatial reference system identifier.
unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...
unsigned int m_maxBlockSize
The input image will be split into blocks with this width for processing, this parameter tells the ma...
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
SegmenterStrategyParameters const * getSegStrategyParams() const
Returns a pointer to the internal specific segmenter strategy parameters.
boost::condition_variable * m_blockProcessedSignalPtr
Pointer to a signal to be emited when a segments block was processed (default:0)*/.
Index into a lookup table.
#define MAX(a, b)
Macro that returns max between two values.
std::vector< unsigned int > m_leftCutOffProfile
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
Raster Processing algorithm input parameters base interface.