Processing Colour Components

Splitting a raster into 3 colour components

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Extracts red, green and blue components from a raster
version(1.0);

# Change the directory below to correspond to your file location.
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/";

# Open image file.
image = open(baseDir&"photo.srf");

# Create a new raster to store the colour components
colourComponent = new(image);
edit(colourComponent,"type","other");

# Extract the three colour components and save the rasters.
colourComponent = red(image);
colouredit(colourComponent,"rules","0 0, 255 255 0 0");
save(colourComponent,baseDir&"redImage.srf");

colourComponent = green(image);
colouredit(colourComponent,"rules","0 0, 255 0 255 0");
save(colourComponent,baseDir&"greenImage.srf");

colourComponent = blue(image);
colouredit(colourComponent,"rules","0 0, 255 0 0 255");
save(colourComponent,baseDir&"blueImage.srf");
photo.srf:
photo.srf

redImage.srf, greenImage.srf, blueImage.srf:
R G B components

Creating a colour composite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Combines three colour component rasters to create a single colour composite.
version(1.0);

# Change the directory below to correspond to your file location.
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/";

# Open image file.
redImage = open(baseDir&"redImage.srf");
grnImage = open(baseDir&"greenImage.srf");
bluImage = open(baseDir&"blueImage.srf");

# Create and save a new raster storing colour composite
composite = new(redImage);
edit(composite,"type","image");
colouredit(composite,"raw");

composite = rgb(redImage,grnImage,bluImage);
save(composite,baseDir&"compositeImage.srf");

Converting a colour raster to greyscale

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Creates a greyscale version of a raster using the formula
# greyLev = 0.3R + 0.59G + 0.11B
version(1.0);

# Change the directory below to correspond to your file location.
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/";

# Open image file.
image = open(baseDir&"photo.srf");

# Create a new raster to store the grey level values
greyImage = new(image);
edit(greyImage,"type","other");
colouredit(greyImage,"rules","0 0, 255 255");

# Do the conversion and save the raster.
greyImage = 0.30*red(image) + 0.59*green(image) + 0.11*blue(image);
save(greyImage,baseDir&"greyImage.srf");
greyImage.srf:
greyImage.srf

Extracting green pixels from a scanned raster map

This example looks for greenish pixels by comparing the R,G and B values for each raster cell. If a pixel is found to be green it is labeled as vegetation (1), otherwise it is labeled as white background (0);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Extracts green components from an image 
version(1.0); 
 
# Change the directory below to correspond to your file location. 
baseDir = "c:/Program Files/LandSerf/data/landscriptExamples/"; 
 
# Open image file. 
image = open(baseDir&"topoMap.srf"); 
 
# Extract the greenish pixels 
image = ifelse((green(image) > blue(image)) and  
               (green(image) > red(image))  and  
               (green(image)/(red(image)+blue(image)) > 0.6),1,0); 
 
# Pass a 5x5 median filter over surface to remove isolated patches. 
veg = new(image); 
veg = median(image[-2,-2],image[-2,-1],image[-2,0],image[-2,1],image[-2,2], 
             image[-1,-2],image[-1,-1],image[-1,0],image[-1,1],image[-1,2], 
             image[ 0,-2],image[ 0,-1],image[ 0,0],image[ 0,1],image[ 0,2], 
             image[ 1,-2],image[ 1,-1],image[ 1,0],image[ 1,1],image[ 1,2], 
             image[ 2,-2],image[ 2,-1],image[ 2,0],image[ 2,1],image[ 2,2]); 
 
# Add metadata and save the green raster. 
colouredit(veg,"rules","0 255 255 255, 1 0 200 0"); 
edit(veg,"type","other"); 
save(veg,baseDir&"veg.srf"); 
topoMap.srf:
topoMap.srf
veg.srf:
veg.srf