YouTip LogoYouTip

Opencv Image Morphological Operations

Image morphological operations are an important technique in image processing, mainly used for processing binary images (i.e., black and white images). Image morphological operations in OpenCV are important tools in image processing. Through erosion, dilation, opening, closing, and morphological gradient operations, effects such as noise removal, object separation, and edge detection can be achieved. Mastering these operations helps better process and analyze image data. The following are the common morphological operations and their functions in OpenCV: | **Operation** | **Function** | **Description** | **Application Scenario** | | --- | --- | --- | --- | | **Erosion** | `cv2.erode()` | Scans the image with a structuring element; if the area covered by the structuring element is entirely foreground, the center pixel is retained. | Noise removal, object separation. | | **Dilation** | `cv2.dilate()` | Scans the image with a structuring element; if any foreground exists in the area covered by the structuring element, the center pixel is retained. | Connecting broken objects, filling holes. | | **Opening** | `cv2.morphologyEx()` | Erosion followed by dilation. | Removing small objects, smoothing object boundaries. | | **Closing** | `cv2.morphologyEx()` | Dilation followed by erosion. | Filling small holes, connecting nearby objects. | | **Morphological Gradient** | `cv2.morphologyEx()` | Dilation image minus erosion image. | Extracting object edges. | | **Top Hat** | `cv2.morphologyEx()` | Original image minus opening result. | Extracting small bright objects against background. | | **Black Hat** | `cv2.morphologyEx()` | Closing result minus original image. | Extracting small dark objects against background. | ## 1. Erosion (cv2.erode()) Erosion is a process that shrinks the foreground objects in an image. Erosion works by convolving the structuring element with the image. The center pixel is retained only when the structuring element completely covers the foreground pixels in the image; otherwise, it gets eroded away. ### Function Prototype cv2.erode(src, kernel, iterations=1) * `src`: Input image, typically a binary image. * `kernel`: Structuring element, can be custom or generated using `cv2.getStructuringElement()`. * `iterations`: Number of erosion iterations, default is 1. ## Example import cv2 import numpy as np # Read image image = cv2.imread('image.png',0) # Define structuring element kernel = np.ones((5,5), np.uint8) # Erosion operation eroded_image = cv2.erode(image, kernel, iterations=1) # Display result cv2.imshow('Eroded Image', eroded_image) cv2.waitKey(0) cv2.destroyAllWindows() Erosion causes foreground objects in the image to become smaller, with edges being eroded away. It is commonly used for noise removal or separating connected objects. ## 2. Dilation (cv2.dilate()) Dilation is the opposite of erosion; it is a process that expands the foreground objects in an image. Dilation works by convolving the structuring element with the image. The center pixel is retained as long as the structuring element overlaps with any foreground pixel in the image. ### Function Prototype cv2.dilate(src, kernel, iterations=1) * `src`: Input image, typically a binary image. * `kernel`: Structuring element, can be custom or generated using `cv2.getStructuringElement()`. * `iterations`: Number of dilation iterations, default is 1. ## Example import cv2 import numpy as np # Read image image = cv2.imread('image.png',0) # Define structuring element kernel = np.ones((5,5), np.uint8) # Dilation operation dilated_image = cv2.dilate(image, kernel, iterations=1) # Display result cv2.imshow('Dilated Image', dilated_image) cv2.waitKey(0) cv2.destroyAllWindows() Dilation causes foreground objects in the image to become larger, with edges being expanded. It is commonly used to fill holes in foreground objects or connect broken objects. ## 3. Opening (cv2.morphologyEx() with cv2.MORPH_OPEN) Opening is a combined operation of erosion followed by dilation. Opening is mainly used to remove small noise in images or separate connected objects. ### Function Prototype cv2.morphologyEx(src, op, kernel) * `src`: Input image, typically a binary image. * `op`: Morphological operation type, use `cv
← Opencv Image Contour DetectionOpencv Image Thresholding β†’