opencv - How to Get Image's Object Outline (Outer Boundary) in Python? -
i'm working on image segmentation using python , opencv right now. have binary image contains 1 object (already thresholded using otsu's methods). want know how image's object outline (outer boundary). so, there black image white object outline. tried googling still don't have idea.
i prefer know how manually without built in function.
built in function: findcontours().
example:
import numpy np import matplotlib.pyplot plt = np.zeros((100,100), np.uint8) a[10:20,30:40] = 1 im2, contours, hierarchy = cv2.findcontours(a, cv2.retr_tree, cv2.chain_approx_simple) cim = np.zeros_like(a) cv2.drawcontours(cim, contours, -1, 255, 1) plt.matshow(cim, cmap=plt.cm.gray)
manual approach: easy way subtracting eroded image original image using binary_erosion(). not necesarily result in closed contour, depending on geometry.
import numpy np import matplotlib.pyplot plt scipy.ndimage.morphology import binary_erosion = np.zeros((100,100), np.uint8) a[10:20,30:40] = 1 m = - binary_erosion(a) plt.matshow(m, cmap=plt.cm.gray)
Comments
Post a Comment