Pixel count within user define range #687
Unanswered
AshokMandal
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi @AshokMandal, in PlantCV we measure shape and size parameters from object contours (the outlines of connected components derived from a mask). These measurements include area. Starting with your mask, the process would look like this: # Assumes input image is stored in a variable called "img" and your mask is a variable called "mask"
from plantcv import plantcv as pcv
# Identify contours in the binary mask
contours, contour_str = pcv.find_objects(img=img, mask=mask)
# Combine contours into a single object
plant_obj, plant_mask = pcv.object_composition(img=img, contours=contours, hierarchy=contour_str)
# Measure shape and size
shape_img = pcv.analyze_object(img=img, obj=plant_obj, mask=plant_mask)
# Pull plant area out of the collected measurements
# Note that this code relies on v3.11 that will be released shortly, for older versions remove ["default"]
plant_area = pcv.outputs.observations["default"]["area"]["value"]
# Image size
y, x = img.shape[:2]
image_area = y * x
# Proportion plant area
prop = plant_area / image_area
# Store custom measurement
# Note that this code relies on v3.11 that will be released shortly, for older versions remove sample="default"
pcv.outputs.add_observation(sample="default", variable="proportion_plant", trait="proportion plant area",
method="ratio of pixels", scale="proportion", datatype=float, value=prop, label="proportion")
pcv.print_results(filename="results.txt") The above is compatible with parallel image analysis with PlantCV. However, if you just want to measure plant area more directly to calculate the proportion you can use NumPy with the mask you already have. import numpy as np
# Count white pixels to determine plant area
plant_area = np.count_nonzero(mask)
# Image size
y, x = mask.shape
image_area = y * x
# Proportion plant area
prop = plant_area / image_area |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to extract the live plant parts in the images. I am looking for counting the number of pixels within a user define range in any color space and finally dividing that number of pixels by total number of pixels to get percentage in each images. I am able to mask the image by thresholding but could not find any function to count the area of masked part in the image. Is it possible to do in PlantCV???
Beta Was this translation helpful? Give feedback.
All reactions