Edge linking and boundary detection are fundamental techniques in digital image processing and computer vision for identifying the boundaries or edges of objects within an image. These methods are used to segment an image into distinct regions corresponding to different objects or parts of objects.
The first step is to apply an edge detection operator to the image to identify pixels where there are sharp intensity changes or discontinuities. Common edge detection algorithms include:
The output is typically a binary image with pixels marked as edge (1) or non-edge (0). However, edges are usually thick and disconnected after applying these operators.
The gradient of an image at location is given by:
Where and are the partial derivatives in the x and y directions respectively. These can be approximated by convolving with derivative filters such as:
Sobel Operators:
The gradient magnitude is used to identify edge pixels as those exceeding a threshold.
The LoG computationally approximates the second derivative, finding zero-crossings which indicate edges.
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('sample.jpg',0) edges = cv2.Canny(img,100,200) plt.subplot(121),plt.imshow(img,cmap='gray') plt.title('Original Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(edges,cmap='gray') plt.title('Edge Image'), plt.xticks([]), plt.yticks([]) plt.show()
Once we have a binary edge image, the next step is to link the edges together into coherent edge chains or curves corresponding to the boundaries of objects. Common algorithms include:
The output is a list of linked edge point coordinates representing the object boundaries.
The Hough transform maps edge points to parameters of lines:
Curves in the image map to points in the parameter space. Boundaries correspond to peaks in the accumulator array counting co-linear points.
The extracted boundaries are often thick after linking due to the initial edge operator response. So a final thinning step is applied to obtain thin, one pixel-wide boundary curves. Popular thinning algorithms include:
Thinning iteratively removes outer boundary pixels if certain conditions are met, based on consecutive neighbors in a square neighborhood .
Common algorithms evaluate a hit-or-miss transform kernel to identify removable pixels while preserving connectivity.
import cv2 import numpy as np img = cv2.imread('sample.jpg',0) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) edges = cv2.Canny(img,100,200) dilated = cv2.dilate(edges,kernel,iterations=1) eroded = cv2.erode(dilated,kernel,iterations=1) result = cv2.bitwise_and(dilated,eroded) cv2.imshow('Thinned Edges',result) cv2.waitKey(0) cv2.destroyAllWindows()
Boundary detection has many applications in image analysis, including:
While conceptually simple, reliable boundary detection remains a challenging problem, especially for objects with complex, irregular shapes or in the presence of noise, occlusions, and other artifacts.