def performStage2TilesSegmentation(self, tilesImgDIR, tilesMaskedDIR, tilesSegsDIR, tilesSegBordersDIR, tmpDIR, tilesBase, s1BordersImage, segStatsInfo, minPxlsVal, distThresVal, bandsVal, ncpus): rsgisUtils = rsgislib.RSGISPyUtils() imgTiles = glob.glob(os.path.join(tilesImgDIR, tilesBase+"*.kea")) for imgTile in imgTiles: baseName = os.path.splitext(os.path.basename(imgTile))[0] maskedFile = os.path.join(tilesMaskedDIR, baseName + '_masked.kea') dataType = rsgisUtils.getRSGISLibDataTypeFromImg(imgTile) imageutils.maskImage(imgTile, s1BordersImage, maskedFile, 'KEA', dataType, 0, 0) imgTiles = glob.glob(os.path.join(tilesMaskedDIR, tilesBase+"*_masked.kea")) def stage2threadedTiledImgSeg(imgTile): baseName = os.path.splitext(os.path.basename(imgTile))[0] clumpsFile = os.path.join(tilesSegsDIR, baseName + '_segs.kea') kMeansCentres, imgStretchStats = self.findSegStatsFiles(imgTile, segStatsInfo) segutils.runShepherdSegmentationPreCalcdStats(imgTile, clumpsFile, kMeansCentres, imgStretchStats, outputMeanImg=None, tmpath=os.path.join(tmpDIR, baseName+'_segstemp'), gdalformat='KEA', noStats=False, noStretch=False, noDelete=False, minPxls=minPxlsVal, distThres=distThresVal, bands=bandsVal, processInMem=False) p = Pool(ncpus) p.map(stage2threadedTiledImgSeg, imgTiles) segTiles = glob.glob(os.path.join(tilesSegsDIR, tilesBase+"*_segs.kea")) for segTile in segTiles: baseName = os.path.splitext(os.path.basename(segTile))[0] borderMaskFile = os.path.join(tilesSegBordersDIR, baseName + '_segsborder.kea') rastergis.defineBorderClumps(segTile, 'BoundaryClumps') rastergis.exportCol2GDALImage(segTile, borderMaskFile, 'KEA', rsgislib.TYPE_8UINT, 'BoundaryClumps')
def createStage3ImageSubsets(self, inputImage, s2BordersImage, s3BordersClumps, subsetImgsDIR, subsetImgsMaskedDIR, subImgBaseName, minSize): segmentation.clump(s2BordersImage, s3BordersClumps, 'KEA', True, 0) rastergis.populateStats(s3BordersClumps, True, True) rastergis.spatialExtent(s3BordersClumps, 'minXX', 'minXY', 'maxXX', 'maxXY', 'minYX', 'minYY', 'maxYX', 'maxYY') rsgisUtils = rsgislib.RSGISPyUtils() dataType = rsgisUtils.getRSGISLibDataTypeFromImg(inputImage) ratDS = gdal.Open(s3BordersClumps, gdal.GA_Update) minX = rat.readColumn(ratDS, "minXX") maxX = rat.readColumn(ratDS, "maxXX") minY = rat.readColumn(ratDS, "minYY") maxY = rat.readColumn(ratDS, "maxYY") Histogram = rat.readColumn(ratDS, "Histogram") for i in range(minX.shape[0]): if i > 0: subImage = os.path.join(subsetImgsDIR, subImgBaseName + str(i) + '.kea') #print( "[" + str(minX[i]) + ", " + str(maxX[i]) + "][" + str(minY[i]) + ", " + str(maxY[i]) + "]" ) imageutils.subsetbbox(inputImage, subImage, 'KEA', dataType, minX[i], maxX[i], minY[i], maxY[i]) if Histogram[i] > minSize: maskedFile = os.path.join(subsetImgsMaskedDIR, subImgBaseName + str(i) + '_masked.kea') else: maskedFile = os.path.join(subsetImgsMaskedDIR, subImgBaseName + str(i) + '_burn.kea') imageutils.maskImage(subImage, s2BordersImage, maskedFile, 'KEA', dataType, 0, 0) rastergis.populateStats(maskedFile, True, False) ratDS = None
def createTilesFromMasks(inputImage, tilesBase, tilesMetaDIR, tilesImgDIR, datatype, gdalformat): """ A function to apply the image tile masks defined in createTileMaskImages to the input image to extract the individual tiles. Where: :param inputImage: is the input image being tiled. :param tileMasksBase: is the base path for the tile masks. glob will be used to find them with \*.kea added to the end. :param outTilesBase: is the base file name for the tiles. """ maskFiles = glob.glob(os.path.join(tilesMetaDIR, tilesBase + "*.kea")) idx = 1 for maskFile in maskFiles: tileImage = os.path.join(tilesImgDIR, os.path.basename(maskFile)) imageutils.maskImage(inputImage, maskFile, tileImage, gdalformat, datatype, 0, 0) imageutils.popImageStats(tileImage, True, 0., True)