A geospatial Python class for crop recognition over drone orthophoto - Tutorial

spatialPythonClassCropRecognitionDroneOrtophoto.PNG

Spatial analysis and machine learning sometimes require massive coding in order to achieve decent results such as identifying plants from a drone orthophoto. We wanted to create a simple workflow for beginner and intermediate Python users to work with these libraries without much pain or frustration. This tutorial has the complete procedure to use a Python class that recognizes plants from an orthophoto based on sample points and creates intermediate plots and identifies plants as point shapefiles.

Tutorial


Code

from workingTools import cropRecognition
import matplotlib.pyplot as plt

Create crop object

crop = cropRecognition()

Define raster and sample plants

crop.defineRaster('../odm_orthophoto/agaveCrop.tif')
CRS of Raster Data: EPSG:4326
Number of Raster Bands: 3
Interpretation of Raster Bands: (<ColorInterp.red: 3>, <ColorInterp.green: 4>, <ColorInterp.blue: 5>)
crop.definePoints('../Shp/agavePoints_3p.shp')
CRS of Point Data: epsg:4326

Retrieve information about the raster and plants

# get raster crs
crop.cropRaster.crs
CRS.from_epsg(4326)
# get point coordinates
crop.pointCoords
[(-104.60251209450945, 21.488914700478507),
 (-104.6026805707282, 21.488557889609346),
 (-104.60290182768277, 21.488567022907)]

Define parameter for the match template and raster band

# ratio of analysis
crop.pointRatio = 40
# raster band for the match template analysis
crop.selectedBand = crop.blueBand
# sample row, col and array for each point
crop.getPointRowCol()
Point N°:0 corresponds to row, col: 161, 6842
Point N°:1 corresponds to row, col: 3162, 5517
Point N°:2 corresponds to row, col: 3085, 3776
# plant arrays
crop.plotReferenceImages()
output_11_0.png

Perform single match template

# define object order and output raster path
matchTemplate2 = crop.singleMatchTemplate(2,'../Rst/matchTemplate2.tif')
# plot match template
#plt.imshow(matchTemplate0)

Match template analysis for all points

# define method (quantile or threshold) and value
crop.pointsMatchTemplate('threshold',0.32)
Processing image Nº 0
Processing image Nº 1
Processing image Nº 2
# plot all templates 
#crop.plotAllTemplates()

# save match template to a shapefile
crop.saveMatchShp('../Shp/matchCrop.shp')

Cluster analysis

# Birch clustering
crop.birchFilter()
# plot birch points
crop.plotBirchPoints()
output_20_0.png
# save cluster points as shapefile
crop.saveBirchShp('../Shp/birchCrop.shp')

Input file

You can dowload the input files from this link.

The code with sample orthophoto can be downloaded from this Github repository:

https://github.com/SaulMontoya/spatialPythonClassCropRecognitionDroneOrtophoto


1 Comment

Saul Montoya

Saul Montoya es Ingeniero Civil graduado de la Pontificia Universidad Católica del Perú en Lima con estudios de postgrado en Manejo e Ingeniería de Recursos Hídricos (Programa WAREM) de la Universidad de Stuttgart con mención en Ingeniería de Aguas Subterráneas y Hidroinformática.

 

Suscribe to our online newsletter

Subscribe for free newsletter, receive news, interesting facts and dates of our courses in water resources.