def performStage1TilesSegmentation(self, tilesImgDIR, stage1TilesSegsDIR, tmpDIR, tilesBase, tileSegInfoJSON, strchStatsBase, kCentresBase, numClustersVal, minPxlsVal, distThresVal, bandsVal, samplingVal, kmMaxIterVal): imgTiles = glob.glob(os.path.join(tilesImgDIR, tilesBase + "*.kea")) tileStatsFiles = dict() for imgTile in imgTiles: baseName = os.path.splitext(os.path.basename(imgTile))[0] tileID = baseName.split('_')[-1] clumpsFile = os.path.join(stage1TilesSegsDIR, baseName + '_segs.kea') tmpStatsJSON = os.path.join(tilesImgDIR, baseName + '_segstats.json') strchStatsOutFile = strchStatsBase + "_" + tileID + '.txt' kCentresOutFile = kCentresBase + "_" + tileID print(clumpsFile) segutils.runShepherdSegmentation(imgTile, clumpsFile, outputMeanImg=None, tmpath=os.path.join( tmpDIR, tileID + '_segstemp'), gdalformat='KEA', noStats=False, noStretch=False, noDelete=False, numClusters=numClustersVal, minPxls=minPxlsVal, distThres=distThresVal, bands=bandsVal, sampling=samplingVal, kmMaxIter=kmMaxIterVal, processInMem=False, saveProcessStats=True, imgStretchStats=strchStatsOutFile, kMeansCentres=kCentresOutFile, imgStatsJSONFile=tmpStatsJSON) with open(tmpStatsJSON, 'r') as f: jsonStrData = f.read() segStatsInfo = json.loads(jsonStrData) tileStatsFiles[baseName] = segStatsInfo os.remove(tmpStatsJSON) with open(tileSegInfoJSON, 'w') as outfile: json.dump(tileStatsFiles, outfile, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)
def threadedTiledImgSeg(imgTile): baseName = os.path.splitext(os.path.basename(imgTile))[0] tileID = baseName.split('_')[-1] clumpsFile = os.path.join(stage1TilesSegsDIR, baseName + '_segs.kea') tmpStatsJSON = os.path.join(tilesImgDIR, baseName + '_segstats.json') strchStatsOutFile = strchStatsBase + "_" + tileID + '.txt' kCentresOutFile = kCentresBase + "_" + tileID print(clumpsFile) try: segutils.runShepherdSegmentation(imgTile, clumpsFile, outputMeanImg=None, tmpath=os.path.join(tmpDIR,tileID+'_segstemp'), gdalformat='KEA', noStats=False, noStretch=False, noDelete=False, numClusters=numClustersVal, minPxls=minPxlsVal, distThres=distThresVal, bands=bandsVal, sampling=samplingVal, kmMaxIter=kmMaxIterVal, processInMem=False, saveProcessStats=True, imgStretchStats=strchStatsOutFile, kMeansCentres=kCentresOutFile, imgStatsJSONFile=tmpStatsJSON) print(" !!! finished the seg on tile "+ imgTile) except: print(" !&!" + imgTile + " Error: Its likely this tile doesn't have enough of the geotiff inside its extent to run the seg algorithm. I recommend overlaying the tiling shapefile (..._S1Tiles.shp) on the geotiff to check the extents. You can't proceed further until you deal with this as Part 2 will fail.") pass
def ShepherdSegTest(inImage, numClusters, minPxls,tmpath, band = [1]): outputClumps = os.path.splitext(inImage)[0] + "_" + str(numClusters) + "_" + str(minPxls) + "_"+ ''.join([str(i) for i in band]) + "_clumps.kea" outputMeanImg = os.path.splitext(inImage)[0] + "_" + str(numClusters) + "_" + str(minPxls) + "_"+ ''.join([str(i) for i in band]) + "_clumps_mean.kea" segutils.runShepherdSegmentation(inImage, outputClumps, outputMeanImg, numClusters=numClusters, minPxls=minPxls, distThres=200, bands= band, tmpath= tmpath) # remove small stepwise # export columns gdalFormat = 'KEA' dataType = rsgislib.TYPE_32INT field = 'Histogram' outImage=os.path.splitext(outputClumps)[0] + "export.kea" rastergis.exportCol2GDALImage(outputClumps, outImage, gdalFormat, dataType, field) # polygonize if os.path.exists(os.path.splitext(outImage)[0] + ".shp"): os.remove(os.path.splitext(outImage)[0] + ".shp") cmd = "gdal_polygonize.py " + outImage + """ -f "ESRI Shapefile" """ + os.path.splitext(outImage)[0] + ".shp" subprocess.call(cmd, shell = True)
imagefilter.applyLeeFilter(inputImage, outputImage, 3, 3, "GTiff", rsgislib.TYPE_32FLOAT) bandList=['VV','VH','VVdivVH'] rsgislib.imageutils.setBandNames(outputImage, bandList) inputImage=outputImage # define lee image as input for rest of the script ############################################################################################## # Shepherd segmentation - this output will be used in second script to run classifier ############################################################################################## print('Performing the segmentation...') outputClumps=inputImage.replace('.tif','_clumps2.kea') outputMeanImg=inputImage.replace('.tif','_clumps2_mean.kea') segutils.runShepherdSegmentation(inputImage, outputClumps, outputMeanImg, minPxls=100) bandList=['VV','VH','VVdivVH'] rsgislib.imageutils.setBandNames(outputMeanImg, bandList) rastergis.populateStats(outputClumps, True, True, True, 1) rastergis.populateStats(outputMeanImg, True, True, True, 1) clumps=outputClumps # rename clumps image ratutils.populateImageStats(inputImage, clumps, calcMin=True,calcMax=True,calcMean=True, calcStDev=True) # add radar stats ############################################################################################## # add global urban footprint stats gufSnap=guf.split('.')[0]+'_'+inputImage.split('/')[-1].split('_')[4]+'.tif' inRefImg=clumps # base raster to snap to gdalFormat = 'GTiff' rsgislib.imageutils.resampleImage2Match(inRefImg, guf, gufSnap, gdalFormat, interpMethod='nearestneighbour', datatype=None) # perform resampling/snap
# Create folders to store results if thay do no exist if not os.path.exists("temp"): os.makedirs("temp") if not os.path.exists("shp"): os.makedirs("shp") ##################### Perform Segmentation ##################### for i in range(len(rasterList)): # The input image for the segmentation inputImage = rasterList[i] # The output segments (clumps) image clumpsFile = "temp/" + rasterList[i][:-4] + "_Clump.kea" # The output clump means image (for visualsation) meanImage = "temp/" + rasterList[i][:-4] + "_Segments.kea" # The output shapefile shapeOut = "shp/" + rasterList[i][:-4] + ".shp" # run segmentation segutils.runShepherdSegmentation(inputImage, clumpsFile, meanImage, numClusters=100, minPxls=1) # run polygonization vectorutils.polygoniseRaster(clumpsFile, shapeOut)
def segmentation(imagery_files, config): dev_mode = config['dev_mode'] for element in imagery_files: inputImage_difference = imagery_files[element][0] inputImage_post = imagery_files[element][1] inputImage_color = inputImage_post nameFile = (element) + "_segmented" clumpsFile = config['k_means_segmentation']['output'][ 'segments'] + nameFile + '.kea' # Run segmentation_file.py algorithm segutils.runShepherdSegmentation( inputImage_difference, clumpsFile, tmpath=config['k_means_segmentation']['temps']['shepherd'], numClusters=config['k_means_segmentation']['params'] ['numClusters'], minPxls=config['k_means_segmentation']['params']['minPxls'], distThres=config['k_means_segmentation']['params']['distThres'], bands=[1], sampling=config['k_means_segmentation']['params']['sampling'], kmMaxIter=config['k_means_segmentation']['params']['kmMaxIter']) # Features extraction and computation of statistical measures # Feature from Image difference bs = [rastergis.BandAttStats(band=1, meanField='ratio_rg_change')] rastergis.populateRATWithStats(inputImage_difference, clumpsFile, bs) bs = [rastergis.BandAttStats(band=2, meanField='ndvi_change')] rastergis.populateRATWithStats(inputImage_difference, clumpsFile, bs) bs = [rastergis.BandAttStats(band=3, meanField='brightness_change')] rastergis.populateRATWithStats(inputImage_difference, clumpsFile, bs) # Feature from post_image bs = [ rastergis.BandAttStats(band=1, meanField='B4'), rastergis.BandAttStats(band=2, meanField='B3'), rastergis.BandAttStats(band=3, meanField='B2'), rastergis.BandAttStats(band=4, meanField='B8'), rastergis.BandAttStats(band=5, meanField='ndvi'), rastergis.BandAttStats(band=6, maxField='slope_max', meanField='slope_mean'), rastergis.BandAttStats(band=7, meanField='mean_height', minField='min_height', maxField='max_height'), rastergis.BandAttStats(band=8, meanField='nd_stdDev'), rastergis.BandAttStats(band=12, meanField='gndvi'), rastergis.BandAttStats(band=13, meanField='brightness') ] rastergis.populateRATWithStats(inputImage_color, clumpsFile, bs) add_Coordinates(clumpsFile) if dev_mode: input_training = imagery_files[element][2] add_Training(clumpsFile, input_training, config) reading_tables(clumpsFile, element, nameFile, config['k_means_segmentation']['output']['tables'], config)
inputImg = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3.kea' outputClumps = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3_clumps.kea' outputMeanImg = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3_clumps_mean.kea' inputImg = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3_testAOI2.kea' outputClumps = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3_testAOI2_gridclumps2_100.kea' outputMeanImg = '/Users/Andy/Documents/Zambia/RemoteSensing/Sentinel_1/GRD/Out/Subset/S1B_IW_GRDH_1SDV_20170704T165718_Sigma0_stack_lee3_testAOI2grid_clumps_100_mean.kea' # # # run segmentation # # print('Performing the segmentation...') segutils.runShepherdSegmentation(inputImg, outputClumps, outputMeanImg, minPxls=100) gdalformat = 'KEA' # rsgislib.segmentation.generateRegularGrid(inputImg, outputClumps, gdalformat, 1, 1) rastergis.populateStats(outputClumps, True, True, True, 1) bandList = ['VV', 'VH', 'VVdivVH'] rsgislib.imageutils.setBandNames(inputImg, bandList) rsgislib.imageutils.setBandNames(outputMeanImg, bandList) rastergis.populateStats(outputMeanImg, True, True, True, 1) # populate RAT with mean stats from S1 clumps = outputClumps # rename clumps image ratutils.populateImageStats(inputImg, clumps, calcMean=True, calcStDev=True) # populate clumps with training data print('Populating clumps with stats...')
def ShepherdSeg(inImage, numClusters, minPxls,tmpath): outputClumps = os.path.splitext(inImage)[0] + "_clumps_elim_final.kea" outputMeanImg = os.path.splitext(inImage)[0] + "_clumps_elim_final_mean.kea" segutils.runShepherdSegmentation(inImage, outputClumps, outputMeanImg, numClusters=numClusters, minPxls=minPxls, distThres=100, tmpath= tmpath)
def segment_listfields(listfields): for f in listfields: os.chdir(f) # read in DTM resolution from file tresfile = open("DTMres.txt", "r") tres = tresfile.readlines()[0] tresfile.close() tres = int(float(tres.split("(")[1].split(",")[0])) #/size1deg # tell user which field we're in print("Field h" + f + " DTM resolution " + str(tres)) # enter "LandSerf" directory os.chdir("LandSerf") ## Perform Segmentation ## # number of clusters numClusters = 200 # The minimum object size in pixels # use 0.2sqkm and 0.04 (40000m^2) minObjectSize02 = int(math.ceil(200000. / (tres**2))) minObjectSize004 = int(math.ceil(40000. / (tres**2))) outPath02 = "rsgis_02sqkm/" outPath004 = "rsgis_004sqkm/" # make output directories if they are needed os.system("mkdir -p " + outPath02) # only do smaller segments if DTM 125m or better if tres <= 125: os.system("mkdir -p " + outPath004) # sampling of the input image imgSampling = 1000 # assume there is only one EqCyl lat 40 rawAsp layerstack file per directory inputImage = glob.glob( 'h' + f + '*LandSerfLayerstack_rawAsp_add9999_6band.kea')[0] # output segments (clumps) image segmentClumps = inputImage[0:-4] + "_segmentclumps" # output clumps mean images outputMeanSegments = inputImage[0:-4] + "_meansegs" # temporary path tmpPath = "./tmp/" # distance threshold to prevent merging distThres = 1000000 # Maximum number of iterations within Kmeans maxKmeanIter = 200 # output filenames segmentClumps02 = segmentClumps + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize02) + ".kea" outputMeanSegments02 = outputMeanSegments + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize02) + ".kea" segmentClumps004 = segmentClumps + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize004) + ".kea" outputMeanSegments004 = outputMeanSegments + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize004) + ".kea" inputImageUpdir = "../" + inputImage os.chdir(outPath02) segutils.runShepherdSegmentation(inputImageUpdir, segmentClumps02, outputMeanSegments02, tmpPath, "KEA", False, False, False, numClusters, minObjectSize02, distThres, None, imgSampling, maxKmeanIter) os.chdir('..') if tres <= 125: print("DTM resolution = {r}m".format(r=tres)) os.chdir(outPath004) segutils.runShepherdSegmentation(inputImageUpdir, segmentClumps004, outputMeanSegments004, tmpPath, "KEA", False, False, False, numClusters, minObjectSize004, distThres, None, imgSampling, maxKmeanIter) os.chdir('..') os.chdir('../..')
def main(argv=None): # IGNORE:C0111 '''Command line options.''' if argv is None: argv = sys.argv else: sys.argv.extend(argv) program_name = os.path.basename(sys.argv[0]) program_version = "v%s" % __version__ program_build_date = str(__updated__) program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date) program_shortdesc = __import__('__main__').__doc__.split("\n")[1] program_license = '''%s Created by rantolin on %s. Copyright 2014 NRS. All rights reserved. Licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 Distributed on an "AS IS" basis without warranties or conditions of any kind, either express or implied. USAGE Segmentates a raster image based in LiDAR height metrics ''' % (program_shortdesc, str(__date__)) try: # Setup argument parser parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter) parser.add_argument("input", help="The input image for the segmentation", metavar='file') parser.add_argument("-o", "--output", help="The output base name", metavar="path", required=True) parser.add_argument("-t", "--thermal", help="Thermal image to populate the Segments with Stats", metavar="file") parser.add_argument("-n", "--numclusters", help="The number of clusters in the KMeans [default: %(default)s]", metavar='int', default=8, type=int) parser.add_argument("-m", "--min_size", help="The minimum object size in pixels. [default: %(default)s]", metavar='int', default=50, type=int) parser.add_argument("-d", "--distance", help="The distance threshold to prevent merging. This has been set to an arbitrarily large number to disable this function [default: %(default)s]", metavar='float', default=1000000, type=float) parser.add_argument("-s", "--sampling", help="The sampling of the input image for KMeans (every 10th pixel). [default: %(default)s]", metavar='float', default=10, type=float) parser.add_argument("-of", "--format", help="The output format. [default: %(default)s]", metavar='GDAL format', default='KEA') parser.add_argument("-D", "--delete", help="Delete auxiliar files", action='store_true') parser.add_argument("-I", "--iterations", help="Max. number of iterations for the KMeans. [default: %(default)s]", metavar='int', default=200, type=int) parser.add_argument("-V", "--version", action="version", version=program_version_message) # Process arguments args = parser.parse_args() inputImage = args.input output = args.output numClusters = args.numclusters thermalInImage = args.thermal minObjectSize = args.min_size distThres = args.distance sampling = args.sampling kmMaxIter = args.iterations outputFormat = args.format delete = args.delete # Name of auxiliar files baseName = os.path.basename(output).split('.')[0] baseDir = os.path.dirname(os.path.abspath(output)) baseOutput = os.path.join(baseDir,baseName) segmentClumps = baseOutput + '_segs.kea' inputSegmentations = [segmentClumps] #, 'Aberfoyle_SubCompartments.kea'] MeanSegments = baseOutput + '_meansegs.kea' segmentClumpsWithSubCompartments = baseOutput + '_segs_sc.kea' segmentClumpsWithSubCompartmentsRMSmallSegs = baseOutput + '_segs_sc_rmsmall.kea' segmentClumpsWithSubCompartmentsFinal = baseOutput + '_segs_final.kea' tmpPath = "./tmp/" print 'HOLA\n\n\n\n\n\n\n' ##################### Perform Segmentation ##################### # RSGISLib function call to execute the segmentation # Utility function to call the segmentation algorithm of Shepherd et al. (2014). # Shepherd, J., Bunting, P., Dymond, J., 2013. Operational large-scale segmentation # of imagery based on iterative elimination. Journal of Applied Remote Sensing. Submitted. segutils.runShepherdSegmentation(inputImage, segmentClumps, outputMeanImg=MeanSegments, tmpath=tmpPath, gdalFormat='KEA', noStats=False, noStretch=False, noDelete=False, numClusters=numClusters, minPxls=minObjectSize, distThres=distThres, bands=None, sampling=sampling, kmMaxIter=kmMaxIter) ################################################################ ##################### Merge Segmentations ##################### # Union of Clumps rsgislib.segmentation.UnionOfClumps(segmentClumpsWithSubCompartments, 'KEA', inputSegmentations, 0) # Output # Populates statics for thematic images rsgislib.rastergis.populateStats(segmentClumpsWithSubCompartments, True, True) # Input # eliminate clumps smaller than a given size from the scene rsgislib.segmentation.RMSmallClumpsStepwise(inputImage, segmentClumpsWithSubCompartments, segmentClumpsWithSubCompartmentsRMSmallSegs, 'KEA', False, "", False, False, 5, 1000000) rsgislib.segmentation.relabelClumps(segmentClumpsWithSubCompartmentsRMSmallSegs, segmentClumpsWithSubCompartmentsFinal, 'KEA', False) rsgislib.rastergis.populateStats(segmentClumpsWithSubCompartmentsFinal, True, True) ################################################################ ############# Populate the Segments with Stats ################# bandStats = [] try: src_ds = gdal.Open(inputImage, GA_ReadOnly) except RuntimeError, e: print 'Unable to open {0}'.format(inputImage) print e sys.exit(1) for band in range( src_ds.RasterCount ): band += 1 srcband = src_ds.GetRasterBand(band) bandName = srcband.GetDescription() bandStats.append(rsgislib.rastergis.BandAttStats(band=band, meanField=bandName+'Mean', stdDevField=bandName+'StdDev')) # Run the RSGISLib command with the input parameters. rsgislib.rastergis.populateRATWithStats(inputImage, segmentClumpsWithSubCompartmentsFinal, bandStats) ################################################################ ############# Populate the Segments with Stats ################# bandStats = [] if thermalInImage: bandStats.append(rsgislib.rastergis.BandAttStats(band=1, meanField='ThermalMean', stdDevField='ThermalStdDev', minField='ThermalMin', maxField='ThermalMax')) # Run the RSGISLib command with the input parameters. rsgislib.rastergis.populateRATWithStats(thermalInImage, segmentClumpsWithSubCompartmentsFinal, bandStats) ################################################################ if outputFormat != 'KEA': #Open output format driver, see gdal_translate --formats for list driver = gdal.GetDriverByName(outputFormat) # outputExtension = driver.GetMetadata()['DMD_EXTENSION'] src_filename = baseOutput + '_segs_final.kea' dst_filename = baseOutput + '.' + driver.GetMetadata()[DMD_EXTENSION] #Open existing dataset src_ds = gdal.Open(src_filename) #Output to new format dst_ds = driver.CreateCopy(dst_filename, src_ds, 0) #Properly close the datasets to flush to disk dst_ds = None src_ds = None try: os.remove(src_filename) except OSError, e: print "Warning", "{0}\nImpossible to remove auxiliar files.".format(e)
gdalformat = 'KEA' datatype = rsgislib.TYPE_32FLOAT imgMask = outimage outImg = inputImage.replace('.tif', '_wb_mask.kea') rsgislib.imageutils.maskImage(inputImage, imgMask, outImg, gdalformat, datatype, 0, [2, 3]) # mask out dry or wetVeg pixels imageutils.popImageStats(outImg, True, 0.0, True) #segment image and add stats from S1 image inImg = outImg # in image based on masked radar image clumps = inImg.replace('.kea', '_clumps2.kea') clumpsMean = outImg.replace('.kea', '_clumps2_mean.kea') segutils.runShepherdSegmentation(inImg, clumps, clumpsMean, minPxls=100, numClusters=5) bandList = ['VV', 'VH', 'VVdivVH'] rsgislib.imageutils.setBandNames(inImg, bandList) #################################################################### # extract otsu threshold from VV band ds1 = gdal.Open(clumpsMean) maskVV = np.array(ds1.GetRasterBand(1).ReadAsArray()) maskVV = maskVV[maskVV < 0] threshold = filters.threshold_otsu(maskVV) ds1 = None #################################################################### # apply threshold
# Stack normal, then standardised band data as kea file npStackRast(band_data, stack_dir_fn, stack_fn, sentBandNames, stack_temp_ds) npStackRast(band_data_stan, stack_dir_fn, stan_rast_fn, sentBandNames, stack_temp_ds) print("Normalised raster stack created...") # ------------------------------------------------- # # Segmentation process print("Segmenting image...") segutils.runShepherdSegmentation( stan_rast_fn, segments_fn, tmpath=temp_fn, gdalformat='KEA', noStretch=True, # Issue when this is false noStats=True, # Issue when this is false numClusters=60, minPxls=25, distThres=10, sampling=100, kmMaxIter=200) # ------------------------------------------------- # # Image statistics print('Calculating image statistics...') # Spectral stats rastergis.populateStats(segments_fn) ratutils.populateImageStats( stan_rast_fn, segments_fn, calcMean=True, calcMin=True,
def segment_listfields(listfields): for f in listfields: os.chdir(f) # read in DTM resolution from file tresfile = open("DTMres.txt", "r") tres = tresfile.readlines()[0] tresfile.close() tres = int(float(tres.split("(")[1].split(",")[0])) #/size1deg # user interaction print("Field h" + f + " DTM resolution " + str(tres)) # enter "LandSerf" directory os.chdir("LandSerf") ## Perform Segmentation ## # number of clusters numClusters = 200 # The minimum object size in pixels # use size of 1 sq km. and 4 sq. km # initially 0.2sqkm minObjectSize02 = int(math.ceil(200000. / (tres**2))) #minObjectSize1 = int(math.ceil(1000000./(tres**2))) #minObjectSize4 = int(math.ceil(4000000./(tres**2))) outPath02 = "rsgis_02sqkm/" #outPath1 = "rsgis_1sqkm/" #outPath4 = "rsgis_4sqkm/" os.system("mkdir -p " + outPath02) #os.system("mkdir -p "+outPath1) #os.system("mkdir -p "+outPath4) # sampling of the input image imgSampling = 1000 # assume there is only one EqCyl lat 40 layerstack file per directory inputImage = glob.glob('h' + f + '*LandSerfLayerstack_add9999_6band.kea')[0] # output segments (clumps) image segmentClumps = inputImage[0:-4] + "_segmentclumps" # output clumps mean images outputMeanSegments = inputImage[0:-4] + "_meansegs" # temporary path tmpPath = "./tmp/" # distance threshold to prevent merging distThres = 1000000 # distThres4 = 4 # distThres3 = 3 # distThres100 = 100 # distThres10 = 10 # distThres5 = 5 # distThres2 = 2 # Maximum number of iterations within Kmeans maxKmeanIter = 200 segmentClumps02 = segmentClumps + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize02) + ".kea" outputMeanSegments02 = outputMeanSegments + "_numC_" + str( numClusters) + "_minObSz_" + str(minObjectSize02) + ".kea" #segmentClumps1 = segmentClumps + "_numC_"+str(numClusters)+"_minObSz_"+str(minObjectSize1)+".kea" #outputMeanSegments1 = outputMeanSegments + "_numC_"+str(numClusters)+"_minObSz_"+str(minObjectSize1)+".kea" #segmentClumps4 = segmentClumps + "_numC_"+str(numClusters)+"_minObSz_"+str(minObjectSize4)+".kea" #outputMeanSegments4 = outputMeanSegments + "_numC_"+str(numClusters)+"_minObSz_"+str(minObjectSize4)+".kea" inputImageUpdir = "../" + inputImage os.chdir(outPath02) segutils.runShepherdSegmentation(inputImageUpdir, segmentClumps02, outputMeanSegments02, tmpPath, "KEA", False, False, False, numClusters, minObjectSize02, distThres, None, imgSampling, maxKmeanIter) os.chdir('..') #os.chdir(outPath1) # RSGISLib function call #segutils.runShepherdSegmentation(inputImageUpdir, segmentClumps1, outputMeanSegments1, tmpPath, "KEA", False, False, False, numClusters, minObjectSize1, distThres, None, imgSampling, maxKmeanIter) #os.chdir('..') #os.chdir(outPath4) #segutils.runShepherdSegmentation(inputImageUpdir, segmentClumps4, outputMeanSegments4, tmpPath, "KEA", False, False, False, numClusters, minObjectSize4, distThres, None, imgSampling, maxKmeanIter) #os.chdir('..') os.chdir('../..')
def image_segmentation(ndvi, predict): write_cog(ndvi.to_array().compute(), "NDVI.tif", overwrite=True) # store temp files somewhere directory = "tmp" if not os.path.exists(directory): os.mkdir(directory) tmp = "tmp/" # inputs to image seg tiff_to_segment = "NDVI.tif" kea_file = "NDVI.kea" segmented_kea_file = "segmented.kea" # convert tiff to kea gdal.Translate( destName=kea_file, srcDS=tiff_to_segment, format="KEA", outputSRS="EPSG:6933" ) # run image seg with HiddenPrints(): segutils.runShepherdSegmentation( inputImg=kea_file, outputClumps=segmented_kea_file, tmpath=tmp, numClusters=60, minPxls=100, ) # convert kea to tif kwargs = { 'outputType': gdal.GDT_Float32, } gdal.Translate( destName=segmented_kea_file[:-3]+'tif', srcDS=segmented_kea_file, outputSRS="EPSG:6933", format='GTiff', **kwargs ) # open segments segments = xr.open_rasterio(segmented_kea_file[:-3]+'tif').squeeze().values # calculate mode count, _sum = _stats(predict, labels=segments, index=segments) mode = _sum > (count / 2) mode = xr.DataArray( mode, coords=predict.coords, dims=predict.dims, attrs=predict.attrs ) # remove the tmp folder shutil.rmtree(tmp) os.remove(kea_file) os.remove(segmented_kea_file) os.remove(tiff_to_segment) os.remove(segmented_kea_file[:-3]+'tif') return mode.chunk({})