def classification(data): # Add channel pcimod(file=inputfile, pciop='add', pcival=[0, 0, 1, 0, 0, 0]) # Define input parameters classesNumber = [8] # define number of classes iterations = [10] # define number iterations moveThresh = [0.01] # define move threshhold print "Running unsupervized k-means classification..." print "Creating " + str(classesNumber) + " classes, applying " + str( iterations) + " iterations at a move-threshhold of " + str( moveThresh) # Run algorithm and create classification report try: Report.clear() enableDefaultReport(report) kclus(file=inputfile, dbic=inputChans, dboc=[chansCount + 1], numclus=classesNumber, maxiter=iterations, movethrs=moveThresh) finally: enableDefaultReport('term') # this will close the report file flag1 = time.time() print "Classification complete! Time elapsed: " + str( flag1 - start) + " seconds" print ""
def kMeansClustering(fileInput, inputChannels,outputChannel): mask = [] # Area or window in the input image to process numberOfClasses = [5] # Number of classes seedfile = "" maximumIterations = [5] # Number of Iterations for calculating the mean positions movethrs = [0.01] # Movement threshold siggen = "" backval = [] # Background gray level value to be ignored during classification nsam = [] # Number of samples to collect on which to perform the iterative clustering try: kclus(fileInput, inputChannels, outputChannel, mask, numberOfClasses, seedfile, maximumIterations, movethrs, siggen, backval, nsam) print "K means clustering finished" except PCIException, e: print e
def classification(self, file, alg): if (alg == 'kmeans'): print("Your selection is K-means Classification") else: print("Your selection is Iso-Cluster Classification") report = os.getcwd() + "\\Data\\Result\\Report.txt" if os.path.isfile(report): print("Deleting old Report") os.remove(report) with datasource.open_dataset(file) as dataset: cc = dataset.chan_count print("Available Channels: ") print(cc) if (alg == 'kmeans'): file = file dbic = list(range(1, cc + 1, 1)) # input channels dboc = [cc + 1] # output channel print(dboc) mask = [] # process entire image numclus = [5] # requested number of clusters seedfile = '' # automatically generate seeds maxiter = [20] # no more than 20 iterations movethrs = [0.01] print("The file is: " + root.filename) ## enableDefaultReport(prev_report) pcimod(file=file, pciop='add', pcival=[1, 0, 0, 0, 0, 0]) Report.clear() enableDefaultReport(report) print("Applying K-Means Classification") kcluster = kclus(file, dbic, dboc, mask, numclus, seedfile, maxiter, movethrs) print("K-Means Classification is successful") else: file = file # input file dbic = list(range(1, cc + 1, 1)) # input channels dboc = [cc + 1] # output channel print(dboc) mask = [] # process entire image numclus = [5] # requested number of clusters maxclus = [7] # at most 20 clusters minclus = [5] # at least 5 clusters seedfile = '' # automatically generate seeds maxiter = [5] # no more than 20 iterations movethrs = [0.01] siggen = "NO" # no signature generation samprm = [5] stdv = [10.0] lump = [1.0] maxpair = [5] # no more than 5 cluster center pairs # clumped in one iteration backval = [] # no background value nsam = [] # default number of samples print("The file is: " + root.filename) ## enableDefaultReport(prev_report) pcimod(file=file, pciop='add', pcival=[1, 0, 0, 0, 0, 0]) Report.clear() enableDefaultReport(report) print("Applying Iso-cluster Classification") isoclus( file, dbic, dboc, mask, numclus, maxclus, minclus, seedfile, maxiter,\ movethrs, siggen, samprm, stdv, lump, maxpair, backval, nsam ) print("Iso-Cluster Classification is successful") pcimod(file=file, pciop='add', pcival=[1]) ## root.status.set("Running fmo") print("Applying MOD filter") kfmo = fmo(file=file, dbic=[cc + 1], dboc=[cc + 2]) print("MOD filter is successfully applied") pcimod(file=file, pciop='add', pcival=[1]) ## root.status.set("Running sieve") print("Applying SIEVE filter") ksieve = sieve(file=file, dbic=[cc + 1], dboc=[cc + 3], sthresh=[32]) print("SIEVE filter is successfully applied") ## Split and insert directory name split_file = file.split("/") split_file.insert(-1, "Result") join_result = "\\".join(split_file) ## Split by . dot_result = join_result.split(".") ## print(dot_result) ## print("dot result") ## join .shp at last shp_result = dot_result[0] + ".shp" current_file_path, ext = os.path.splitext(str(file)) current_file = current_file_path.split("/")[-1] ## print(current_file) ## print("current file") ## Delete previously generated shapefiles prev_files = glob.glob(os.getcwd() + "\\Data\\Result\\" + "*") ## print(prev_files) ## print("previous files") for prev_file in prev_files: prev_file_name, format = os.path.splitext( str(os.path.basename(prev_file))) ## print(prev_file_name) ## print(" prev file names") if (current_file in prev_file_name): print("DELETING: " + str(prev_file)) os.remove(prev_file) print("Successfully DELETED: " + str(prev_file)) ## if(os.path.exists("../Data/Result/"+dot_result[0])): ## print("inside if ......") ## os.remove(shp_result) ## print("shp is deleted...") print("Exporting to shapefile") ras2poly(fili=file, dbic=[cc + 3], filo=shp_result, smoothv="Yes", ftype="SHP", foptions="") print("Shapefile is successfully created")
# Execution qprint('Press any key to continue...') input() qprint('\n\n===== (>_<) Starting Execution (>_<) =====\n\n') # KCLUS from pci.kclus import kclus qprintl('[1 of 5] KCLUS\tExecuting K-Means...') kclus( file=dataset.name, # Input file name dbic=inputChannels, # Input channels dboc=[kClusChannel], # Output channel numclus=[5], # Number of output cluster classes maxiter=[5] # Maximum iterations ) updateChannelName(kClusChannel, KCLUS_RESULT) qprintl('[1 of 5] KCLUS\tExecuting K-Means...OK \\(^o^)/') # FMO from pci.fmo import fmo qprint('[2 of 5] FMO\tApplying Mode Filter...') fmo( file=dataset.name, # Input file dbic=[kClusChannel], # Input channel dboc=[fmoChannel], # Output channel
for i in range(1, dataset.chan_count + 1): qprintl(' Channel {}: {}'.format( i, auxiliaryData.get_chan_description(i))) else: qprintl(' No') # KCLUS # kclus: Unsupervised Classification with K-Means from pci.kclus import kclus qprintl('Executing K-Means...') kclus( file=dataset.name, # Input file name dbic=list(range(1, 7)), # Input channels dboc=[7], # Output channel numclus=[5], # Number of output cluster classes maxiter=[5] # Maximum iterations ) auxiliaryData.set_chan_description('K-Means', 7) qprintl('Executing K-Means: OK \\(^o^)/') # FMO # fmo: Mode Filter from pci.fmo import fmo qprint('Applying Mode Filter...') fmo( file=dataset.name, # Input file dbic=[7], # Input channel
def classification(image): # Set timer start = time.time() # Define output file name outputFile = "GH_classPolygons.shp" output = outputFolder + "\\" + outputFile # get image statistics to extract channel number print "Detecting current channels..." with ds.open_dataset(image) as dataset: chansCount = dataset.chan_count print str(chansCount) + " channels detected" print "" # Whipe previously created channels (only use if you want a fresh clean input file and don't need previously created channels) chansDel = range(7, chansCount + 1) if chansCount > 6: # Adjust depending on image bands pcimod(file=image, pciop='del', pcival=chansDel) print str(len(chansDel)) + " previously created channels deleted" # Whipe previously created shape files in output folder files = glob.glob(outputFolder + "\\" + '*') for f in files: os.remove(f) print "Previous output files deleted" print "" # Count input channels and create input channel list with ds.open_dataset(image) as dataset: inputChansCount = dataset.chan_count inputChans = range(1, inputChansCount + 1) print inputChans # Add 3 channels to the image for storing algorithm output print "Adding three 8-bit channels to image..." pcimod(file=image, pciop='add', pcival=[3, 0, 0, 0, 0, 0]) print "Three 8-bit channels added" print "" # Run k-means cluster algorithm classesNumber = [8] # define number of classes iterations = [10] # define number iterations moveThresh = [0.01] # define move threshhold print "Running unsupervized k-means classification..." print "Creating " + str(classesNumber) + " classes, applying " + str( iterations) + " iterations at a move-threshhold of " + str(moveThresh) kclus(file=image, dbic=inputChans, dboc=[7], numclus=classesNumber, maxiter=iterations, movethrs=moveThresh) flag1 = time.time() print "Classification complete! Time ellapsed: " + str(flag1 - start) + " seconds" print "" # Run mode filter print "Running Mode Filter..." fmo(file=image, dbic=[7], dboc=[8], thinline="OFF", flsz=[3, 3]) flag2 = time.time() print "Filtering complete! Time ellapsed: " + str(flag2 - start) + " seconds" print "" # Run sieve print "Applying sieve..." sieve(file=image, dbic=[8], dboc=[9], sthresh=[16]) flag3 = time.time() print "Sieve complete! Time ellapsed: " + str(flag3 - start) + " seconds" print "" # Create vector ploygons and export as shape file print "Creating polygons..." ras2poly(fili=image, dbic=[9], filo=output, ftype="SHP") flag4 = time.time() print "Polygons created! Time ellapsed: " + str(flag4 - start) + " seconds" print "" print "Exporting as shape file..." end = time.time() print "Total time ellapsed: " + str(end - start) + " seconds"