[GRASS-user] Writing a script for image classification

Hi all!

Will it be possible to write a simple script for image classification routine? Basically I have several image groups/subgroups in same/different mapsets in GRASS. I perform supervised classification with the following routine:

  1. Vector layer containing training sites imported from QGIS - same sites to be used for all image groups/subgroups

  2. Conversion of vector to raster

  3. Generation of signature sites using i.gensig - here the usage of groups/subgroups arise

  4. Classification using i.maxlik
    So far I have been doing all these operations using GUI but now I want to become a power user. Can anyone give me a brief overview/outline of how a script would look like for my purpose. I did look into the wiki pages but wasn’t able to understand it. So I seek suggestions from regular script users in this community.

Thanks and regards,

Chethan S.

Hi,
this is a script I use for automatic smap classification for landsat images.
You only have to set the variables for the mapset, the rasters for the group
and the shapefile with training areas (and the colum to use). It's in
German, but maybe it can give you some hints...

#############################################
# überwachte SMAP Klassifikation #
#############################################

maps="31_10_2009"

# Eingangsraster: welche Raster kommen in die Klassifikation

raster="REF_$maps.2,REF_$maps.3,REF_$maps.4,REF_$maps.5,REF_$maps.7"

# Untergruppe

sub="sub1"

# Gruppenname der Eingangsraster
gruppe="GruppeTest_pca"

# Eingangsshape
ein="/home/martin/Dissertation/Feldaufnahmen/trainingsflaechen_linguere_utm.shp"
shape="traningsf"

# Outputraster der Trainingsflächen, damit wird weitergearbeitet
aus="trainingsflaechen"

# Ausgabeklassifikationsraster
klassifikation="TESTsmap"

g.mapset $maps

# Gruppieren der für die Klassifikation zu verwendenden Raster
i.group --verbose --o group=$gruppe subgroup=$sub input=$raster --o
i.group -l -g group=$gruppe sub=$sub

# Shape importieren
v.in.ogr dsn=$ein output=$shape --o

# Welche Klasse (Spalte) soll ausgewählt werden?

klasse="KLASSE1"

# eine neue Spalte "id" wird angelegt und jeder Klasse eine numerische Zahl
zugewiesen

v.db.select $shape
v.db.addcol $shape col="id integer"
v.db.select $shape
v.db.update $shape col=id where="$klasse = 'Acker'" val=1
v.db.update $shape col=id where="$klasse = 'Weide'" val=2
v.db.update $shape col=id where="$klasse = 'Siedlung'" val=0
v.db.update $shape col=id where="$klasse = 'Wasser'" val=3
v.db.select $shape

# Vektor wird in Raster umgewandelt:

v.to.rast in=$shape out=$aus use=attr column=id labelcol=$klasse --o
r.stats -cpla $aus

#Generate statistics from training areas

i.gensigset group=$gruppe subgroup=$sub sig=Signatur maxsig=15
trainingmap=$aus --v --o

#Perform supervised SMAP classification
i.smap --o group=$gruppe subgroup=$sub sig=Signatur out=$klassifikation

# Filter um Salt&Pepper Effekt etwas einzudämmen
r.neighbors --o method=median input=$klassifikation
output=${klassifikation}_filtered

# Kappatest (idealerweise mit neuen Trainingsfächen)

r.kappa -w classification=$klassifikation reference=$aus

-----
Department of Geography and
Regional Research
UZA II, Althanstr. 14
1090 Wien, AUSTRIA
http://geooekologie.univie.ac.at
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Writing-a-script-for-image-classification-tp6448874p6449330.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Thanks Martin! I used Google Translate and could read the script in English.

Can you please let me know from where/how do I call the script? Is it from
the GRASS Command line in the usual Linux way - ./something?

Sorry for my ignorance but I haven't used scripts in GRASS till now.

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Writing-a-script-for-image-classification-tp6448874p6452166.html
Sent from the Grass - Users mailing list archive at Nabble.com.

Hi, are you using Windows or Linux?

-----
Department of Geography and
Regional Research
UZA II, Althanstr. 14
1090 Wien, AUSTRIA
http://geooekologie.univie.ac.at
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Writing-a-script-for-image-classification-tp6448874p6452758.html
Sent from the Grass - Users mailing list archive at Nabble.com.

I use Linux, Ubuntu 11.04.

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Writing-a-script-for-image-classification-tp6448874p6453003.html
Sent from the Grass - Users mailing list archive at Nabble.com.

you can save the script as myscript.sh, and start Grass in a terminal (type
grass) and select your location and mapset (now you can close the gui). In
the terminal you go to the directory where you saved the script, make it
executable (type chmod 0755 myscript.sh) and start the script with
./myscript.sh..

before that you need to modify the script, i.e. set the variables....for
example maps="yourmapset" is your mapset, raster"band1,band2,band3" are your
raster files you want to group......if you already made a signaturfile, you
can skip most of the script....

-----
Department of Geography and
Regional Research
UZA II, Althanstr. 14
1090 Wien, AUSTRIA
http://geooekologie.univie.ac.at
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Writing-a-script-for-image-classification-tp6448874p6453188.html
Sent from the Grass - Users mailing list archive at Nabble.com.