Skip to content

Tips and Tricks #3 improve Machine Learning with multiple inputs from a single image

  • blog

I’m writing a series of irregular short posts on some tips and tricks on using Geospatial Python. They may not be best practice, but I hope they are of help. I’ll try and give some context to when I’ve used the ‘tip’.

Using many image processing techniques for input

When you are building your machine learning model with Earth Observation, have you considered running several image processing steps and using these as inputs into your model? Instead of using a single band (or 12 if Sentinel 2), build a Pandas dataframe and use that as your X train / X test data.

img = skimage.io.imread(raster_file, 1)
print(img.shape)
img2 = img.reshape(-1)
df = pd.DataFrame()
df['Single_Image_band'] = img2

Read your image in (in this case with skimage), flattern it to a 1d array, build an empty dataframe and append your flattern values. Eg

 Single_Image_band
0 1037.6664
1 1039.1943
2 1031.0990
3 1046.0711
4 1027.2312
5 1011.3457
6 1016.8776
7 1029.1838
8 1010.3475
9 1016.0901

Next run n image processing methods and assign the results to the dataframe. In this case canny edge detection (but it could be anything)

edges1 = feature.canny(img)
edges2 = feature.canny(img, sigma=3)
canny1 = edges1.reshape(-1)
canny2 = edges2.reshape(-1)
df[‘Edges1’] = canny1
df[‘Edges2’] = canny2

Which gives me a dataframe that looks like:

 Single_Image_band Edges1 Edges2
0 1037.6664 False False
1 1039.1943 False False
2 1031.0990 False False
3 1046.0711 False False
4 1027.2312 False False
5 1011.3457 False False
6 1016.8776 False False
7 1029.1838 False False
8 1010.3475 False False
9 1016.0901 False False

You can keep expanding this! Just remember to create a label column for your labels and then you can use this in your machine learning classification. This is a super simple step that may well improve your modelling without having to collect more training data (of course that depends on your training data!).

 

I’ve added comments where needed to the code here I hope this has been of use.

I’ll be adding more tips and tricks


Image credit https://unsplash.com/photos/a8BgHxXpFpI

I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you.

Feel free to connect or follow me; I am always keen to talk about Earth Observation.

I am @map_andrew on twitter