Fosip Expt 9
Fosip Expt 9
CLASS: TE COMPS A
EXPERIMENT 9
def morphological_boundary_extraction(image):
"""
Extract the boundary of objects in an image using
morphological operations.
As per the paper, boundary extraction can be done by
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Args:
image: Input binary image
Returns:
The boundary of objects in the image
"""
# Define a structuring element (as mentioned in the
paper)
# Using a 3x3 square structuring element
se = np.ones((3, 3), dtype=np.uint8)
# Perform erosion
eroded = morphology.erosion(image, se)
return boundary
def main():
# Specify the path to your local image
image_path = "man.jpg" # Change this to your image
file path
try:
# Check if file exists
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image file not
found: {image_path}")
plt.subplot(1, 3, 1)
plt.imshow(img, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(boundary, cmap='gray')
plt.title('Boundary using Morphology')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(sobel_edges, cmap='gray')
plt.title('Edge Detection using Sobel')
plt.axis('off')
plt.tight_layout()
plt.show()
except Exception as e:
print(f"Error loading image: {e}")
print("Using a test image instead...")
# Convert to binary
binary_img = img > 128
plt.subplot(1, 3, 1)
plt.imshow(img, cmap='gray')
plt.title('Test Image')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(boundary, cmap='gray')
plt.title('Boundary using Morphology')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(sobel_edges, cmap='gray')
plt.title('Edge Detection using Sobel')
plt.axis('off')
plt.tight_layout()
plt.show()
Conclusion:
This implementation shows that morphological operations effectively extract
boundaries in binary images. Key insights include: