---
title: "Plant IMage Analysis"
author: "Tiago Olivoto"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
fig_caption: yes
link-citations: true
vignette: >
  %\VignetteIndexEntry{Plant IMage Analysis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r global_options, include = FALSE}
knitr::opts_chunk$set(comment = "#", collapse = TRUE, fig.width = 5)
```

# Getting started
`pliman` (**pl**ant **im**age **an**alysis) is designed to analyze plant images, especially related to leaf analysis. You provide color palettes, tell `pliman` what each one represents, and it takes care of the details. The package will help you to:

* Get object statistics (number, area, perimeter, etc.) with `analyze_objects()`.
* Measure disease severity with `symptomatic_area()`.
* Get the RGB values for each object in an image with `objects_rgb()`.
* Get object measures with `get_measures()`.
* Plot objec measures with `plot_measures()`.


# Leaf area
The function `analyze_objects()` can be used to measure the leaf area in an image. The pixel area can be adjusted to metric units (cm) in two ways: (i) Using an object of known area to correct the measures or (ii) knowing the image resolution in dpi (dots per inch).

To show the first approach, first we count the number of objects and plot an object id using the argument `marker = "text"` of the function `analyze_objects()`. This allows for the further adjustment of the leaf area using the known object, that in this case is the leaf square, with 4 cm$^2$.



```{r}
library(pliman)
leaves <- image_pliman("la_leaves.jpg")
plot(leaves)
count <- analyze_objects(leaves, marker = "id")
```


- Adjust the leaf area by the area of the known object

The function `get_measures()` is used to adjust the leaf area using object 6.

```{r leaf6}
area <- 
  get_measures(count,
               id = 6,
               area ~ 4)
area
# plot the area to the segmented image
image_segment(leaves, index = "NB", verbose = FALSE)
plot_measures(area, measure = "area")

```


When the image resolution is known, we can the image dpi (dots per inch) to correct the pixel units given by `analyze_objects()` to metric units. The function `dpi()` can be used to compute the dpi of an image, provided that the size of any object is known. See a brief tutorial [here](https://www.youtube.com/watch?v=Rh10_pLgeng).
```{r}
get_measures(count, dpi = 84)
```



# Counting crop grains 

Here, we will count the grains in the image `soybean_touch.jpg`. This image has a cyan background and contains 30 soybean grains that touch with each other.

```{r}
soy <- image_pliman("soybean_touch.jpg")
plot(soy)

# Count the objects in the image
grains <- analyze_objects(soy)

# Draws the object id (by default)
plot_measures(grains)
```



# Disease severity
The function `measure_disease()` is used to compute the percentage of symptomatic leaf area in a sample or entire leaf based on provided color palettes samples. A general linear model (binomial family) fitted to the RGB values is used to segment the lesions from the healthy leaf. If a pallet of background is provided, the function takes care of the details to isolate it before computing the number and area of lesions.

The next example computes the symptomatic area of a soybean leaf. The proportion of the healthy and symptomatic areas is given as the proportion of the total leaf area, after segmenting the leaf from the background (blue).

```{r}
img <- image_pliman("sev_leaf.jpg")
healthy <- image_pliman("sev_healthy.jpg")
symptoms <- image_pliman("sev_sympt.jpg")
background <- image_pliman("sev_back.jpg")
image_combine(img, healthy, symptoms,background)

# Computes the symptomatic area
measure_disease(img = img,
                img_healthy = healthy,
                img_symptoms = symptoms,
                img_background = background,
                show_image = TRUE)
```


# RGB values for objects
The function `objects_rgb()` can be used to get the Red Green and Blue (RGB) for objects in an image. Here, the RGB for 

```{r}
img <- image_pliman("soy_green.jpg")
# Segment the foreground (grains) using the normalized blue index
# Shows the average value of the blue index in each object

rgb <- 
  analyze_objects(img,
                  object_index = "B",
                  marker = "index")


# plot the distribution of RGB values of each object
plot(rgb)
```



# Getting help

-   If you encounter a clear bug, please file a minimal reproducible example on [github](https://github.com/TiagoOlivoto/pliman/issues)

-   Suggestions and criticisms to improve the quality and usability of the package are welcome!

# <i class="fas fa-scroll"></i> A little bit more!

<!-- inicio font awesome -->
<script src="https://kit.fontawesome.com/1f72d6921a.js" crossorigin="anonymous"></script>

> At [this link](https://tiagoolivoto.github.io/paper_pliman/code.html) you will find more examples on how to use {pliman} to analyze plant images. Source code and images can be downloaded [here](https://github.com/TiagoOlivoto/paper_pliman/archive/refs/heads/master.zip). You can also find a talk (Portuguese language) about {pliman} [here](https://www.youtube.com/watch?v=ElvUVlPocgA). <i class="far fa-lightbulb"></i>Lights, <i class="fas fa-camera"></i>camera, {pliman}!
