4

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

enter image description here

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))

enter image description here

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.

M Sandoval
  • 87
  • 6
  • This is a really cool question, are you a scientist? – Brock Brown Mar 15 '23 at 02:07
  • 1
    Thanks. Yes, this is for my PhD thesis. I want to analyze hundreds of photos like this in R. Any advice on the code is welcome. – M Sandoval Mar 15 '23 at 04:08
  • 1
    alpha shapes, which is like convex hull but not convex, can give you a decent outline of your shapes. then you can just calculate a mask for what's not there. if you already have contours... get those contours with hierarchy, or just get the outermost contours (make sure you understand the semantics of what's a white=foreground and a black=background pixel) – Christoph Rackwitz Mar 15 '23 at 08:38
  • @MSandoval That's awesome, good luck! I'm taking a look at this today, hopefully should get some code to you sometime soon. – Brock Brown Mar 16 '23 at 14:24

1 Answers1

1

Alright, here's a start for you:

https://github.com/brockbrownwork/leaves

I'll polish up this answer later, but here's the main results:

Input: input_image Output: frayed_edges holes

Fraying: 3.947%

Percentage of non-fray holes: 5.589%

Edit:

If you have a few examples of the images you're going to use, that would be helpful. Mainly I want to know if convex hulls will accurately wrap around your leaves, otherwise we'll have to use something else. Are you looking for the stats of each individual leaf, or just the average of the whole? You may have to change the variable that represents the minimum area of a leaf in pixels. The hole detection wraps a little bit around the border of the leaf too, will fix that.

Brock Brown
  • 956
  • 5
  • 11
  • may I suggest that you outline coarsely the concepts/principles you used to reach these results, in the answer? links to off-site complete code may be okay if the answer on the site is enough to replicate the results qualitatively. – Christoph Rackwitz Mar 16 '23 at 23:02
  • Wow! Thank you! your code is almost exactly what was looking for. There are some regions of leaves that your code couldn't detect on the edges. Do you think is possible to account for that damage too? maybe using convex hull or alpha hull to fill the contour before applying the masks. Also, I would like to extract the percentage of damage of each individual leaves if it's possible. I really appreciate your help! more photos can be found here, [link](https://postimg.cc/gallery/fyLmS7D) – M Sandoval Mar 17 '23 at 22:12
  • 1
    Hi there @MSandoval! It was a pleasure working on your project. The code should be ready in about a day, and if you need any more coding assistance on other stuff, please feel free to reach out to me. I'd be happy to help out with your thesis or any other of these projects, I think it's really neat. – Brock Brown Mar 18 '23 at 22:17
  • @ChristophRackwitz This is an excellent suggestion, I will add it I finish up the code. – Brock Brown Mar 18 '23 at 22:18
  • @BrockBrown Thank you, I will contact you via your GitHub repository. – M Sandoval Mar 21 '23 at 23:35