-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-leaf_segmentation.py
72 lines (57 loc) · 2.63 KB
/
4-leaf_segmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from plantcv import plantcv as pcv
import cv2
class options:
def __init__(self):
self.image = "~/samples/col_1_25.png"
self.debug = "plot"
self.writeimg = False
self.outdir = "~/results"
# Get options
args = options()
# Set debug to the global parameter
pcv.params.debug = args.debug
# Read image
# Inputs:
# filename - Image file to be read in
# mode - Return mode of image; either "native" (default), "rgb", "gray", or "csv"
img, path, filename = pcv.readimage(filename=args.image)
# Crop the image down to focus on just one plant
crop_img = img[100:550,1000:1750]
pcv.plot_image(crop_img)
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
# remove the excessive noise using THRESHOLD
thresh, img_binary = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
pcv.plot_image(img_binary)
# Find objects
# Inputs:
# img = image that the objects will be overlayed
# mask = what is used for object detection
id_objects, obj_hierarchy = pcv.find_objects(img=crop_img, mask=img_binary)
# Combine objects
# Inputs:
# img = RGB or grayscale image data for plotting
# contours = contour list
# hierarchy = contour hierarchy array
obj, mask = pcv.object_composition(img=crop_img, contours=id_objects, hierarchy=obj_hierarchy)
# Apply mask
# Inputs:
# img = RGB or grayscale image data
# mask = binary mask image data
# mask_color = "white" or "black"
masked = pcv.apply_mask(img=crop_img, mask=mask, mask_color="black")
# Use watershed segmentation
# Inputs:
# rgb_img = RGB image data
# mask = binary image, single channel, object in white and background in black
# distance = minimum distance of local maximum, lower values are more sensitive,
# and segments more objects (default: 10)
# label = optional label parameter, modifies the variable name of observations recorded. (default `label="default"`)filled_img = pcv.morphology.fill_segments(mask=cropped_mask, objects=edge_objects)
analysis_images = pcv.watershed_segmentation(rgb_img=crop_img, mask=mask, distance=15, label="default")
# 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='segmentation_results.json')
import pandas as pd
pdObj = pd.read_json('segmentation_results.json', orient="index")
print(pdObj)