'Loops for calculating vegetation indices for multiple images and each image have multiple bands
I am a beginner in programmation
I have a raster stack of 40 elements
The stack 'images' is created using 3 bands (RGB) of drone. So each image have multiple bands. The sequence of the rasters in the stack is:
Position [[1]]: red band image 1
Position [[2]]: green band image 1
Position [[3]]: blue band image 1
Position [[4]]: red band image 2
Position [[5]]: green band image 2
Position [[6]]: blue band image 2
Position [[7]]: red band image 3
...
I want to derive vegetation index for each image: (40 EXG = excess green) : EXG = (2* green band - red band - blue band) /255. I divided by 255 to normalized the index.
So far I have doing it manually. For example:
EXG1-> (2*images[[2]] - images[[1]]) - images[[3]])/255
EXG2-> (2*images[[5]] - images[[4]]) - images[[6]])/255
etc.
Since my raster stack as a pattern, I am looking for an automatic way to derive the indices. Something like a loop that calculates the index, save it with the appropriate name and then moves to the next 'position' to repeat the process.
Any help on that please?
thank you
Solution 1:[1]
Assuming there is 40 images in stack, each with 3 bands something like:
indices <- data.frame(image_nr = integer(), index = double())
for (i in 1:40) {
EXG -> (2*images[[i*3-1]] - images[[i*3-2]] - images[[i*3]]) /255
newrow <- data.frame(image_nr = i, index = EXG)
indices <- rbind(indices, newrow)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
