Created
July 9, 2016 17:02
-
-
Save danclewley/393799411ca413cd29c53d73196f6935 to your computer and use it in GitHub Desktop.
RSGISLib Tiled Segmentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Apply RSGISLib segmentation to image | |
using standard or tiled approach. | |
For more details see: | |
https://spectraldifferences.wordpress.com/2016/07/09/scalable-image-segmentation-using-rsgislib/ | |
Dan Clewley (09/07/2016) | |
""" | |
from __future__ import print_function | |
import argparse | |
import shutil | |
import sys | |
import tempfile | |
from rsgislib.segmentation import tiledsegsingle | |
from rsgislib.segmentation import segutils | |
# Set values for clustering | |
NUM_CLUSTERS = 60 | |
MIN_PIXELS = 100 | |
DIST_THRESHOLD = 100 | |
# Set values for tiles segmentation | |
TILE_WIDTH = 2000 | |
TILE_HEIGHT = 2000 | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("inputimage", nargs=1, | |
type=str, help="Input Image") | |
parser.add_argument("outputclumps", nargs=1, | |
type=str, help="Output clumps") | |
parser.add_argument("--tiled", | |
default=False, | |
action="store_true", | |
help="Use tiled segmentation") | |
args = parser.parse_args() | |
# Make temp directory for intermediate files | |
temp_dir = tempfile.mkdtemp(prefix='rsgislib_seg_') | |
try: | |
if args.tiled: | |
# If requested run tiled segmentation | |
tiledsegsingle.performTiledSegmentation(args.inputimage[0], args.outputclumps[0], | |
tmpDIR=temp_dir, | |
tileWidth=TILE_WIDTH, tileHeight=TILE_HEIGHT, | |
validDataThreshold=0.3, | |
numClusters=NUM_CLUSTERS, | |
minPxls=MIN_PIXELS, | |
distThres=DIST_THRESHOLD, | |
sampling=100, kmMaxIter=200) | |
else: | |
# If not run standard | |
segutils.runShepherdSegmentation(args.inputimage[0], args.outputclumps[0], | |
tmpath=temp_dir, | |
numClusters=NUM_CLUSTERS, | |
minPxls=MIN_PIXELS, | |
distThres=DIST_THRESHOLD, | |
sampling=100, kmMaxIter=200) | |
except KeyboardInterrupt: | |
sys.exit(2) | |
except Exception as err: | |
print("Segmentation failed: {}".format(err), file=sys.stderr) | |
sys.exit(1) | |
finally: | |
# Remove temp directory | |
shutil.rmtree(temp_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment