pythonrimage-processingimage-segmentationroi

Is there a way to detect and quantify the missing area of objects in images?


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:

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.


Solution

  • 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.