I am trying to calculate the percentage of leaf damage using R. I am able to do the image segmentation and area calculation using the pliman package, following the vignettes: https://cran.r-project.org/web/packages/pliman/vignettes/pliman_start.html
I am using the following R code. I put a 1cm square as a reference scale. A sample segmented image can be downloaded here:
#...........................Load image...........................
library(pliman)
img_segm <- pliman::image_import("C:/Users/sando/Desktop/test/imgseg/imgseg.jpg")
#.........................Analize leaves.........................
layout(t(1:2))
meas <- analyze_objects(img_segm, index = "BIM", marker = "id", invert= F,show_image=T, parallel=T, watershed= T, fill_hull= F,
tolerance = 30, object_size = "large")
# Measures area in pixels
area1 <- get_measures(meas)[,c(1,4)]
area1
# Correct the measures using an object in cm
real.area <- get_measures(meas, id = 1, area ~ 1)[,c(1,4)]
real.area
#........................Analize contour.........................
# Draw a convex hull around the objects.
cont_meas <- analyze_objects(img_segm,
watershed = T,
marker = "id",
show_chull = TRUE,index = "BIM", invert= F,show_image=T, parallel=T, fill_hull= F, tolerance = 30, object_size = "large") # shows the convex hull
# Measures area
real.area2 <- get_measures(cont_meas, id = 1, area ~ 1 )[,c(1,4)]
However, I can't obtain the area damaged, and I can't use color segmentation because background is white. I would like:
- Identify each leaf individually
- Predict or select the missing leaf edges using some form of convex hull detection or ROI.
- Calculate the area of damage (in red) and total area (leaf + red).
I know it's possible to do some binary transformation. So I've tried the following code:
#### detect contours in red
library(imager)
img_segm2 <- imager::as.cimg(img_segm)
plot(img_segm2)
# isoblur, greyscale
img_segm3 <- isoblur(grayscale(img_segm2),2) > .60
plot(img_segm3)
px <- img_segm3 > 0.1
ct <- imager::contours(px,nlevels=3)
plot(px)
#Add contour lines
purrr::walk(ct,function(v) lines(v$x,v$y,col="red",lwd=1.5))
Is there any method that allows me to obtain this in a semi-automatic way? I know it can be done in ImageJ and some software like leafbyte, or bioleaf, but I would like to be able to analyze these images in R or Python. Thank you for your time.