Automatically Detect a Color Card with cv2.mcc tools¶
Automatically detects a Macbeth ColorChecker and creates a labeled mask.
This uses a different process from pcv.transform.detect_color_card and may be
useful for images that have multiple color cards or color card like objects in inconsistent locations.
plantcv.transform.mcc_detect(rgb_img, color_chip_size=None, roi=None, delta_E=True, **kwargs)
returns color_matrix
-
Parameters
- rgb_img - Input RGB image data containing a color card.
- roi - Optional rectangular ROI as returned by
pcv.roi.rectanglewithin which to look for the color card. (default = None) - delta_E - Boolean, should Delta E be calculated between the observed and expected color card values? This will add delta E values to
outputs.metadata. Seepcv.transform.deltaE - **kwargs - Other keyword arguments passed to
cv2.mcc.CheckerDetector.process.- chartType - int, code for type of chart (defaults to
cv2.mcc.MCC24, which is 0) - nc - int, number of charts to detect (defaults to 1)
- useNet - bool, should a neural network be used to detect the chart? (defaults to False)
- params -
cv2.mcc.DetectorParametersobject describing parameters for card detection (defaults tocv2.mcc.DetectorParameters.create())
- chartType - int, code for type of chart (defaults to
-
Returns
- color_matrix - Detected color values as a matrix, the same format as output from
pcv.transform.get_color_matrix).
- color_matrix - Detected color values as a matrix, the same format as output from
-
Context
- In general we advocate for using
pcv.transform.auto_correct_colorfor simple color correction rather thanmcc_detectorpcv.transform.detect_color_card. - One setting where this function is a better option is if there are multiple color cards or color card like objects, since
cv2.mccdetectors will find all matching regions then attempt to pick the best color card from avaiable options. This may be useful for some images where there are multiple color cards or other potentially confounding objects present. - If either this function or
pcv.transform.detect_color_cardare proving difficult to implement then we suggest at least trying the other function since they will work in slightly different ways to return the same output. - Debug images similar to those from
pcv.transform.detect_color_cardare generated
- In general we advocate for using
Note
Unlike pcv.transform.detect_color_card this function does not support astrobotany color cards or size scaling based on color chips. cv2.mcc does not return the dimensions of color chips so this function does not serve the dual-purpose that other detection methods will allow for.
Original Image
Here the pcv.transform.detect_color_card may struggle without an ROI that excludes the extra color card side, so we use mcc_detect.

from plantcv import plantcv as pcv
rgb_img, path, filename = pcv.readimage("target_img.png")
# Detecting a color card
cc_matrix = pcv.transform.mcc_detect(rgb_img=rgb_img)
MCC Color Card Detection

# When using mcc_detect, as with detect_color_card, you will always set pos=3
tgt_matrix = pcv.transform.std_color_matrix(pos=3)
corrected_img = pcv.transform.affine_color_correction(rgb_img=rgb_img,
source_matrix=cc_matrix,
target_matrix=tgt_matrix)
Image After MCC Correction

Source Code: Here