from plantcv import plantcv as pcv
import numpy as np
# Throughout the other tutorials we use pcv.readimage to read in
# RGB and grayscale images
# Inputs
# filename - Image file to be read (possibly including a path)
# mode - Return mode of the image; either 'native' (default),
# 'rgb', 'gray', or 'csv'
img, path, filename = pcv.readimage(filename="./img/rgb_img.png")
# Notice that the image wasn't printed to the Jupyter notebook.
# We haven't set the debug mode yet, and it defaults to None.
# However, if each intermediate image isn't needed we can always
# manually plot out images.
pcv.plot_image(img)
# Similarly, we can print specific images manually.
pcv.print_image(img=img, filename='IO_img_example.jpg')
# Set debug to the global parameter
pcv.params.debug = 'plot'
img, path, filename = pcv.readimage(filename="./img/rgb_img.png")
# We can look at the shape of the image we read in. In this case
# (2055, 2454, 3) means that the image has dimensions 2055x2424
# and 3 color channels in the 3rd dimension
np.shape(img)
(2056, 2454, 3)
# Read in as grayscale.
img2, path2, filename2 = pcv.readimage(filename="./img/rgb_img.png", mode='gray')
np.shape(img2)
(2056, 2454)
# Read in a grayscale image
img3, path3, filename3 = pcv.readimage(filename="./img/grayscale_img.png")
np.shape(img3)
(254, 320)
# Read in grayscale image as RGB
img4, path4, filename4 = pcv.readimage(filename="./img/grayscale_img.png", mode='rgb')
np.shape(img4)
(254, 320, 3)
# There is also a function specifically designed to read images
# with Bayer mosaic pixel patterns and return a demosaicked image.
# Inputs:
# filename - Image file to be read (possibly including a path)
# bayerpattern - Arrangement of the pixels. Often found by trail
# and error. Either 'BG' (default), 'GB', 'RG', 'GR'
# alg - Algorithm with which to demosaic the image. Either 'default'
# 'EdgeAware', 'VariableNumberGradients'. Not case sensitive.
bayer_img = pcv.readbayer(filename="./img/bayer_img.png")
# There is a plotting method that can be used to examine the
# distribution of signal within an image.
# This will print out the histogram
# Inputs:
# img - Image data (most commonly grayscale), the original image for analysis.
# mask - Optional binary mask made from selected contours (default mask=None)
# bins - Number of class to divide spectrum into (default bins=256)
# color - color of the line drawn
# title - custom title for the plot gets drawn if title is not None
hist_figure = pcv.visualize.histogram(img=img2)
# The save results function will take the measurements stored when running any PlantCV analysis 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.outputs.save_results(filename='test_pipeline_results.txt')