def draw_tip_leaf(np_image, coef_x=1, coef_y=1): leaf_image, leaf_mask = extract_leaf(np_image) centroid = find_centroid(np_image) contours = find_external_contours(np_image) contours = find_good_contours(contours, MIN_CONTOUR_PIXELS) lengths = [] for contour in contours: lengths_of_contour = [] for pixel in contour: point = tuple(pixel[0]) length = math.sqrt((coef_x * (centroid[1] - point[0]))**2 + (coef_y * (centroid[0] - point[1]))**2) lengths_of_contour.append(length) lengths.append(lengths_of_contour) length_leaves = [] locates = [] for length in lengths: idx = [i for i in range(len(length))] f = np.polynomial.chebyshev.Chebyshev.fit( padding_idx(idx, len(idx) // 2), padding_length(length, len(length) // 2), choose_deg(length)) value, locate = find_extreme(f, idx) length_leaves += [length[i] for i in locate] locates.append(locate) length_average = sum(length_leaves) / len(length_leaves) tip_locates = get_tip_locates(locates, contours) draw_redlines(np_image, tip_locates)
def find_green_fixels(np_image): leaf_image, leaf_mask = extract_leaf(np_image) green_pixels = [] for i in range(leaf_mask.shape[0]): for j in range(leaf_mask.shape[1]): if leaf_mask[i, j] != 0: green_pixels.append((i, j)) return green_pixels
def find_contours(np_image): leaf_image, leaf_mask = extract_leaf(np_image) edges = cv2.Canny(leaf_mask, 100, 1200) edges_pixels = [] for i in range(edges.shape[0]): for j in range(edges.shape[1]): if edges[i, j] != 0: edges_pixels.append((i, j)) return edges_pixels
def find_external_contours(np_image): leaf_image, leaf_mask = extract_leaf(np_image) ret, thresh = cv2.threshold(leaf_mask, 100, 255, 0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) return contours