Introduction

Production of the images of the cores

Tree stem sections are scanned by an A3 flat scanner at 1200 ppi resolution Images are saved as PNG [ask to Cristian why] Using Gimp, images are open and 3 cores are selected, one at a time, rotating the selection rectangle [ask MatteoP how to do it]. Cores are rectanles with the bark at the left and the pith at the rigth end, Widths schuold be between 50 and 100 lines, image schod not include any non-wood portion. Core 1 is selected towards the reference sign, core 2 circa 120 deg clockwise and core 3 other 120 deg clockwise. SOME IMAGE PROCESSING IS GENERALLY REQUIRED TO OPTIMIZE RING DETECTION [ask MakeInNuoro how to detail this step] For each core, ELIMINATE ALPHA CHANNEL FOM THE LEVEL (*), then save as .tiff with no compression. File name:-

-

(*) search: “gimp export tiff with only 3 channels” answer: “https://graphicdesign.stackexchange.com/questions/45265/export-png-image-without-alpha-channel-in-gimp” “In GIMP, alpha channels exist per layer. If you go to the layers dialogue (Ctrl+L), right-click on your layer, there is an item Remove Alpha Channel, which does what you want. If it is greyed out, your layer does not have an alpha channel.”

Testing the package with its demo images

library(tidyverse)
library(measuRing)
library(imager)
plotCore <- function(grey, rb) {
  cc <- min(dim(grey))/2
  bc <- c(1:max(dim(grey)))[!is.na(rb$borders)]
  brd <- data.frame( x = bc, y = cc)
  grey %>% 
    as.cimg %>% 
    as.data.frame %>% 
    ggplot(aes(y, x)) + 
    geom_raster(aes(fill = value)) + 
    scale_fill_gradient(low="black",high="white") + 
    theme(legend.position = "none") +
    geom_point(data = brd, aes(x, jitter(y, cc)), colour = "white", shape = 4)
}

plotSmoothed <- function(rb, last.year) {
  coreID <- names(rb)[1]
  SmtdG <- 'Smoothed gray'
  rb1 <- rb %>% 
    rename(SmtdG = 1) %>% 
    mutate(col = 1:nrow(.))
  rb2 <- rb1 %>% 
    filter(!is.na(borders)) %>% 
    mutate(ringn = nrow(.):1, 
           year = last.year:(last.year-nrow(.)+1))
  ggplot() +
    geom_line(data  = rb1, aes(col, SmtdG), colour = "grey") +
    geom_point(data = rb2, aes(col, SmtdG), colour = "black", shape = 4) +
    geom_text(data = rb2, aes(col, SmtdG, label = year), hjust = 2, size = 3, angle = 90)
}

processCore <- function(image, 
                        plot = T, last.year = NULL,...) {
  grey <- imageTogray(image)
  rb <- ringBorders(grey)
  if(is.null(last.year)) last.year <- sum(!is.na(rb$borders))
  print(last.year)
  core <- plotCore(grey, rb)
  smtd <- plotSmoothed(rb, last.year)
  ggpubr::ggarrange(smtd, core, 
          ncol = 1, nrow = 2, align = "v")
  #  title = coreID
}

img_pth <- "measuRing_demo_imgs"
last.year <- 2011
imgs <- dir(img_pth, pattern = "P")
imgs
## [1] "P105_a.png" "P105_a.tif" "P105_b.tif" "P105_c.tif" "P105_d.tif"
image <- paste0(img_pth, "/", imgs[2])
processCore(image, last.year = last.year)
## [1] 2011

# GDrive folder "Rotelle_scansione": https://drive.google.com/drive/folders/1qr3ijH7iC8-BcueIO3l0VSkGWTk58U9a
img_dir <- "G:\\Il mio Drive\\DocumentiGDuniss\\02-RICERCA\\2017_Arzana-pineta\\Rotelle_scansione\\Cores\\"
last.year <- 2018
(imgs <- dir(img_dir))
## [1] "67-6-1.tif"  "desktop.ini"
image <- paste0(img_dir, "67-6-1.tif")

str(tiff::readTIFF(image))
##  num [1:1256, 1:2288, 1:3] 1 1 1 1 1 1 1 1 1 1 ...
processCore(image, last.year = last.year)
## [1] 2018