def performClumpingSingleThread(inputImage, clumpsImage, tmpDIR='tmp', width=2000, height=2000, gdalformat='KEA'): """ Clump the input image using a tiled processing chain allowing large images to be clumped more quickly. :param inputImage: the input image to be clumped. :param clumpsImage: the output clumped image. :param tmpDIR: the temporary directory where intermediate files will be written (default is 'tmp'). Directory will be created and deleted if does not exist. :param width: int for width of the image tiles used for processing (Default = 2000). :param height: int for height of the image tiles used for processing (Default = 2000). :param gdalformat: string with the GDAL image format for the output image (Default = KEA). NOTE. KEA is used as intermediate format internally and therefore needs to be available. """ createdTmp = False if not os.path.exists(tmpDIR): os.makedirs(tmpDIR) createdTmp = True rsgisUtils = rsgislib.RSGISPyUtils() uidStr = rsgisUtils.uidGenerator() dataType = rsgisUtils.getRSGISLibDataTypeFromImg(inputImage) baseName = os.path.splitext(os.path.basename(inputImage))[0] + "_" + uidStr imgTilesDIR = os.path.join(tmpDIR, "imgtiles_" + uidStr) tilesClumpsDIR = os.path.join(tmpDIR, "imgclumpstiles_" + uidStr) tilesImgBase = os.path.join(imgTilesDIR, baseName) initMergedClumps = os.path.join(tmpDIR, "MergedInitClumps_" + uidStr + ".kea") if not os.path.exists(imgTilesDIR): os.makedirs(imgTilesDIR) if not os.path.exists(tilesClumpsDIR): os.makedirs(tilesClumpsDIR) imageutils.createTiles(inputImage, tilesImgBase, int(width), int(height), 0, False, 'KEA', dataType, 'kea') imageTiles = glob.glob(tilesImgBase + "*") for tile in imageTiles: tilBaseName = os.path.splitext(os.path.basename(tile))[0] clumpedTile = os.path.join(tilesClumpsDIR, tilBaseName + '_clumps.kea') segmentation.clump(tile, clumpedTile, 'KEA', True, 0, True) clumpTiles = glob.glob(os.path.join(tilesClumpsDIR, '*_clumps.kea')) print("Create Blank Image") imageutils.createCopyImage(inputImage, initMergedClumps, 1, 0, 'KEA', rsgislib.TYPE_32UINT) print("Merge Tiles into Blank Image") segmentation.mergeClumpImages(clumpTiles, initMergedClumps, True) print("Merge Tile Boundaries") segmentation.mergeEquivClumps(initMergedClumps, clumpsImage, gdalformat, ['PixelVal']) shutil.rmtree(imgTilesDIR) shutil.rmtree(tilesClumpsDIR) os.remove(initMergedClumps) if createdTmp: shutil.rmtree(tmpDIR)
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
segmentation.eliminateSinglePixels(inputImage, outputImage_1, outputImage_2, tempFile, gdalformat, True, ignoreZeros) except: print("An error occurred while performing eliminateSinglePixels!") print(sys.exc_info()[0]) sys.exit(1) # ------------------------------------------------------------------------------------------------------------------ # Perform clumping # ------------------------------------------------------------------------------------------------------------------ outputImage_3 = tmpPath + "rsgislib_shep_clump_" + rand_str() + ".tif" try: segmentation.clump(outputImage_2, outputImage_3, gdalformat, True, 0) except: print("An error occurred while performing clump!") print(sys.exc_info()[0]) sys.exit(1) # ------------------------------------------------------------------------------------------------------------------ # Eliminate small segments/clumps # ------------------------------------------------------------------------------------------------------------------ outputImage_4 = tmpPath + "rsgislib_shep_clump_clean_" + rand_str( ) + ".tif" minSize = int(float(sys.argv[4])) spectralThresh = float(sys.argv[5]) try:
def clumpImgFunc(imgs): """ Clump an image with values provides as an array for use within a multiprocessing Pool """ segmentation.clump(imgs[0], imgs[1], 'KEA', True, 0, True)