Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param gray_img: numpy.ndarray
:param min_value: int
:param max_value: int
:return c: numpy.ndarray
"""
params.device += 1
if len(np.shape(gray_img)) != 2:
fatal_error("Image is not grayscale")
rescaled_img = np.interp(gray_img, (gray_img.min(), gray_img.max()), (min_value, max_value))
rescaled_img = (rescaled_img).astype('uint8')
if params.debug == 'print':
print_image(rescaled_img, os.path.join(params.debug_outdir, str(params.device) + "_rescaled.png"))
elif params.debug == 'plot':
plot_image(rescaled_img, cmap='gray')
return rescaled_img
kernel = Kernel size (int). A k x k kernel will be built. Must be greater than 1 to have an effect.
i = iterations, i.e. number of consecutive filtering passes
Returns:
dil_img = dilated image
:param gray_img: numpy.ndarray
:param kernel: int
:param i: int
:return dil_img: numpy.ndarray
"""
kernel1 = int(kernel)
kernel2 = np.ones((kernel1, kernel1), np.uint8)
dil_img = cv2.dilate(src=gray_img, kernel=kernel2, iterations=i)
params.device += 1
if params.debug == 'print':
print_image(dil_img, os.path.join(params.debug, str(params.device) + '_dil_image_' + 'itr_' + str(i) + '.png'))
elif params.debug == 'plot':
plot_image(dil_img, cmap='gray')
return dil_img
img_copy = np.zeros((iy, ix, 3), dtype=np.uint8)
rand_color = color_palette(len(coordlist))
for i, x in enumerate(coordlist):
for a in x:
if roi_obj_hierarchy[0][a][3] > -1:
pass
else:
cv2.drawContours(img_copy, roi_objects, a, rand_color[i], -1, hierarchy=roi_obj_hierarchy)
if show_grid:
for y in rbreaks:
cv2.line(img_copy, (0, y), (ix, y), (255, 0, 0), params.line_thickness)
for x in cbreaks:
cv2.line(img_copy, (x, 0), (x, iy), (255, 0, 0), params.line_thickness)
if params.debug=='print':
print_image(img_copy, os.path.join(params.debug_outdir, str(params.device) + '_clusters.png'))
elif params.debug=='plot':
plot_image(img_copy)
return grouped_contour_indexes, contours, roi_obj_hierarchy
Inputs:
gray_img = grayscale image to analyze
mask = binary mask made from selected contours
bins = number of classes to divide spectrum into
color = color of the line drawn
title = custom title for the plot gets drawn if title is not None
:param gray_img: numpy.ndarray
:param mask: numpy.ndarray
:param bins: int
:param color: str
:param title: str
:return fig_hist: ggplot
"""
params.device += 1
debug = params.debug
# Apply mask if one is supplied
if mask is not None:
# apply plant shaped mask to image
params.debug=None
mask1 = binary_threshold(mask, 0, 255, 'light')
mask1 = (mask1 / 255)
masked = np.multiply(gray_img, mask1)
else:
masked = gray_img
if gray_img.dtype == 'uint16':
maxval = 65536
else:
maxval = 256
def _draw_roi(img, roi_contour):
"""Draw an ROI
:param img: numpy.ndarray
:param roi_contour: list
"""
# Make a copy of the reference image
ref_img = np.copy(img)
# If the reference image is grayscale convert it to color
if len(np.shape(ref_img)) == 2:
ref_img = cv2.cvtColor(ref_img, cv2.COLOR_GRAY2BGR)
# Draw the contour on the reference image
cv2.drawContours(ref_img, roi_contour, -1, (255, 0, 0), params.line_thickness)
if params.debug == "print":
# If debug is print, save the image to a file
print_image(ref_img, os.path.join(params.debug_outdir, str(params.device) + "_roi.png"))
elif params.debug == "plot":
# If debug is plot, print to the plotting device
plot_image(ref_img)
h = label_coord_y[i]
cv2.putText(img=labeled_img, text=text, org=(w, h), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=.4,
color=(150, 150, 150), thickness=1)
segment_label = "ID" + str(i)
curvature_header.append(segment_label)
curvature_data.append(curvature_measure[i])
outputs.measurements['morphology_data']['segment_curvature'] = curvature_measure
# Reset debug mode
params.debug = debug
# Auto-increment device
params.device += 1
if params.debug == 'print':
print_image(labeled_img, os.path.join(params.debug_outdir, str(params.device) + '_segment_curvature.png'))
elif params.debug == 'plot':
plot_image(labeled_img)
return curvature_header, curvature_data, labeled_img
# Calculate geodesic distance
text = "{:.3f}".format(curvature_measure[i])
w = label_coord_x[i]
h = label_coord_y[i]
cv2.putText(img=labeled_img, text=text, org=(w, h), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=.4,
color=(150, 150, 150), thickness=1)
segment_label = "ID" + str(i)
curvature_header.append(segment_label)
curvature_data.append(curvature_measure[i])
outputs.measurements['morphology_data']['segment_curvature'] = curvature_measure
# Reset debug mode
params.debug = debug
# Auto-increment device
params.device += 1
if params.debug == 'print':
print_image(labeled_img, os.path.join(params.debug_outdir, str(params.device) + '_segment_curvature.png'))
elif params.debug == 'plot':
plot_image(labeled_img)
return curvature_header, curvature_data, labeled_img
# Once a segment is complete (traverses from stem to leaf tip), add to new leaf objects list
new_leaf_obj.append(combined_segment)
# Create a plot reflecting new leaf objects to show how they got combined
rand_color = color_palette(len(new_leaf_obj))
labeled_img = cv2.cvtColor(plotting_img, cv2.COLOR_GRAY2RGB)
for i, cnt in enumerate(new_leaf_obj):
labeled_img = cv2.drawContours(labeled_img, cnt, -1, rand_color[i],
params.line_thickness, lineType=8)
# Reset debug mode
params.debug = debug
# Auto-increment device
params.device += 1
if params.debug == 'print':
print_image(labeled_img,
os.path.join(params.debug_outdir, str(params.device) + '_auto_combined_segments.png'))
elif params.debug == 'plot':
plot_image(labeled_img)
return labeled_img, new_leaf_obj
rand_color = color_palette(num_cycles)
for i, cnt in enumerate(cycle_objects):
cv2.drawContours(cycle_img, cycle_objects, i, rand_color[i], params.line_thickness, lineType=8,
hierarchy=cycle_hierarchies)
# Store Cycle Data
cycle_header = ['HEADER_CYCLE', 'num_cycles']
cycle_data = ['CYCLE_DATA', num_cycles]
if 'morphology_data' not in outputs.measurements:
outputs.measurements['morphology_data'] = {}
outputs.measurements['morphology_data']['num_cycles'] = num_cycles
# Reset debug mode
params.debug = debug
# Auto-increment device
params.device += 1
if params.debug == 'print':
print_image(cycle_img, os.path.join(params.debug_outdir, str(params.device) + '_cycles.png'))
elif params.debug == 'plot':
plot_image(cycle_img)
return cycle_header, cycle_data, cycle_img
Returns:
er_img = eroded image
:param gray_img: numpy.ndarray
:param kernel: int
:param i: int
:return er_img: numpy.ndarray
"""
kernel1 = int(kernel)
kernel2 = np.ones((kernel1, kernel1), np.uint8)
er_img = cv2.erode(src=gray_img, kernel=kernel2, iterations=i)
params.device += 1
if params.debug == 'print':
print_image(er_img, os.path.join(params.debug_outdir,
str(params.device) + '_er_image_' + 'itr_' + str(i) + '.png'))
elif params.debug == 'plot':
plot_image(er_img, cmap='gray')
return er_img