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 ·
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().
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 ·
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 ·
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...
Shimat
shimat.github.io › opencvsharp_docs › html › 994caec0-26f1-1fe3-86ea-329bad89c3af.htm
Cv2.GetStructuringElement Method (MorphShapes, Size, Point)
public: static Mat^ GetStructuringElement( MorphShapes shape, Size ksize, Point anchor )
OpenCV
docs.opencv.org › 4.x › dd › dd7 › tutorial_morph_lines_detection.html
OpenCV: Extract horizontal and vertical lines by using morphological operations
Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size,1));
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



