The naive Bayes multiclass approach is an extension of the naive Bayes approach. It can be trained to output binary images given an input color image. Unlike the naive Bayes method, the naive Bayes multiclass approach can be trained to classify two or more classes, defined by the user. Additionally, the naive Bayes multiclass method is trained using colors sparsely sampled from images rather than the need to label all pixels in a given image.
To train the classifier, we need to build a table of red, green, and blue color values for pixels sampled evenly from each class. The idea here is to collect a relevant sample of pixel color data for each class. The size of the sample needed to build robust probability density functions for each class will depend on a number of factors, including the variability in class colors and imaging quality/reproducibility. To collect pixel color data we currently use the Pixel Inspection Tool in ImageJ. Each column in the tab-delimited table is a feature class (in this example, plant, pustule, chlorosis, or background) and each cell is a comma-separated red, green, and blue triplet for a pixel.
Once a satisfactory sample of pixels is collected, save the table as a tab-delimited text file. Use plantcv-train.py
to use the pixel samples to output probability density functions (PDFs)
for each class.
plantcv-train.py naive_bayes_multiclass --file pixel_samples.txt --outfile naive_bayes_pdfs.txt --plots
The output file from plantcv-train.py
will contain one row for each color channel (hue, saturation, and value) for
each class. The first and second column are the class and channel label, respectively. The
remaining 256 columns contain the p-value from the PDFs for each intensity value observable in an 8-bit image (0-255).
Once we have the plantcv-train.py
output file, we can classify pixels in a color image in PlantCV. In the example image for this tutorial we have already collected pixels and created the probability density functions for each class.
# Import libraries
import cv2
from plantcv import plantcv as pcv
class options:
def __init__(self):
self.image = "./img/color_image.jpg"
self.debug = "plot"
self.writeimg= False
self.result = "ml_tutorial_results.json"
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)
# Use the output file from `plantcv-train.py` to run the multiclass
# naive bayes classification on the image. The function below will
# print out 4 masks (plant, pustule, chlorosis, background)
# Inputs:
# rgb_img - RGB image data
# pdf_file - Output file containing PDFs from `plantcv-train.py`
mask = pcv.naive_bayes_classifier(rgb_img=img,
pdf_file="./img/machine_learning.txt")
# We can apply each mask to the original image to more accurately
# see what got masked
# Inputs:
# img - RGB or grayscale image data
# mask - Binary mask image data
# mask_color - 'white' or 'black'
plant_img = pcv.apply_mask(mask=(mask['plant']), img=img, mask_color='black')
pustule_img = pcv.apply_mask(mask=(mask['pustule']), img=img, mask_color='black')
chlorosis_img = pcv.apply_mask(mask=(mask['chlorosis']), img=img, mask_color='black')
background_img = pcv.apply_mask(mask=(mask['background']), img=img, mask_color='black')