This answer was inspired by this excellent post.

import numpy as np
import cv2

if __name__ == '__main__':

    image = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)

    binary = cv2.bitwise_not(binary)

    H = cv2.Sobel(binary, cv2.CV_8U, 0, 2)
    V = cv2.Sobel(binary, cv2.CV_8U, 2, 0)

    rows,cols = image.shape[:2]

    _,contours,_ = cv2.findContours(V, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        # rows/3 is the threshold for length of line
        if h > rows/3:
            cv2.drawContours(V, [cnt], -1, 255, -1)
            cv2.drawContours(binary, [cnt], -1, 255, -1)
        else:
            cv2.drawContours(V, [cnt], -1, 0, -1)

    _,contours,_ = cv2.findContours(H, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        # cols/3 is the threshold for length of line
        if w > cols/3:
            cv2.drawContours(H, [cnt], -1, 255, -1)
            cv2.drawContours(binary, [cnt], -1, 255, -1)
        else:
            cv2.drawContours(H, [cnt], -1, 0, -1)

    kernel = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(3,3))
    H = cv2.morphologyEx(H, cv2.MORPH_DILATE, kernel,iterations = 3)
    V = cv2.morphologyEx(V, cv2.MORPH_DILATE, kernel, iterations = 3)

    cross = cv2.bitwise_and(H, V)

    _,contours,_ = cv2.findContours(cross,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    centroids = []
    for cnt in contours:
        mom = cv2.moments(cnt)
        (x,y) = mom['m10']/mom['m00'], mom['m01']/mom['m00']
        cv2.circle(image,(int(x),int(y)),4,(0,255,0),-1)
        centroids.append((x,y))

    centroids.sort(key = lambda x: x[0], reverse = False)
    centroids.sort(key = lambda x: x[1], reverse = False)

    dx = int(centroids[1][0] - centroids[0][0])
    centroids = np.array(centroids, dtype = np.float32)
    (x,y,w,h) = cv2.boundingRect(centroids)

    if x-dx > -5: x = max(x-dx,0)
    if h+dx <= rows+5: h = min(h+dx,rows)
    if w+dx <= cols+5: w = min(w+dx,cols)
    cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0))

    roi = binary[y:y+h,x:x+w]

    roi = cv2.morphologyEx(roi, cv2.MORPH_OPEN, kernel,iterations = 1)

    cv2.imshow('image', image)
    cv2.imshow('roi', roi)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Answer from zindarod on Stack Overflow
🌐
Python Programming
pythonprogramming.net › morphological-transformation-python-opencv-tutorial
Morphological Transformations OpenCV Python Tutorial
cap = cv2.VideoCapture(1) while(1): _, frame = cap.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_red = np.array([30,150,50]) upper_red = np.array([255,255,180]) mask = cv2.inRange(hsv, lower_red, upper_red) res = cv2.bitwise_and(frame,frame, mask= mask) kernel = np.ones((5,5),np.uint8) opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) cv2.imshow('Original',frame) cv2.imshow('Mask',mask) cv2.imshow('Opening',opening) cv2.imshow('Closing',closing) k = cv2.waitKey(5) & 0xFF if k == 27: break cv2.destroyAllWindows() cap.release()
🌐
PyTutorial
pytutorial.com › python-opencv-cv2morphologyex-guide
PyTutorial | Python OpenCV cv2.morphologyEx() Guide
January 16, 2025 - Learn how to use Python OpenCV cv2.morphologyEx() for advanced image processing. Includes examples, code, and explanations for beginners.
🌐
OpenCV
docs.opencv.org › 4.13.0 › d9 › d61 › tutorial_py_morphological_ops.html
OpenCV: Morphological Transformations
void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
🌐
PyImageSearch
pyimagesearch.com › home › blog › opencv morphological operations
OpenCV Morphological Operations - PyImageSearch
May 9, 2021 - The first required argument of cv2.morphologyEx is the image we want to apply the morphological operation to. The second argument is the actual type of morphological operation — in this case, it’s an opening operation.
Top answer
1 of 1
8

This answer was inspired by this excellent post.

import numpy as np
import cv2

