import collections, glob
from rsgislib import rastergis


listFiles=glob.glob('*_clumps2.kea')
for clumps in listFiles[0:2]:
#	clumps=listFiles[1]
	field = 'OutClass_mode_cert'
classcolours = {}
colourCat = collections.namedtuple('ColourCat', ['red', 'green', 'blue', 'alpha'])
classcolours[1] = colourCat(red=106, green=143, blue=255, alpha=255)
classcolours[2] = colourCat(red=169, green=169, blue=169, alpha=255)
classcolours[3] = colourCat(red=177, green=255, blue=165, alpha=255)
classcolours[4] = colourCat(red=255, green=255, blue=191, alpha=255)
rastergis.colourClasses(clumps, field, classcolours)
def colour_SM_image(inimage, colourimage, max_value=0.5, band=1):
    """
    Colour image 

    """
    try:
        import rsgislib
        from rsgislib import imagecalc
        from rsgislib.imagecalc import BandDefn
        from rsgislib import rastergis
    except ImportError:
        raise ImportError("Could not import RSGISLib, required"
                          " to colour image")

    if max_value == 0.5:

        # Add class field:
        bandDefns = []
        bandDefns.append(BandDefn('SM', inimage, band))

        expression = []
        expression.append('0.00')
        expression.append('(SM > 0.00) && (SM <= 0.05)? 1 : 0')
        expression.append('(SM > 0.05) && (SM <= 0.10)? 2 : 0')
        expression.append('(SM > 0.10) && (SM <= 0.15)? 3 : 0')
        expression.append('(SM > 0.15) && (SM <= 0.20)? 4 : 0')
        expression.append('(SM > 0.20) && (SM <= 0.25)? 5 : 0')
        expression.append('(SM > 0.25) && (SM <= 0.30)? 6 : 0')
        expression.append('(SM > 0.30) && (SM <= 0.35)? 7 : 0')
        expression.append('(SM > 0.35) && (SM <= 0.40)? 8 : 0')
        expression.append('(SM > 0.40) && (SM <= 0.45)? 9 : 0')
        expression.append('(SM > 0.45) && (SM <= 0.50)? 10 : 0')

        gdalformat = get_gdal_format(colourimage)
        datatype = rsgislib.TYPE_8INT

        temp_dir = tempfile.mkdtemp(prefix='soilscape_upscaling')
        colourbase = os.path.basename(colourimage)
        colourbase, colourext = os.path.splitext(colourbase)
        colour = []
        for i in range(11):
            colour.append(
                os.path.join(temp_dir,
                             "{}_{}{}".format(colourbase, i + 1, colourext)))
            imagecalc.bandMath(colour[i], expression[i], gdalformat, datatype,
                               bandDefns)

        bandDefns = []
        for i in range(11):
            SMclass = 'SM' + str(i)
            bandDefns.append(BandDefn(SMclass, colour[i], 1))
            if i == 0:
                expression = SMclass
            else:
                expression = expression + '+' + SMclass
        imagecalc.bandMath(colourimage, expression, gdalformat, datatype,
                           bandDefns)

        shutil.rmtree(temp_dir)

        # Populate stats (converts to RAT):
        rastergis.populateStats(colourimage, False, False, True, ratband=1)

        # Add class field:
        bandStats = []
        bandStats.append(rastergis.BandAttStats(band=1, maxField="SMclass"))
        rastergis.populateRATWithStats(colourimage,
                                       colourimage,
                                       bandStats,
                                       ratband=1)

        field = 'SMclass'
        classcolours = {}
        colourCat = collections.namedtuple('ColourCat',
                                           ['red', 'green', 'blue', 'alpha'])
        for i in range(11):
            classcolours[i] = colourCat(red=0, green=0, blue=0, alpha=255)
        classcolours[0] = colourCat(red=0, green=0, blue=0, alpha=255)
        classcolours[1] = colourCat(red=165, green=0, blue=38, alpha=255)
        classcolours[2] = colourCat(red=215, green=48, blue=39, alpha=255)
        classcolours[3] = colourCat(red=244, green=109, blue=67, alpha=255)
        classcolours[4] = colourCat(red=253, green=174, blue=97, alpha=255)
        classcolours[5] = colourCat(red=254, green=224, blue=144, alpha=255)
        classcolours[6] = colourCat(red=224, green=243, blue=248, alpha=255)
        classcolours[7] = colourCat(red=171, green=217, blue=233, alpha=255)
        classcolours[8] = colourCat(red=116, green=173, blue=209, alpha=255)
        classcolours[9] = colourCat(red=69, green=117, blue=180, alpha=255)
        classcolours[10] = colourCat(red=49, green=54, blue=149, alpha=255)
        rastergis.colourClasses(colourimage, field, classcolours)

    else:
        raise ValueError('Failed to find colours for specified max_value')
	elif i==3:
		names.append('VegWater')

# write columns to RAT
rios.rat.writeColumn(outputClumps, 'OutClass_mode_pc', x_percent, colType=gdal.GFT_Real)
rios.rat.writeColumn(outputClumps, 'OutClass_mode_cert', mode_cert, colType=gdal.GFT_Integer)
rios.rat.writeColumn(outputClumps, 'OutClass_mode_cert_names', names, colType=gdal.GFT_String)

# define the output colours
classColours = dict()
classColours['Other'] = [212,125,83]
classColours['Water'] = [157,212,255]
classColours['VegWater'] = [191,255,0]

field='OutClass_mode_cert_names'
rastergis.colourClasses(outputClumps, field, classColours)

############################################################################################ # Random forests ranks parameters in terms of importance to the classifier
# there is a lot which can be learnt here...
'''
print('Feature importance: ' + inputImg)
featImportances = classifier.feature_importances_
featIndices = numpy.argsort(featImportances)[::-1]
# Print the feature ranking
print("Feature ranking:")
for f in range(len(variables)):
	print("\t{0}. {1} ({2})".format(f + 1, variables[featIndices[f]], featImportances[featIndices[f]]))
'''
############################################################################################

Exemplo n.º 4
0
# classColours['Other'] = [212,125,83]
# classColours['Water'] = [157,212,255]
# classColours['VegWater'] = [191,255,0]
# #
# classcolours = {}
# colourCat = collections.namedtuple('ColourCat', \
#                         ['red', 'green', 'blue', 'alpha'])
# classcolours[0] = colourCat(red=0, green=0, blue=0, alpha=0)
# classcolours[1] = colourCat(red=255, green=0, blue=0, alpha=255)
# classcolours[2] = colourCat(red=0, green=0, blue=255, alpha=255)
# classcolours[3] = colourCat(red=0, green=200, blue=0, alpha=255)
# classcolours[4] = colourCat(red=0, green=100, blue=0, alpha=255)
# rastergis.colourClasses(classification, 'Class', classColours)

# Add pyramids (for fast display)
# rastergis.populateStats(classification, addclrtab=False, \
#           calcpyramids=True, ignorezero=False)

# pyramids=True
# colourtable=True
# rastergis.populateStats(classification, colourtable, pyramids)

field = 'Value'
classcolours = {}
colourCat = collections.namedtuple('ColourCat',
                                   ['red', 'green', 'blue', 'alpha'])
classcolours[1] = colourCat(red=200, green=50, blue=50, alpha=255)
classcolours[2] = colourCat(red=200, green=240, blue=50, alpha=255)
classcolours[3] = colourCat(red=50, green=240, blue=50, alpha=255)
rastergis.colourClasses(classification, field, classcolours)