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 "../memory/CachedRaster.h"
39 #include "../dataaccess/datasource/DataSource.h"
40 #include "../common/PlatformUtils.h"
41 #include "../common/StringUtils.h"
42 #include "../geometry/Coord2D.h"
43 #include "../datatype/Enums.h"
63 m_segStratParamsPtr = 0;
76 m_inputRasterBands.clear();
77 m_enableThreadedProcessing =
true;
79 m_enableBlockProcessing =
true;
80 m_enableBlockMerging =
true;
82 m_strategyName.clear();
83 m_enableProgress =
false;
84 m_enableRasterCache =
true;
86 if( m_segStratParamsPtr )
88 delete m_segStratParamsPtr;
89 m_segStratParamsPtr = 0;
124 if( m_segStratParamsPtr )
126 delete m_segStratParamsPtr;
127 m_segStratParamsPtr = 0;
136 return m_segStratParamsPtr;
159 m_outputRasterPtr.reset();
180 m_inputParametersPtr = 0;
181 m_outputParametersPtr = 0;
182 m_segsBlocksMatrixPtr = 0;
183 m_generalMutexPtr = 0;
184 m_inputRasterIOMutexPtr = 0;
185 m_outputRasterIOMutexPtr = 0;
186 m_blockProcessedSignalMutexPtr = 0;
187 m_abortSegmentationFlagPtr = 0;
188 m_segmentsIdsManagerPtr = 0;
189 m_blockProcessedSignalPtr = 0;
190 m_runningThreadsCounterPtr = 0;
191 m_inputRasterGainsPtr = 0;
192 m_inputRasterOffsetsPtr = 0;
193 m_enableStrategyProgress =
false;
211 throw( te::rp::Exception )
222 std::vector< te::rst::BandProperty* > bandsProperties;
227 bandsProperties[ 0 ]->m_noDataValue = 0;
239 "Output raster creation error" );
244 std::auto_ptr< SegmenterStrategy > strategyPtr(
247 "Unable to create an segmentation strategy" );
250 "Unable to initialize the segmentation strategy" );
252 const double stratMemUsageEstimation = strategyPtr->getMemUsageEstimation(
257 "Invalid strategy memory usage factorMemUsageFactor" );
259 const unsigned stratBlocksOverlapSize =
260 strategyPtr->getOptimalBlocksOverlapSize();
264 const unsigned int totalRasterPixels =
267 const double originalRasterDataMemUsageEstimation = (double)(
274 const double freeVMem =
MIN( totalPhysMem, ( totalVMem - usedVMem ) );
275 const double pixelRequiredRam =
276 ( originalRasterDataMemUsageEstimation + stratMemUsageEstimation )
277 / ((
double)totalRasterPixels);
278 const double maxSimultaneousMemoryPixels = 0.7 *
MIN(
279 ((
double)totalRasterPixels),
280 freeVMem / pixelRequiredRam );
284 std::auto_ptr< te::rst::Raster > cachedRasterHandler;
291 cachedRasterPtr = cachedRasterHandler.get();
296 std::vector< double > inputRasterGains(
298 std::vector< double > inputRasterOffsets(
302 const unsigned int nRows =
304 const unsigned int nCols =
306 unsigned int row = 0;
307 unsigned int col = 0;
308 double bandMin = DBL_MAX;
309 double bandMax = -1.0 * DBL_MAX;
312 for(
unsigned int inputRasterBandsIdx = 0 ; inputRasterBandsIdx <
319 bandMax = -1.0 * DBL_MAX;
321 for( row = 0 ; row < nRows ; ++row )
322 for( col = 0 ; col < nCols ; ++col )
326 if( bandMin > value ) bandMin = value;
327 if( bandMax < value ) bandMax = value;
330 if( bandMax != bandMin )
332 inputRasterGains[ inputRasterBandsIdx ] = 1.0 / ( bandMax - bandMin );
333 inputRasterOffsets[ inputRasterBandsIdx ] = -1.0 * bandMin;
340 unsigned int maxSegThreads = 0;
350 unsigned int maxNonExpandedBlockWidth = 0;
351 unsigned int maxNonExpandedBlockHeight = 0;
352 unsigned int maxExpandedBlockWidth = 0;
353 unsigned int maxExpandedBlockHeight = 0;
354 unsigned int blocksHOverlapSize = 0;
355 unsigned int blocksVOverlapSize = 0;
360 ( maxSegThreads > 0 )
362 ( maxSimultaneousMemoryPixels < ((
double)totalRasterPixels ) )
379 unsigned int maxBlockPixels = 0;
388 maxBlockPixels =
static_cast<unsigned int>(
389 ( maxSimultaneousMemoryPixels /
390 (
static_cast<double>( maxSegThreads ? maxSegThreads : 1 ) ) ) );
403 ( stratBlocksOverlapSize + stratBlocksOverlapSize + 1 )
405 ( stratBlocksOverlapSize + stratBlocksOverlapSize + 1 )
408 stratBlocksOverlapSize,
409 stratBlocksOverlapSize,
410 maxNonExpandedBlockWidth,
411 maxNonExpandedBlockHeight ),
412 "Error calculating best block size" );
414 maxExpandedBlockWidth = maxNonExpandedBlockWidth +
415 stratBlocksOverlapSize + stratBlocksOverlapSize;
416 maxExpandedBlockHeight = maxNonExpandedBlockHeight +
417 stratBlocksOverlapSize + stratBlocksOverlapSize;
426 stratBlocksOverlapSize * stratBlocksOverlapSize,
430 maxNonExpandedBlockWidth,
431 maxNonExpandedBlockHeight ),
432 "Error calculating best block size" );
434 maxExpandedBlockWidth = maxNonExpandedBlockWidth;
435 maxExpandedBlockHeight = maxNonExpandedBlockHeight;
440 maxNonExpandedBlockWidth = maxExpandedBlockWidth =
442 maxNonExpandedBlockHeight = maxExpandedBlockHeight =
448 const unsigned int hBlocksNumber = (
unsigned int)ceil(
450 ((
double)maxNonExpandedBlockWidth) );
451 const unsigned int vBlocksNumber = (
unsigned int)ceil(
453 ((
double)maxNonExpandedBlockHeight ) );
458 std::vector< std::vector< unsigned int> > imageHorizontalProfiles;
459 std::vector< unsigned int > imageHorizontalProfilesCenterLines;
460 std::vector< std::vector< unsigned int> > imageVerticalProfiles;
461 std::vector< unsigned int > imageVerticalProfilesCenterLines;
468 const unsigned int tileHNeighborhoodSize = blocksHOverlapSize / 2;
469 const unsigned int tileVNeighborhoodSize = blocksVOverlapSize / 2;
470 const unsigned int pixelNeighborhoodSize = 5;
471 const unsigned int profileAntiSmoothingFactor = 3;
473 std::vector< unsigned int> profile;
474 unsigned int profileIdx = 0;
476 for( profileIdx = 1 ; profileIdx < hBlocksNumber ;
481 const unsigned int centerLine = ( ( profileIdx ) *
482 maxNonExpandedBlockHeight );
483 imageHorizontalProfilesCenterLines.push_back( centerLine );
487 pixelNeighborhoodSize, tileHNeighborhoodSize,
488 profileAntiSmoothingFactor, profile ) )
490 imageHorizontalProfiles.push_back( profile );
494 imageHorizontalProfiles.push_back( std::vector< unsigned int>() );
498 for( profileIdx = 1 ; profileIdx < vBlocksNumber ;
503 const unsigned int centerLine = ( ( profileIdx ) *
504 maxNonExpandedBlockWidth );
505 imageVerticalProfilesCenterLines.push_back( centerLine );
509 pixelNeighborhoodSize, tileVNeighborhoodSize,
510 profileAntiSmoothingFactor, profile ) )
512 imageVerticalProfiles.push_back( profile );
516 imageVerticalProfiles.push_back( std::vector< unsigned int>() );
536 hBlocksNumber ),
"Blocks matrix reset error" );
538 const int linesBound = (int)
540 const int colsBound = (int)
547 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
550 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
554 segmentsMatrixLine, segmentsMatrixCol );
556 blockXStart = ((int)( segmentsMatrixCol *
557 maxNonExpandedBlockWidth ) ) - ((
int)blocksHOverlapSize );
558 blockYStart = ((int)( segmentsMatrixLine *
559 maxNonExpandedBlockHeight ) ) - ((
int)blocksVOverlapSize );
560 blockXBound = blockXStart + ((int)maxExpandedBlockWidth);
561 blockYBound = blockYStart + ((int)maxExpandedBlockHeight);
565 segmentsBlock.
m_startX = (
unsigned int)
MAX( 0, blockXStart );
566 segmentsBlock.
m_startY = (
unsigned int)
MAX( 0, blockYStart );
568 segmentsBlock.
m_width = ((
unsigned int)
MIN( blockXBound,
569 colsBound ) ) - segmentsBlock.
m_startX;
570 segmentsBlock.
m_height = ((
unsigned int)
MIN( blockYBound,
571 linesBound ) ) - segmentsBlock.
m_startY;
582 imageHorizontalProfiles, imageVerticalProfiles,
583 segmentsBlock ),
"Block cutOff profiles update error" );
592 cachedRasterHandler.reset();
597 boost::mutex generalMutex;
598 boost::mutex inputRasterIOMutex;
599 boost::mutex outputRasterIOMutex;
600 boost::mutex blockProcessedSignalMutex;
602 volatile bool abortSegmentationFlag =
false;
606 boost::condition_variable blockProcessedSignal;
608 volatile unsigned int runningThreadsCounter = 0;
610 std::auto_ptr< te::common::TaskProgress > progressPtr;
612 ( ( vBlocksNumber * hBlocksNumber ) > 1 ) )
615 progressPtr->setTotalSteps( vBlocksNumber * hBlocksNumber );
616 progressPtr->setMessage(
"Segmentation" );
627 &outputRasterIOMutex;
629 &blockProcessedSignalMutex;
634 &runningThreadsCounter;
638 ( ( vBlocksNumber * hBlocksNumber ) == 1 );
639 segmenterThreadEntryParams.
m_progressPtr = maxSegThreads ? 0 : progressPtr.get();
646 runningThreadsCounter = maxSegThreads;
648 boost::thread_group threads;
650 for(
unsigned int threadIdx = 0 ; threadIdx < maxSegThreads ;
654 &segmenterThreadEntryParams ) );
659 while( (!abortSegmentationFlag) && (runningThreadsCounter > 0 ) )
661 boost::unique_lock<boost::mutex> lock( blockProcessedSignalMutex );
662 blockProcessedSignal.timed_wait( lock,
663 boost::posix_time::seconds( 1 ) );
665 if( progressPtr.get() )
667 int segmentedBlocksNmb = 0;
668 for(
unsigned int segmentsMatrixLine = 0 ; segmentsMatrixLine <
671 for(
unsigned int segmentsMatrixCol = 0 ; segmentsMatrixCol <
674 if( segmentsblocksMatrix[ segmentsMatrixLine ][ segmentsMatrixCol ].m_status
677 ++segmentedBlocksNmb;
682 if( segmentedBlocksNmb != progressPtr->getCurrentStep() )
684 progressPtr->pulse();
687 if( ! progressPtr->isActive() )
689 abortSegmentationFlag =
true;
709 runningThreadsCounter = 1;
713 return (!abortSegmentationFlag);
728 throw( te::rp::Exception )
737 "Invalid raster pointer" );
743 "Invalid raster bands number" );
745 for(
unsigned int inRasterBandsIdx = 0 ; inRasterBandsIdx <
751 "Invalid raster bands" );
766 const unsigned int totalImageLines,
767 const unsigned int totalImageCols,
768 const unsigned int minExapandedBlockPixels,
769 const unsigned int maxExapandedBlockPixels,
770 const unsigned int blocksHOverlapSize,
771 const unsigned int blocksVOverlapSize,
772 unsigned int& nonExpandedBlockWidth,
773 unsigned int& nonExpandedBlockHeight )
const
775 if( minExapandedBlockPixels > maxExapandedBlockPixels )
return false;
777 const double maxScaleFactor =
778 ((double)(totalImageLines * totalImageCols))
780 ((
double)minExapandedBlockPixels);
782 unsigned int rescaledAndExtendedBlockPixelsNmb = 0;
784 for(
double scaleFactor = 1.0 ; scaleFactor <= maxScaleFactor ;
787 nonExpandedBlockHeight = (
unsigned int)std::ceil( ((
double)totalImageLines) / scaleFactor );
788 nonExpandedBlockWidth = (
unsigned int)std::ceil( ((
double)totalImageCols) / scaleFactor );
790 rescaledAndExtendedBlockPixelsNmb = ( nonExpandedBlockHeight + blocksVOverlapSize +
791 blocksVOverlapSize ) * ( nonExpandedBlockWidth + blocksHOverlapSize + blocksHOverlapSize );
793 if( rescaledAndExtendedBlockPixelsNmb <= maxExapandedBlockPixels )
834 boost::shared_ptr< SegmenterStrategy > strategyPtr(
838 "Unable to create an segmentation strategy" );
839 if( ! strategyPtr->initialize(
858 std::auto_ptr< te::rst::Raster > tempInRasterPtr;
859 std::auto_ptr< te::rst::Raster > tempOutRasterPtr;
864 std::vector< unsigned int > currentInRasterBands;
865 unsigned int currentOutRasterBand = 0;
867 for(
unsigned int sBMLine = 0 ; sBMLine <
870 for(
unsigned int sBMCol = 0 ; sBMCol <
911 if( ( currentInRasterPtr == 0 ) ||
919 std::vector< te::rst::BandProperty* > newInBandProperties;
921 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
927 inRasterBandsIdx ).getProperty() ) ) );
935 std::map< std::string, std::string > rInfo;
937 tempInRasterPtr.reset(
942 &newULC, oldGridPtr->
getSRID() ),
943 newInBandProperties, rInfo ) );
945 if( tempInRasterPtr.get() == 0 )
962 currentInRasterPtr = tempInRasterPtr.get();
964 currentInRasterBands.clear();
966 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
970 currentInRasterBands.push_back( inRasterBandsIdx );
981 unsigned int blkY = 0;
982 unsigned int blkX = 0;
983 unsigned int rasterY = 0;
988 for(
unsigned int inRasterBandsIdx = 0; inRasterBandsIdx <
995 inRasterBandsIdx ] );
997 tempInRasterPtr->getBand( inRasterBandsIdx );
999 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
1003 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
1007 outBandPtr->
setValue( blkX, blkY, value );
1017 if( ( currentOutRasterPtr == 0 ) ||
1028 std::vector< te::rst::BandProperty* > newBandProperties;
1029 newBandProperties.push_back( newBandPropertyPtr );
1036 std::map< std::string, std::string > rInfo;
1038 tempOutRasterPtr.reset(
1043 &newULC, oldGridPtr->
getSRID() ),
1044 newBandProperties, rInfo ) );
1046 if( tempOutRasterPtr.get() == 0 )
1065 currentOutRasterPtr = tempOutRasterPtr.get();
1066 currentOutRasterBand = 0;
1074 currentOutRasterBand = 0;
1080 if( ! strategyPtr->execute(
1082 *currentInRasterPtr, currentInRasterBands,
1085 *currentOutRasterPtr, currentOutRasterBand,
1108 unsigned int blkY = 0;
1109 unsigned int blkX = 0;
1110 unsigned int rasterY = 0;
1119 const bool hasTopCutOffProfile =
1121 const bool hasBottomCutOffProfile =
1123 const bool hasLeftCutOffProfile =
1125 const bool hasRightCutOffProfile =
1127 bool isDummyPixel =
false;
1130 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
1134 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
1136 isDummyPixel =
false;
1138 if( hasTopCutOffProfile )
1142 isDummyPixel =
true;
1145 if( hasBottomCutOffProfile )
1149 isDummyPixel =
true;
1152 if( hasLeftCutOffProfile )
1156 isDummyPixel =
true;
1159 if( hasRightCutOffProfile )
1163 isDummyPixel =
true;
1169 inBand.
getValue( blkX, blkY, value );
1178 for( blkY = 0 ; blkY < segsBlk.
m_height ; ++blkY )
1182 for( blkX = 0 ; blkX < segsBlk.
m_width ; ++blkX )
1184 inBand.
getValue( blkX, blkY, value );
1234 boost::lock_guard<boost::mutex> blockProcessedSignalLockGuard(
1263 const std::vector< unsigned int >& inRasterBands,
1264 const unsigned int pixelNeighborhoodSize,
1265 const unsigned int tileNeighborhoodSize,
1266 const unsigned int profileAntiSmoothingFactor,
1267 std::vector< unsigned int>& profile )
const
1270 "Invalid parameter" )
1274 if( tileNeighborhoodSize < pixelNeighborhoodSize )
return false;
1278 const int inRasterBandsSize = (int)inRasterBands.size();
1282 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterRowsNmb - 1,
1283 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1284 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterRowsNmb,
1285 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1286 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1287 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1290 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1291 (int)pixelNeighborhoodSize;
1292 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1293 (int)pixelNeighborhoodSize;
1295 int crossProfileIdx = 0;
1296 int crossProfileStartIdx = 0;
1297 int crossProfileBoundIdx = 0;
1299 int pixelNBStartIdx = 0;
1300 int pixelNBEndIdx = 0;
1301 int pixel1NBIdx = 0;
1302 int pixel2NBIdx = 0;
1303 int pixelNBOffset = 0;
1306 double currBandDiffSum = 0;
1307 double higherDiffSum = 0;
1308 int higherDiffSumIdx = 0;
1310 int inRasterBandsIdx = 0;
1311 unsigned int bandIdx = 0;
1313 double pixel1Value = 0;
1314 double pixel2Value = 0;
1316 profile.resize( inRasterColsNmb, 0 );
1318 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterColsNmb ;
1319 ++profileElementIdx )
1321 if( profileElementIdx )
1323 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1324 (int)profileAntiSmoothingFactor;
1325 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1326 minCrossProfileStartIdx );
1328 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1329 ((int)profileAntiSmoothingFactor);
1330 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1331 maxCrossProfileBoundIdx );
1335 crossProfileStartIdx = minCrossProfileStartIdx;
1336 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1340 higherDiffSumIdx = crossProfileStartIdx;
1342 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1343 crossProfileBoundIdx ; ++crossProfileIdx )
1350 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1351 ++inRasterBandsIdx )
1353 bandIdx = inRasterBands[ inRasterBandsIdx ];
1357 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1358 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1359 currBandDiffSum = 0;
1361 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1364 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1365 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1368 ( pixel1NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1370 ( pixel2NBIdx < inRasterRowsNmb ),
"Invalid pixel2Idx" )
1372 inRaster.
getValue( profileElementIdx,
1373 pixel1NBIdx, pixel1Value, bandIdx );
1374 inRaster.
getValue( profileElementIdx,
1375 pixel2NBIdx, pixel2Value, bandIdx );
1377 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1380 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1381 pixelNBStartIdx + 1 ) ) );
1384 if( diffSum > higherDiffSum )
1386 higherDiffSum = diffSum;
1387 higherDiffSumIdx = crossProfileIdx;
1391 profile[ profileElementIdx ] = higherDiffSumIdx;
1399 const std::vector< unsigned int >& inRasterBands,
1400 const unsigned int pixelNeighborhoodSize,
1401 const unsigned int tileNeighborhoodSize,
1402 const unsigned int profileAntiSmoothingFactor,
1403 std::vector< unsigned int>& profile )
const
1406 "Invalid parameter" )
1410 if( tileNeighborhoodSize < pixelNeighborhoodSize )
return false;
1414 const int inRasterBandsSize = (int)inRasterBands.size();
1418 const int tilesBufferStartIdx =
MAX( 0,
MIN( inRasterColsNmb - 1,
1419 ((
int)profileCenter) - ((
int)tileNeighborhoodSize) ) );
1420 const int tilesBufferBoundIdx =
MAX( 0,
MIN( inRasterColsNmb,
1421 1 + ((
int)profileCenter) + ((
int)tileNeighborhoodSize) ) );
1422 const int tilesBufferSize = tilesBufferBoundIdx - tilesBufferStartIdx;
1423 if( tilesBufferSize < ( 1 + ( 2 * ( (
int)( pixelNeighborhoodSize ) ) ) ) )
1426 const int minCrossProfileStartIdx = tilesBufferStartIdx +
1427 (int)pixelNeighborhoodSize;
1428 const int maxCrossProfileBoundIdx = tilesBufferBoundIdx -
1429 (int)pixelNeighborhoodSize;
1431 int crossProfileIdx = 0;
1432 int crossProfileStartIdx = 0;
1433 int crossProfileBoundIdx = 0;
1435 int pixelNBStartIdx = 0;
1436 int pixelNBEndIdx = 0;
1437 int pixel1NBIdx = 0;
1438 int pixel2NBIdx = 0;
1439 int pixelNBOffset = 0;
1442 double currBandDiffSum = 0;
1443 double higherDiffSum = 0;
1444 int higherDiffSumIdx = 0;
1446 int inRasterBandsIdx = 0;
1447 unsigned int bandIdx = 0;
1449 double pixel1Value = 0;
1450 double pixel2Value = 0;
1452 profile.resize( inRasterRowsNmb, 0 );
1454 for(
int profileElementIdx = 0 ; profileElementIdx < inRasterRowsNmb ;
1455 ++profileElementIdx )
1457 if( profileElementIdx )
1459 crossProfileStartIdx = profile[ profileElementIdx - 1 ] -
1460 (int)profileAntiSmoothingFactor;
1461 crossProfileStartIdx =
MAX( crossProfileStartIdx,
1462 minCrossProfileStartIdx );
1464 crossProfileBoundIdx = profile[ profileElementIdx - 1 ] + 1 +
1465 ((int)profileAntiSmoothingFactor);
1466 crossProfileBoundIdx =
MIN( crossProfileBoundIdx,
1467 maxCrossProfileBoundIdx );
1471 crossProfileStartIdx = minCrossProfileStartIdx;
1472 crossProfileBoundIdx = maxCrossProfileBoundIdx;
1476 higherDiffSumIdx = crossProfileStartIdx;
1478 for( crossProfileIdx = crossProfileStartIdx ; crossProfileIdx <
1479 crossProfileBoundIdx ; ++crossProfileIdx )
1486 for( inRasterBandsIdx = 0 ; inRasterBandsIdx < inRasterBandsSize ;
1487 ++inRasterBandsIdx )
1489 bandIdx = inRasterBands[ inRasterBandsIdx ];
1493 pixelNBStartIdx = crossProfileIdx - ( (
int)pixelNeighborhoodSize);
1494 pixelNBEndIdx = crossProfileIdx + ( (int)pixelNeighborhoodSize);
1495 currBandDiffSum = 0;
1497 for( pixelNBOffset = 0 ; pixelNBOffset < ((int)pixelNeighborhoodSize) ;
1500 pixel1NBIdx = pixelNBStartIdx + pixelNBOffset;
1501 pixel2NBIdx = pixelNBEndIdx - pixelNBOffset;
1504 ( pixel1NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1506 ( pixel2NBIdx < inRasterColsNmb ),
"Invalid pixel2Idx" )
1508 inRaster.
getValue( pixel1NBIdx, profileElementIdx,
1509 pixel1Value, bandIdx );
1510 inRaster.
getValue( pixel2NBIdx, profileElementIdx,
1511 pixel2Value, bandIdx );
1513 currBandDiffSum +=
ABS( pixel1Value - pixel2Value );
1516 diffSum += ( currBandDiffSum / ((double)( pixelNBEndIdx -
1517 pixelNBStartIdx + 1 ) ) );
1520 if( diffSum > higherDiffSum )
1522 higherDiffSum = diffSum;
1523 higherDiffSumIdx = crossProfileIdx;
1527 profile[ profileElementIdx ] = higherDiffSumIdx;
1534 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1535 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1544 imageHorizontalProfiles.size() ) )
1546 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1549 if( ! profile.empty() )
1552 int profElementValue = 0;
1554 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1558 profile.size(),
"Internal error" )
1560 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1562 profElementValue =
MAX( 0, profElementValue );
1563 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1575 imageHorizontalProfiles.size() )
1577 const std::vector< unsigned int >& profile = imageHorizontalProfiles[
1580 if( ! profile.empty() )
1583 int profElementValue = 0;
1585 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_width ;
1589 profile.size(),
"Internal error" )
1591 ((
int)profile[ profEIdx + segmentsBlock.
m_startX ]) -
1593 profElementValue =
MAX( 0, profElementValue );
1594 profElementValue =
MIN( (
int)segmentsBlock.
m_height, profElementValue );
1607 imageVerticalProfiles.size() ) )
1609 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1612 if( ! profile.empty() )
1615 int profElementValue = 0;
1617 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1621 profile.size(),
"Internal error" )
1623 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1625 profElementValue =
MAX( 0, profElementValue );
1626 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1638 imageVerticalProfiles.size() )
1640 const std::vector< unsigned int >& profile = imageVerticalProfiles[
1643 if( ! profile.empty() )
1646 int profElementValue = 0;
1648 for(
unsigned int profEIdx = 0 ; profEIdx < segmentsBlock.
m_height ;
1652 profile.size(),
"Internal error" )
1654 ((
int)profile[ profEIdx + segmentsBlock.
m_startY ]) -
1656 profElementValue =
MAX( 0, profElementValue );
1657 profElementValue =
MIN( (
int)segmentsBlock.
m_width, profElementValue );
1668 const std::vector< std::vector< unsigned int> >& imageHorizontalProfiles,
1669 const std::vector< unsigned int >& imageHorizontalProfilesCenterLines,
1670 const std::vector< std::vector< unsigned int> >& imageVerticalProfiles,
1671 const std::vector< unsigned int >& imageVerticalProfilesCenterLines,
1672 const std::string& filename )
1675 imageHorizontalProfilesCenterLines.size(),
"Internal error" )
1677 imageVerticalProfilesCenterLines.size(),
"Internal error" )
1679 std::vector< te::rst::BandProperty* > bandsProperties;
1684 bandsProperties[ 0 ]->m_noDataValue = 0;
1688 std::map< std::string, std::string > rInfo;
1689 rInfo[
"URI" ] =
"cutofflines.tif";
1691 std::auto_ptr< te::rst::Raster > outRasterPtr(
1694 bandsProperties, rInfo ) );
1696 "Tiff creation error" )
1699 for(
unsigned int profIdx = 0 ; profIdx < imageHorizontalProfiles.size() ;
1702 const std::vector< unsigned int>& profile =
1703 imageHorizontalProfiles[ profIdx ];
1705 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1707 const unsigned int centerLineIdx =
1708 imageHorizontalProfilesCenterLines[ profIdx ];
1710 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1713 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1717 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1719 outRasterPtr->setValue( eIdx, centerLineIdx, 255, 0 );
1720 outRasterPtr->setValue( eIdx, profile[ eIdx ] , 255, 0 );
1726 for(
unsigned int profIdx = 0 ; profIdx < imageVerticalProfiles.size() ;
1729 const std::vector< unsigned int>& profile =
1730 imageVerticalProfiles[ profIdx ];
1732 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1734 const unsigned int centerLineIdx =
1735 imageVerticalProfilesCenterLines[ profIdx ];
1737 outRasterPtr->getGrid()->getNumberOfColumns(),
"Internal error" )
1739 for(
unsigned int eIdx = 0 ; eIdx < profile.size() ;
1743 outRasterPtr->getGrid()->getNumberOfRows(),
"Internal error" )
1745 outRasterPtr->setValue( centerLineIdx, eIdx, 255, 0 );
1746 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.
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) */.
std::vector< double > const * m_inputRasterGainsPtr
Pointer to a vector of input raster bands gain values */.
A raster band description.
int getSRID() const
Returns the grid spatial reference system identifier.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
boost::mutex * m_outputRasterIOMutexPtr
Pointer to the mutex used when accessing the output raster (default:0)*/.
bool calcBestBlockSize(const unsigned int totalImageLines, const unsigned int totalImageCols, const unsigned int minExapandedBlockPixels, const unsigned int maxExapandedBlockPixels, const unsigned int blocksHOverlapSize, const unsigned int blocksVOverlapSize, unsigned int &nonExpandedBlockWidth, unsigned int &nonExpandedBlockHeight) const
Calc the best sub-image block size for each thread to process.
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.
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::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 ).
An utility struct for representing 2D coordinates.
The parameters passed to the Segmenter::segmenterthreadEntry method.
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.
std::vector< double > const * m_inputRasterOffsetsPtr
Pointer to a vector of input raster bands offset values */.
#define MAX(a, b)
Macro that returns max between two values.
bool isActive() const
Verify if the task is active.
bool volatile * m_abortSegmentationFlagPtr
Pointer to the abort segmentation flag (default:0)*/.
static void segmenterThreadEntry(SegmenterThreadEntryParams *paramsPtr)
Segmenter thread entry.
Raster Processing functions.
double getResolutionY() const
Returns the grid vertical (y-axis) resolution.
#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.
double getResolutionX() const
Returns the grid horizontal (x-axis) resolution.
A RAM cache adaptor to an external existent raster that must always be avaliable. ...
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
Segmenter::OutputParameters * m_outputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
A raster band description.
Grid * getGrid()
It returns the raster grid.
unsigned int m_segmentsMatrixYIndex
TERASTEREXPORT int GetPixelSize(int datatype)
Returns the byte size of a given datatype.
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..
boost::mutex * m_inputRasterIOMutexPtr
Pointer to the mutex used when accessing the input raster (default:0)*/.
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.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
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)*/.
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)
Segmenter::InputParameters const * m_inputParametersPtr
A pointer to the global segmenter input execution parameters (default:0). */.
ColorInterp m_colorInterp
The color interpretation.
std::vector< unsigned int > m_topCutOffProfile
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
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.
te::common::TaskProgress * m_progressPtr
A pointer to an active task progress tha should be pulsed for each processed block or a null pointer ...
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