if __name__ == '__main__':

    image = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)

    binary = cv2.bitwise_not(binary)

    H = cv2.Sobel(binary, cv2.CV_8U, 0, 2)
    V = cv2.Sobel(binary, cv2.CV_8U, 2, 0)

    rows,cols = image.shape[:2]

    _,contours,_ = cv2.findContours(V, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        # rows/3 is the threshold for length of line
        if h > rows/3:
            cv2.drawContours(V, [cnt], -1, 255, -1)
            cv2.drawContours(binary, [cnt], -1, 255, -1)
        else:
            cv2.drawContours(V, [cnt], -1, 0, -1)

    _,contours,_ = cv2.findContours(H, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        (x,y,w,h) = cv2.boundingRect(cnt)
        # cols/3 is the threshold for length of line
        if w > cols/3:
            cv2.drawContours(H, [cnt], -1, 255, -1)
            cv2.drawContours(binary, [cnt], -1, 255, -1)
        else:
            cv2.drawContours(H, [cnt], -1, 0, -1)

    kernel = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(3,3))
    H = cv2.morphologyEx(H, cv2.MORPH_DILATE, kernel,iterations = 3)
    V = cv2.morphologyEx(V, cv2.MORPH_DILATE, kernel, iterations = 3)

    cross = cv2.bitwise_and(H, V)

    _,contours,_ = cv2.findContours(cross,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    centroids = []
    for cnt in contours:
        mom = cv2.moments(cnt)
        (x,y) = mom['m10']/mom['m00'], mom['m01']/mom['m00']
        cv2.circle(image,(int(x),int(y)),4,(0,255,0),-1)
        centroids.append((x,y))

    centroids.sort(key = lambda x: x[0], reverse = False)
    centroids.sort(key = lambda x: x[1], reverse = False)

    dx = int(centroids[1][0] - centroids[0][0])
    centroids = np.array(centroids, dtype = np.float32)
    (x,y,w,h) = cv2.boundingRect(centroids)

    if x-dx > -5: x = max(x-dx,0)
    if h+dx <= rows+5: h = min(h+dx,rows)
    if w+dx <= cols+5: w = min(w+dx,cols)
    cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0))

    roi = binary[y:y+h,x:x+w]

    roi = cv2.morphologyEx(roi, cv2.MORPH_OPEN, kernel,iterations = 1)

    cv2.imshow('image', image)
    cv2.imshow('roi', roi)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-morphological-operations-in-image-processing-opening-set-1
