The color correction module has been developed as a method of normalizing image-based data sets for more accurate image analysis. For simple input and output, a helper function plantcv.transform.correct_color was developed. We will use the function in the tutorial below but feel free to look at the documentation for more details on the steps that take place within the pcv.transform.correct_color
function.
Conditions
To run color correction on an image, the following are needed:
Target and source images must contain a reference from which color values are sampled. The following example uses a 24-color Colorchecker passport.
A target image (RGB) must be chosen. This image will be of the color profile to which other images will be corrected.
A source image (RGB), that will be corrected to the target image's color profile
A mask (gray-scale) of the target image in which background has value 0, and color chips from the colorchecker are labeled with unique values greater than zero, but less than 255.
A mask (gray-scale) of the source image labeled consistently with the target image's mask.
from plantcv import plantcv as pcv
class options:
def __init__(self):
self.debug = "plot"
self.writeimg= False
self.result = "color_tutorial_results.json"
self.outdir = "."
# Get options
args = options()
# Set debug to the global parameter
pcv.params.debug = args.debug
# Read in source and target images
# Inputs:
# filename - Image file to be read in
# mode - Return mode of image; either 'native' (default), 'rgb', 'gray', or 'csv'
target_img, t_path, t_filename = pcv.readimage(filename="./img/target_image.jpg")
source_img, s_path, s_filename = pcv.readimage(filename="./img/source1_img.jpg")
# Create a labeled color card mask, first use color card finder function
# This won't print anything out but you can look at the dataframe output
# to see the chips that the function found.
# Inputs:
# rgb_img - RGB image data containing color card
# threshold - Optional threshold method; either 'adaptgauss' (default), 'normal', or 'otsu'
# threshvalue - Optional threhsolding value (default threshvalue = 125)
# blurry - Optional boolean; False (default) or if True then image sharpening is applied
# background - Optional type of image background; 'dark' (default) or 'light'
dataframe1, start, space = pcv.transform.find_color_card(rgb_img=target_img, background='light')
# Make the labeled mask of the target image
# Inputs:
# rgb_img - RGB image data containing color card
# radius - Radius of color card chips (masks make circles on chips)
# start_coord - Two-element tuple of the first chip mask, (starting x, starting y)
# spacing - Two-element tuple of the horizontal and vertical spacing between chip masks
# nrows - Number of chip rows
# ncols - Number of chip columns
# exclude - Optional list of chips to exclude. List largest to smallest index
target_mask = pcv.transform.create_color_card_mask(target_img, radius=15, start_coord=start,
spacing=space, nrows=6, ncols=4)
# Our color card chip appears to be in relatively the same position in both the source and target images
# Try using the same parameters and check to make sure it's appropriate for the source image
source_mask = pcv.transform.create_color_card_mask(source_img, radius=10, start_coord=start,
spacing=space, nrows=6, ncols=4)