There is a PlantCV function is based on code contributed by Suxing Liu, Arkansas State University. For more information see https://github.com/lsx1980/Leaf_count. This function uses the watershed algorithm to detect boundary of objects. Needs a mask file which specifies area which is object is white, and background is black. Requires cv2 version 3.0+
from plantcv import plantcv as pcv
class options:
def __init__(self):
self.image = "img/tutorial_images/segmentation/arabidopsis.jpg"
self.debug = "plot"
self.writeimg= False
self.outdir = "."
# Get options
args = options()
# Set debug to the global parameter
pcv.params.debug = args.debug
# Read image
# Inputs:
# filename - Image file to be read in
# mode - Return mode of image; either 'native' (default), 'rgb', 'gray', or 'csv'
img, path, filename = pcv.readimage(filename=args.image)
# Crop the image down to focus on just one plant
crop_img = img[525:900,2030:2400]
# Print it out to see
pcv.plot_image(crop_img)
# Convert image from RGB color space to LAB and keep only the
# green-magenta channel
# Inputs:
# rgb_img = image object, RGB color space
# channel = color subchannel ('l' = lightness, 'a' = green-magenta , 'b' = blue-yellow)
a = pcv.rgb2gray_lab(rgb_img=crop_img, channel='a')
# Set a binary threshold on the image
# Inputs:
# gray_img = img object, grayscale
# threshold = threshold value (0-255)
# max_value = value to apply above threshold (usually 255 = white)
# object_type = light or dark
# - If object is light then standard thresholding is done
# - If object is dark then inverse thresholding is done
img_binary = pcv.threshold.binary(gray_img=a, threshold=110, max_value=255, object_type='dark')
# Find objects
# Inputs:
# img = image that the objects will be overlayed
# mask = what is used for object detection
id_objects, obj_hierarchy = pcv.find_objects(img=crop_img, mask=img_binary)
# Combine objects
# Inputs:
# img = RGB or grayscale image data for plotting
# contours = Contour list
# hierarchy = Contour hierarchy array
obj, mask = pcv.object_composition(img=crop_img, contours=id_objects, hierarchy=obj_hierarchy)
# Appy mask
# Inputs:
# rgb_img = RGB image data
# mask = Binary mask image data
# mask_color = 'white' or 'black'
masked = pcv.apply_mask(rgb_img=crop_img, mask=mask, mask_color="black")
# Use watershed segmentation
# Inputs:
# rgb_img = RGB image data
# mask = Binary image, single channel, object in white and background black
# distance = Minimum distance of local maximum, lower values are more sensitive,
# and segments more objects (default: 10)
analysis_images = pcv.watershed_segmentation(rgb_img=masked, mask=mask, distance=15)
# The print_results function will take the measurements stored when running any (or all) of these functions, format,
# and print an output text file for data analysis. The Outputs class stores data whenever any of the following functions
# are ran: analyze_bound_horizontal, analyze_bound_vertical, analyze_color, analyze_nir_intensity, analyze_object,
# fluor_fvfm, report_size_marker_area, watershed. If no functions have been run, it will print an empty text file
pcv.print_results(filename='segmentation_tutorial_results.txt')
To view and/or download the text file output (saved in JSON format)...
Check out documentation on how to convert JSON format output into table formatted output. Depending on the analysis steps a PlantCV user may have two CSV files (single value traits and multivalue traits).