library(tidyverse)
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, cache = T, out.width = "50%")
# load core functions and libraries
library(dualband)
library(imager)
library(EBImage)
library(pri)
set_read_img("jpg")

Demo

Load images

(img_files <- dir("sample_data/", full.names = T))

img_demo <-
  img_files %>%
  read_imgs %>%
  .^3 # correct gamma value

dualband::show(img_demo, browser = F, all = F)

Divide images into small pieces

To divide images, the locations should be approximated.

location_demo <-
  tribble(
     ~ x,  ~ y, ~ size, ~ location,
     474,  501,    100,    "a",
    1497,  933,    100,    "b"
  )
location_demo

The images are divided into a list of small images---called pieces.

piece_demo <-
  location_demo %>%
  split(.$location) %>%
  map(~ divide_piece(., img_demo))

piece_demo %>%
  map(dualband::show, browser = F)

Detect markers

Determine a center of the marker with set_center. A binarised piece can be checked as the side-effect (.check = T). This function appends attributes related to binarisation, eroding, and geomery of white-reflectance to the input image. That is, only attributes will be changed. Returns NULL when the .check = T.

piece_demo %>%
  map(~ set_center(., .check = T, browser = F,
                   white_ratio = .95, erode_size = 1, occupancy = .001))

piece_demo_1 <-
  piece_demo %>%
  map(~ set_center(., .check = F, browser = F,
                   white_ratio = .95, erode_size = 1, occupancy = .001))

Set ROIs

Determine the ROIs with set_masks. A masked piece can be checked as the side-effect (.check = T). This function appends attributes related to ROIS to the input image. That is, only attributes will be changed. Returns NULL when the .check = T.

piece_demo_1 %>%
  map(~ set_masks(., .check = T, browser = F,
                  ref_white_ratio = 1/5, margin_white_ratio = 1.2, outer_white_ratio = 2))

piece_demo_2 <-
  piece_demo_1 %>%
  map(~ set_masks(., .check = F, browser = F,
                  ref_white_ratio = 1/5, margin_white_ratio = 1.2, outer_white_ratio = 2))
sizes <-
  c(ref = .5, white = 1, margin = 1.2, outer = 2)
colors <-
  c(ref = "grey50", white = "white", margin = "black", outer = "orange")

rectangles <-
  map2(rev(sizes), rev(colors),
       ~ annotate(geom = "rect", xmin = -.x, xmax = .x, ymin = -.x, ymax = .x, fill = .y))

tibble() %>%
  ggplot(aes()) +
  theme_minimal() +
  rectangles +
  coord_fixed()

Summarise marker data

Statistics can be summarised by extract_reflectance.

piece_demo_2 %>%
  map(extract_reflectance)

Check mapping

Map reference (i.e. reflectance standard) and target (i.e. leaf) regions on the input image.

masked_pieces_demo <-
  piece_demo_2 %>%
  # adjust mapping colors and pixel values
  map(check_mask, map_rgb = c(marker = 1, target = 3), map_int = c(marker = 10, target = 10))

masked_full_demo <-
  map_full(list_pieces = masked_pieces_demo, full_size_img = img_demo)

dualband::show(masked_full_demo, browser = F)

Session information

devtools::session_info()


KeachMurakami/dualband documentation built on May 6, 2019, 12:23 p.m.