-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
167 lines (108 loc) · 4.48 KB
/
README.Rmd
File metadata and controls
167 lines (108 loc) · 4.48 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
---
title: "coiR"
author: "Edson Silva-Júnior"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# [coiR](https://github.com/Edbbioeco/coiR)<img src="logo_coiR.png" align = "right" width="150">
> Package to calculate Canopy Openness Index (COI) from canopy images
# Installing package
```{r, eval = FALSE}
require("devtools")
devtools::install_github("Edbbioeco/coiR")
```
# Loading package
`coiR` package is downloaded now, so we can library it. Additionally, we library the following packages:
- [terra](https://rspatial.github.io/terra/): import images files as rasters;
- [purrr](https://purrr-tidyverse-org.translate.goog/?_x_tr_sl=en&_x_tr_tl=pt&_x_tr_hl=pt&_x_tr_pto=tc): create loops for multiple operations;
- [ggplot2](https://ggplot2.tidyverse.org): create elegant graphs for imported images;
- [tidyterra](https://dieghernan.github.io/tidyterra/): visualize RGB raster
```{r}
library(coiR)
library(terra)
library(purrr)
library(ggplot2)
library(tidyterra)
```
# Data
## Importing
Now, we need to import our data. Images may be shotten photos, as .png, .jpg or .jpeg files. first, we informe images directory (`files`), and import them using `terra::rast()` function for every image, throught a loop with `purrr::map()` function. Our images (`images`) are setted as a list class object.
```{r}
files <- paste0("cropped-images/imagem", 1:4, ".png")
files
images <- purrr::map(files,
terra::rast)
names(images) <- paste0("cropped-images/imagem", 1:4, ".png")
images
```
## Visualizing
Next, lets visualize every image, using a `purrr::map()` loop, through `ggplot` and `tidyterra::geom_spatraster_rgb()` function.
```{r}
purrr::map(images, function(data){ggplot() + tidyterra::geom_spatraster_rgb(data = data) + theme_void()})
```
# Canopy Openness Index
## Isolating a single image
For our exemples, lets work in two ways: run for a single and run for multiple images. Lets set first image as a single image.
```{r}
single_image <- images[[1]]
```
## Crop image
Usely, canopy images are shotten photos, square images. For our analysis, we need to crop images into circles. We use `coiR::coir_crop()` |function.
```{r}
single_image |>
coiR::coir_crop()
```
And we also can analyse multiple images from an one shot, using `purrr::map()` loop.
```{r}
purrr::map(images, coiR::coir_crop)
```
## Binarize images
Our next step is to binarize our images. We use `coiR::coir_binarize()` function. To avoid replot canopy image, we use `plot = FALSE` argument at `coiR::coir_crop()` function. To facilite our analysis, lets use pipe (`|>`) to conect functions output.
```{r}
single_image |>
coiR::coir_crop(plot = FALSE) |>
coiR::coir_binarize()
```
As previously made, we can binarize multiple images, making a function in our `purrr::map()` loop.
```{r}
purrr::map(images, function(images){coiR::coir_crop(data = images,
plot = FALSE) |>
coiR::coir_binarize()})
```
## Index
Finally, we get COI index. We use `coiR::coir_index()` for get that value. To avoid replot cropped and binarized images, we set `plot = FALSE` for both `coiR::coir_crop()` and `coiR::coir_binarize()` functions.
```{r}
single_image |>
coiR::coir_crop(plot = FALSE) |>
coiR::coir_binarize(plot = FALSE) |>
coiR::coir_index()
```
As previously made, we can also do for multiple images, making a function in `purrr::map()` function.
```{r}
purrr::map(images, function(images){coiR::coir_crop(data = images,
plot = FALSE) |>
coiR::coir_binarize(plot = FALSE) |>
coiR::coir_index()})
```
Additionally, we can set all COI values into a vector, to make a dataframe. First, we make a null vector (`vector_index <- c()`). Next, we build a function, hyper declaring a new `vector_index` with `<<-` instead `<-`. For more values details, we set `round = 3` argument in `coiR::coir_index()` functions, to set decimal places. All in a `purrr::map()` loop.
```{r}
vector_index <- c()
multiple_index <- function(images, verbose = FALSE){
index <- images |>
coiR::coir_crop(plot = FALSE) |>
coiR::coir_binarize(plot = FALSE) |>
coiR::coir_index(round = 3)
vector_index <<- c(vector_index, index)
}
purrr::map(images, multiple_index)
vector_index
```
Finally, we make a dtaframe with those values.
```{r}
df_index <- tibble::tibble(id = images |> names(),
Index = vector_index)
df_index
```