<jupyter_start><jupyter_text>Module 2: Introduction to plant image analysis with PlantCV[PlantCV homepage](https://plantcv.danforthcenter.org/)[PlantCV documentation](https://plantcv.readthedocs.io/en/stable/)<jupyter_code>%matplotlib widget
import matplotlib
from plantcv import plantcv as pcv
matplotlib.rcParams["figure.max_open_warning"] = False
pcv.params.debug = "plot"
pcv.params.text_size = 5
pcv.params.text_thickness = 10
pcv.params.line_thickness = 10
pcv.__version__
# Open image file
img, imgpath, imgname = pcv.readimage(filename="cropped_plant.png")<jupyter_output><empty_output><jupyter_text>Color information can be represented using different models, or [color spaces](https://en.wikipedia.org/wiki/Color_space). Typical color images use an additive [RGB color model](https://en.wikipedia.org/wiki/RGB_color_model):![RGB color model](https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/AdditiveColor.svg/240px-AdditiveColor.svg.png)In printing, the subtractive [CMYK model](https://en.wikipedia.org/wiki/CMYK_color_model) is more typical:![CMYK color model](https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/CMYK_subtractive_color_mixing.svg/240px-CMYK_subtractive_color_mixing.svg.png)For image analysis and visual perception of color properties, other color models such as [Hue, Saturation, and Value (HSV)](https://en.wikipedia.org/wiki/HSL_and_HSV)or [CIELAB (LAB)](https://en.wikipedia.org/wiki/CIELAB_color_space) have advantages over RGB.![HSV color model](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Hsl-hsv_models.svg/240px-Hsl-hsv_models.svg.png)![LAB color model](https://upload.wikimedia.org/wikipedia/commons/thumb/3/33/Visible_gamut_within_CIELAB_color_space_D65_whitepoint_mesh.png/240px-Visible_gamut_within_CIELAB_color_space_D65_whitepoint_mesh.png)<jupyter_code># Visualize component HSV and LAB color spaces
cs = pcv.visualize.colorspaces(rgb_img=img, original_img=False)
# Convert the RGB image into a grayscale image by choosing one of the HSV or LAB channels
gray_img = pcv.rgb2gray_lab(rgb_img=img, channel="a")
# Visualize a histogram of the grayscale values to identify signal related to the plant and the background
hist = pcv.visualize.histogram(img=gray_img, bins=30)
# Use the histogram to set a binary threshold where the plant pixels will be labeled white and the background will be labeled black
bin_img = pcv.threshold.binary(gray_img=gray_img, threshold=100, max_value=255, object_type="dark")
# Instead of setting a manual threshold, try an automatic threshold method such as Otsu
bin_img = pcv.threshold.otsu(gray_img=gray_img, max_value=255, object_type="dark")
# Remove "salt" noise from the binary image
filter_bin = pcv.fill(bin_img=bin_img, size=100)
# Define a region of interest (ROI) where we expect to find a plant
roi, roi_str = pcv.roi.circle(img=img, x=222, y=244, r=150)
# Identify connected components (contours) using the binary image
cnt, cnt_str = pcv.find_objects(img=img, mask=filter_bin)
# Use the ROI and the contours to filter the contours based on overlap with the ROI
obj, obj_str, obj_mask, obj_area = pcv.roi_objects(img=img, roi_contour=roi, roi_hierarchy=roi_str, object_contour=cnt, obj_hierarchy=cnt_str)
# Combine the contours into a single plant object
plant, mask = pcv.object_composition(img=img, contours=obj, hierarchy=obj_str)
# Measure the size and shape of the plant
shape_img = pcv.analyze_object(img=img, obj=plant, mask=mask, label="plant1")
# Measure the color properties of the plant
color_hist = pcv.analyze_color(rgb_img=img, mask=mask, colorspaces="hsv", label="plant1")
# Save the measurements to a comma-separated value (CSV) file
pcv.outputs.save_results(filename="plant1_results.csv", outformat="csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1051792828685259, 'fraction_numerical': 0.014608233731739707, 'mean_word_length': 6.878661087866109, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '94584', 'license': 'MIT', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1234, 'n_words': 308, 'path': 'module2/reference_notebook.ipynb', 'repo_name': 'danforthcenter/nsf-iuse-hssu-intro', 'size': '3765'}