def canny(image): blurred_image = gaussian_blur(image, kernel_size=9) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) gradient_magnitude, gradient_direction = sobel_edge_detection(blurred_image, edge_filter) new_image = non_max_suppression(gradient_magnitude, gradient_direction) weak = 50 new_image = threshold(new_image, 5, 20, weak) new_image = hysteresis(new_image, weak) new_image = new_image.astype(np.uint8) kernel = np.ones((5, 5)) img_dil = cv2.dilate(new_image, kernel, iterations=1) form = getContours(img_dil, image, image) return form
plt.show() if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") ap.add_argument("-v", "--verbose", type=bool, default=False, help="Path to the image") args = vars(ap.parse_args()) image = cv2.imread(args["image"], 0) blurred_image = gaussian_blur(image, kernel_size=25, verbose=False) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) new_image_x, new_image_y, gradient_magnitude, gradient_direction = sobel_edge_detection( blurred_image, edge_filter, convert_to_degree=True, verbose=args["verbose"]) new_image = non_max_suppression(gradient_magnitude, gradient_direction, verbose=args["verbose"]) weak = 50
if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") ap.add_argument("-v", "--verbose", type=bool, default=False, help="Path to the image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) blurred_image = gaussian_blur(image, kernel_size=9, verbose=args["verbose"]) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) gradient_magnitude, gradient_direction = sobel_edge_detection( blurred_image, edge_filter, convert_to_degree=True, verbose=args["verbose"]) new_image = non_max_suppression(gradient_magnitude, gradient_direction, verbose=args["verbose"]) weak = 50
#kernel = np.ones((4, 3), np.uint8) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # Performing an opening opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) # Performing a closing closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2) # Displaying result of opening cv2.imshow('Opening', opening) cv2.waitKey() # Displaying result of closing cv2.imshow('Closing', closing) cv2.waitKey() blurred_image = gaussian_blur(closing, kernel_size=9, verbose=True) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) gradient_magnitude, gradient_direction = sobel_edge_detection( blurred_image, edge_filter, convert_to_degree=True, verbose=True) new_image = non_max_suppression(gradient_magnitude, gradient_direction, verbose=True) weak = 50 new_image = threshold(new_image, 5, 20, weak=weak, verbose=True) new_image = hysteresis(new_image, weak) new = cv2.convertScaleAbs(new_image, cv2.CV_8UC1)
final_image = top_to_bottom + bottom_to_top + right_to_left + left_to_right final_image[final_image > 255] = 255 return final_image if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") ap.add_argument("-s", "--steps", type=bool, default=False, help="Show all the steps") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) blurred_image = gaussian_blur(image, kernel_size=9, steps=False) edge_filter = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) gradient_magnitude, gradient_direction = sobel_edge_detection(blurred_image, edge_filter, convert_to_degree=True, steps=args["steps"]) new_image = non_max_suppression(gradient_magnitude, gradient_direction, steps=args["steps"]) weak = 50 new_image = threshold(new_image, 5, 20, weak=weak, steps=args["steps"]) new_image = hysteresis(new_image, weak) plt.imshow(new_image, cmap='gray') plt.title("Canny Edge Detector by Vito Kurnik")