Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

OpenCV Python Morphological Transformations Tutorial: Erosion, Dilation, Opening & Closing Explained

OpenCV Python – Morphological Transformations

Morphological transformations are powerful image processing techniques used to modify the structure of objects in an image. They are mainly applied to binary images to remove noise, fill gaps, and improve shape clarity.

In OpenCV Python, morphological operations are widely used in image segmentation, object detection, and preprocessing.


1. What are Morphological Transformations?

Morphological transformations are operations that process images based on their shapes.

They are mainly used for:

  • Removing noise
  • Filling holes
  • Separating objects
  • Enhancing shapes

These operations work using a kernel (structuring element).


2. Import OpenCV and NumPy

import cv2
import numpy as np

3. Read and Convert Image to Binary

Morphological operations work best on binary images.

img = cv2.imread("image.jpg", 0)

_, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

cv2.imshow("Binary Image", binary)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Create Kernel

A kernel defines the operation area.

kernel = np.ones((5, 5), np.uint8)

5. Erosion

Erosion removes white noise and shrinks objects.

erosion = cv2.erode(binary, kernel, iterations=1)

cv2.imshow("Erosion", erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Dilation

Dilation increases object size and fills gaps.

dilation = cv2.dilate(binary, kernel, iterations=1)

cv2.imshow("Dilation", dilation)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. Opening (Erosion + Dilation)

Opening removes small noise.

opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)

cv2.imshow("Opening", opening)
cv2.waitKey(0)
cv2.destroyAllWindows()

8. Closing (Dilation + Erosion)

Closing fills small holes.

closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

cv2.imshow("Closing", closing)
cv2.waitKey(0)
cv2.destroyAllWindows()

9. Morphological Gradient

Highlights object edges.

gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel)

cv2.imshow("Gradient", gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()

10. Types of Morphological Operations

  • Erosion → Shrinks objects
  • Dilation → Expands objects
  • Opening → Removes noise
  • Closing → Fills holes
  • Gradient → Edge detection

11. Why Morphological Transformations are Important

They are widely used in:

  • Image segmentation
  • Medical imaging
  • Object detection
  • OCR (text extraction)
  • Noise removal systems

12. Common Mistakes

❌ Using color image directly

✔ Solution:

cv2.imread("image.jpg", 0)

❌ Wrong kernel size

✔ Solution:

  • Use odd sizes like 3x3, 5x5

13. Conclusion

Morphological transformations in OpenCV Python are essential for cleaning and refining binary images. They help improve shape clarity and remove unwanted noise in computer vision tasks.

Once you master these techniques, you can move to advanced image segmentation and contour detection.




Post a Comment

0 Comments