🌐
Shimat
shimat.github.io › opencvsharp_docs › html › a83a9f62-e3dc-c3e9-36bd-1758462d2198.htm
Cv2.GetStructuringElement Method (MorphShapes, Size)
static member GetStructuringElement : shape : MorphShapes * ksize : Size -> Mat · shape · Type: OpenCvSharpMorphShapes Element shape that could be one of MorphShapes · ksize · Type: OpenCvSharpSize Size of the structuring element. Type: Mat · [Missing <returns> documentation for "M:OpenCvSharp.Cv2.GetStructuringElement(OpenCvSharp.MorphShapes,OpenCvSharp.Size)"] See 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 - morph_rect = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(5, 5)) morph_ellipse = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(5, 5)) morph_cross = cv2.getStructuringElement(shape=cv2.MORPH_CROSS, ksize=(5, 5))
🌐
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io › en › latest › py_tutorials › py_imgproc › py_morphological_ops › py_morphological_ops.html
Morphological Transformations — OpenCV-Python Tutorials beta documentation
We manually created a structuring elements in the previous examples with help of Numpy. It is rectangular shape. But in some cases, you may need elliptical/circular shaped kernels. So for this purpose, OpenCV has a function, cv2.getStructuringElement().
🌐
Python Geeks
pythongeeks.org › python geeks › learn opencv › morphological operations in opencv
Morphological Operations in OpenCV - Python Geeks
February 14, 2023 - To get a cross, rectangle, or ellipse-shaped kernel to perform morphological operations over an image, MORPH_CROSS, MORPH_RECT, and MORPH_ELLIPSE parameters are used in OpenCV. The structuring element to perform these morphological operations ...
🌐
ProgramCreek
programcreek.com › python › example › 89381 › cv2.getStructuringElement
Python Examples of cv2.getStructuringElement
Parameters ---------- mask: :class:`numpy.ndarray` The mask to be eroded or dilated Returns ------- :class:`numpy.ndarray` The erosion kernel to be used for erosion/dilation """ erosion_ratio = self.config["erosion"] / 100 mask_radius = np.sqrt(np.sum(mask)) / 2 kernel_size = max(1, int(abs(erosion_ratio * mask_radius))) erosion_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)) logger.trace("erosion_kernel shape: %s", erosion_kernel.shape) return erosion_kernel
🌐
OpenCV
docs.opencv.org › 3.4 › d4 › d76 › tutorial_js_morphological_ops.html
OpenCV: Morphological Transformations
We use the function: cv.getStructuringElement (shape, ksize, anchor = new cv.Point(-1, -1)) Parameters ·
🌐
coseries
coseries.com › morphological-transformations-in-python-using-opencv
Morphological Transformations in Python using OpenCV | coseries
November 22, 2020 - import cv2 img = cv2.imread("morph.png", 0) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9)) print(kernel) eroded_img = cv2.erode(img, kernel) cv2.imshow("Original image", img) cv2.imshow("Eroded image", eroded_img) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
PyImageSearch
pyimagesearch.com › home › blog › opencv morphological operations
OpenCV Morphological Operations - PyImageSearch
May 9, 2021 - The cv2.getStructuringElement function requires two arguments: the first is the type of structuring element we want, and the second is the size of the structuring element (which we grab from the for loop on Line 40).
🌐
Shimat
shimat.github.io › opencvsharp_docs › html › 1ebf73c1-4823-c23d-4171-05f5930b491b.htm
Cv2.GetStructuringElement Method
Cv2 Methods · GetStructuringElement Method · GetStructuringElement Method (MorphShapes, Size) GetStructuringElement Method (MorphShapes, Size, Point) Overload List · Top · See Also ·
Find elsewhere
🌐
OpenCV
docs.opencv.org › 3.4.20 › d3 › dbe › tutorial_opening_closing_hats.html
OpenCV: More Morphology Transformations
element = cv.getStructuringElement(morph_elem, (2*morph_size + 1, 2*morph_size+1), (morph_size, morph_size))
Top answer
1 of 2
1

This answer explains how to use MORPH_CLOSE around the edge of an image by adding a buffer to the image.

You can add a buffer by creating an image of zeros using numpy:

# Import packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read in the image
img = cv2.imread('/home/stephen/Desktop/OKoUh.png', 0)
# Create a black bufffer around the image
h,w = img.shape
buffer = max(h,w)
bg = np.zeros((h+buffer*2, w+buffer*2), np.uint8)
bg[buffer:buffer+h, buffer:buffer+w] = img

Then you can iterate and check how it looks at different kernel sizes:

for close_size in range(1,11):
    temp  = bg.copy()
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (close_size, close_size))
    result = cv2.morphologyEx(temp, cv2.MORPH_CLOSE, kernel)
    results = result[buffer:buffer+h, buffer:buffer+w]
    cv2.imshow('img', result)
    cv2.waitKey()

My results:

2 of 2
1

Based on Stephen's answer, here is the snippet I ended up implementing :

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (close_size, close_size))
# Padding
im=cv2.copyMakeBorder(im,close_size,close_size,close_size,close_size,
                      borderType=cv2.BORDER_CONSTANT, value = 0)
# Closing
im = cv2.morphologyEx(im, cv2.MORPH_CLOSE, kernel)
# Unpadding
im = im[close_size:-close_size,close_size:-close_size]

As I mentionned in a comment, this can lead to a much longer computation time for large kernel sizes. It's possible that padding with lower values, eg close_size/2 would be enough to prevent border issues (didn't test it).

🌐
Blogger
opencvexamples.blogspot.com › 2013 › 10 › create-structuring-element-for.html
Learn OpenCV by Examples: Create structuring element for morphological operations
getStructuringElement(int shape, Size ksize, Point anchor=Point(-1,-1)) Parameters: shape – Element shape that could be one of the following: MORPH_RECT (0) - a rectangular structuring element: MORPH_ELLIPSE (2) - an elliptic structuring element, that is, a filled ellipse inscribed into the rectangle Rect(0, 0, esize.width, esize.height) MORPH_CROSS (1)- a cross-shaped structuring element: CV_SHAPE_CUSTOM (100) - custom structuring element (OpenCV 1.x API) ksize – Size of the structuring element...
🌐
Adonline
code.adonline.id.au › structuring-elements-in-python
Structuring elements in Python | Adam Dimech's Coding Blog
OpenCV has a getStructuringElement() function that has three shapes: Rectangle (specified with cv2.MORPH_RECT) Ellipse (specified with cv2.MORPH_ELLIPSE) Cross (specified with cv2.MORPH_CROSS) For instance, to create an ellipse measuring 5×5, the following code will work: import cv2 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) This produces the following structuring element: array([[0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0]], dtype=uint8) To create a cross measuring 13×13, the following code will work: kernel = cv2.getStructuringElement
🌐
Hiwonder
docs.hiwonder.com › projects › MentorPi › en › 2024-versions › docs › 9.ros+opencv_lesson.html
8. ROS+OpenCV Lesson — MentorPi 2024 Version v1.0 documentation
... Perform erosion and dilation ... ... cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)): generates a 3x3 rectangular structural element (kernel) that specifies the shape and size of the erosion operation....
🌐
GitHub
github.com › HarendraKumarSingh › form-extractor-ocr › blob › master › main › preprocessing.py
form-extractor-ocr/main/preprocessing.py at master · HarendraKumarSingh/form-extractor-ocr
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize)) vertical = cv2.erode(vertical, verticalStructure, (-1, -1)) vertical = cv2.dilate(vertical, verticalStructure, (-1, -1)) cv2.imwrite("./../data/temp/vertical.jpg", vertical) ·
Author   HarendraKumarSingh