import cv2 import numpy as np # Load the image (assuming it's a binary image with edges) image = cv2.imread('path/to/edge_detected_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply morphological operations to clean up the edges kernel = np.ones((5, 5), np.uint8) closed_image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) # Find contours in the closed image contours, _ = cv2.findContours(closed_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Create a mask for the exterior contours mask = np.zeros_like(image) cv2.drawContours(mask, contours, -1, (255), thickness=cv2.FILLED) # Apply the mask to the original image to get only the exterior edges exterior_edges = cv2.bitwise_and(image, mask) # Display the result or save it to a file cv2.imshow('Exterior Edges', exterior_edges) cv2.waitKey(0) cv2.destroyAllWindows()