Python | Morphological Operations in Image Processing (Opening) | Set-1 - GeeksforGeeks
August 11, 2025 - import cv2 import numpy as np # Start capturing from webcam screenRead = cv2.VideoCapture(0) while True: # Capture a single frame from the webcam _, image = screenRead.read() # Convert to HSV color space hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Define blue color range blue1 = np.array([110, 50, 50]) blue2 = np.array([130, 255, 255]) # Create binary mask for blue color mask = cv2.inRange(hsv, blue1, blue2) # Define 5x5 structuring element (kernel) kernel = np.ones((5, 5), np.uint8) # Apply Opening to remove small white noise opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) # Show both original mask and cleaned version cv2.imshow('Original Blue Mask', mask) cv2.imshow('After Opening (Noise Removed)', opening) # Press 'a' key to stop if cv2.waitKey(1) & 0xFF == ord('a'): break # Clean up cv2.destroyAllWindows() screenRead.release()
🌐
GeeksforGeeks
geeksforgeeks.org › python-opencv-morphological-operations
Python OpenCV - Morphological Operations - GeeksforGeeks
January 3, 2023 - # import the necessary packages import cv2 # read the image img = cv2.imread("your image path", 0) # binarize the image binr = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # define the kernel kernel = np.ones((5, 5), np.uint8) # invert the image invert = cv2.bitwise_not(binr) # use morph gradient morph_gradient = cv2.morphologyEx(invert, cv2.MORPH_BLACKHAT, kernel) # print the output plt.imshow(morph_gradient, cmap='gray') Output: Black Hat image · Comment · More infoAdvertise with us · Next Article · Erosion and Dilation of images using OpenCV in python · J · jssuriyakumar · Follow · Improve · Article Tags : Python · OpenCV · Python-OpenCV · Practice Tags : python · Computer Vision Tutorial ·
🌐
TutorialsPoint
tutorialspoint.com › opencv › opencv_morphological_operations.htm
OpenCV - Morphological Operations
In addition to these two, OpenCV has more morphological transformations. The morphologyEx() of the method of the class Imgproc is used to perform these operations on a given image.
Find elsewhere
🌐
Master Data Science
datahacker.rs › 006-morphological-transformations-with-opencv-in-python
#006 Morphological transformations with OpenCV in Python
August 15, 2020 - # Performing opening and closing opening_1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_1) opening_2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_2) opening_3 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_3) closing_1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_1) closing_2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_2) closing_3 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_3)
🌐
Python Geeks
pythongeeks.org › python geeks › learn opencv › morphological operations in opencv
Morphological Operations in OpenCV - Python Geeks
February 14, 2023 - # Importing OpenCV import cv2 # Importing numpy import numpy as np # Reading the image img = cv2.imread(r"C:\Users\tushi\Downloads\PythonGeeks\dots.jpg", 0) # Displaying the image cv2.imshow('Original',img) cv2.waitKey(0) cv2.destroyAllWindows() # Defining the kernel kernel = np.ones((5,5),np.uint8) # Apply the opening morphological operation using cv2.morphologyEx() function img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # Displaying the image after the opening morphological operation cv2.imshow('Opening',img_opening) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
EDUCBA
educba.com › home › software development › software development tutorials › programming languages tutorial › opencv morphology
OpenCV Morphology | How to work Morphology function in OpenCV?
April 18, 2023 - We make use of the operation MORPH_OPEN in morphologyEx() function to perform opening morphological operations on a given image.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Amroamroamro
amroamroamro.github.io › mexopencv › opencv › morphology_demo_gui2.html
Advanced Morphology Transformations Demo
In this sample you will learn how to use the OpenCV function cv.morphologyEx to apply Morphological Transformation such as: Opening · Closing · Morphological Gradient · Top Hat · Black Hat · Sources: https://docs.opencv.org/3.2.0/d3/dbe/tutorial_opening_closing_hats.html · https://github.com/opencv/opencv/blob/3.2.0/samples/cpp/tutorial_code/ImgProc/Morphology_2.cpp · https://github.com/opencv/opencv/blob/3.2.0/samples/cpp/morphology2.cpp · https://github.com/opencv/opencv/blob/3.2.0/samples/python/morphology.py ·
🌐
TheAILearner
theailearner.com › tag › cv2-morphologyex
cv2.morphologyEx – TheAILearner
July 31, 2019 - Now, let’s discuss how to implement these using OpenCV-Python. For Opening, one way is to first apply erosion and then dilation using the builtin functions we discussed earlier. Similarly, for closing also.
🌐
Medium
mamuncseru.medium.com › a-brief-discussion-on-morphological-operators-using-opencv-ccf6be076896
A brief discussion on Morphological operators using OpenCV | by Md. Abdullah Al Mamun | Medium
January 20, 2023 - white_noise = (np.random.randint(low=0, high=2, size=binary_img.shape) * 255)\ .astype('uint8') binary_with_noise_img = binary_img + white_noise opening_img = cv2.morphologyEx(binary_with_noise_img, cv2.MORPH_OPEN,\ morph_ellipse) plt.imshow(opening_img)
🌐
Codebind
codebind.com › home › opencv python tutorial for beginners – morphological transformations
OpenCV Python Tutorial For Beginners - Morphological Transformations
May 8, 2019 - In this post on OpenCV Python Tutorial For Beginners, I am going to show How to use Morphological Transformations with OpenCV. We will learn different morphological operations like Erosion, Dilation, Opening, Closing etc. We will see different functions like : cv.erode(), cv.dilate(), cv.morphologyEx() etc.
🌐
OpenCV
docs.opencv.org › 4.x › d3 › dbe › tutorial_opening_closing_hats.html
OpenCV: More Morphology Transformations
void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
🌐
Regenerativetoday
regenerativetoday.com › morphological-operations-for-image-preprocessing-in-opencv-in-detail
Morphological Operations for Image Preprocessing in OpenCV, in Detail – Regenerative
grad1 = cv2.morphologyEx(gray.copy(), cv2.MORPH_GRADIENT, kernel) cv2.imwrite('grad1.jpg', grad1) grad2 = cv2.morphologyEx(gray.copy(), cv2.MORPH_GRADIENT, kernel1) cv2.imwrite(‘grad3.jpg’, grad2)
🌐
coseries
coseries.com › morphological-transformations-in-python-using-opencv
Morphological Transformations in Python using OpenCV | coseries
November 22, 2020 - import cv2 img = cv2.imread("morph2.png", 0) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9)) top_hat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel) cv2.imshow("Original image", img) cv2.imshow("Top Hat", top_hat) cv2.waitKey(0) cv2.destroyAllWindows() ... Categories: Image Processing Python Tags: image processing Image processing in python using openCV Image processing using OpenCV Image Smoothing Morphological Transformations openCV openCV in Python
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-morphological-operations-in-image-processing-gradient-set-3
Python | Morphological Operations in Image Processing (Gradient) | Set-3 - GeeksforGeeks
August 11, 2025 - import cv2 import numpy as np # Start capturing from webcam screenRead = cv2.VideoCapture(0) while True: # Capture a single frame from the webcam _, image = screenRead.read() # Convert to HSV color space hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # Define blue color range blue1 = np.array([110, 50, 50]) blue2 = np.array([130, 255, 255]) # Create binary mask for blue color mask = cv2.inRange(hsv, blue1, blue2) # Define 5x5 structuring element (kernel) kernel = np.ones((5, 5), np.uint8) # Apply Gradient to highlight object boundaries gradient = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernel) # Show both original mask and result after gradient cv2.imshow('Original Blue Mask', mask) cv2.imshow('After Gradient (Edges Highlighted)', gradient) # Press 'a' key to stop if cv2.waitKey(1) & 0xFF == ord('a'): break # Clean up cv2.destroyAllWindows() screenRead.release()