You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR introduces support for a new diamond-shaped structuring element in OpenCV’s morphology module. The addition enables users to perform morphological operations (e.g., erosion, dilation, opening, closing) using a diamond-shaped kernel, which is defined by Manhattan distance from the center.
Detailed Changes
New Enum Values:
Added MORPH_DIAMOND to MorphShapes in modules/imgproc/include/opencv2/imgproc.hpp and CV_SHAPE_DIAMOND to MorphShapes_c in modules/imgproc/include/opencv2/imgproc/types_c.h.
Kernel Generation Logic:
Updated getStructuringElement in modules/imgproc/src/morph.dispatch.cpp to:
Accept MORPH_DIAMOND as a valid shape.
Generate a diamond-shaped mask, where each pixel is included if its Manhattan distance from the center is within the kernel radius.
Unit Test:
Added a dedicated test (modules/imgproc/test/test_structuring_element.cpp) that:
Verifies the correctness of the diamond kernel for a standard size (5x5).
Ensures consistency and protects against regression.
Motivation
Diamond-shaped structuring elements are widely used in image processing, particularly where neighborhood connectivity is defined by Manhattan distance (e.g., certain denoising, thinning, or skeletonization tasks). By supporting this shape natively, OpenCV expands its flexibility and applicability to new classes of problems.
Backward Compatibility
This change is fully backward compatible and does not alter any existing functionality or APIs. It only adds a new option for kernel shape selection.
Usage Example
The PR includes comprehensive unit tests for the new diamond shape.
All existing tests pass, and the new test verifies the mask generation logic.
Checklist
Added new enum values for the diamond shape.
Implemented diamond kernel logic in morphology dispatch.
Added and validated unit tests.
Closing
This PR fills a gap in OpenCV’s morphological toolkit by providing a commonly-used structuring element. Please let me know if further changes or documentation are needed.
@KAVYANSHTYAGI, thank you for the contribution! Can you please provide some references to papers, articles where diamond-shaped structuring elements are used? It's the first time I hear about it. In OpenCV you can more or less easily specify structuring element of any form, so may be we don't need extra feature which is just for convenience. We'd like to keep the library compact unless there is some clear advantage of extending it.
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback! I appreciate the emphasis on keeping OpenCV concise and adding features with broad utility.
References
"Morphological Image Analysis: Principles and Applications"
Pierre Soille — Section 3.1.3 covers the diamond (L1-metric) structuring element and its uses. Springer Link
"Fundamentals of Digital Image Processing"
Anil K. Jain — Discusses diamond shapes as a basic structuring element.
Lam, Lee, and Suen, “Thinning Methodologies—A Comprehensive Survey,” IEEE TPAMI, 1992.
Diamond SE is used in skeletonization/thinning algorithms. IEEE Link
“Efficient Implementation of Morphological Operations with Arbitrary Structuring Elements,” IEEE, 1994.
Efficient algorithms for diamond SE.
(https://ieeexplore.ieee.org/document/296353)
scikit-image library --- Offers diamond SE as a built-in primitive.
Justification
The diamond structuring element is a standard primitive in mathematical morphology, especially for L1/Manhattan distance operations. It’s widely documented in textbooks and research, and is essential for connectivity based tasks like skeletonization and thinning.
While OpenCV supports custom kernels, having diamond as a built in, well tested option ensures consistency, reduces user error, and matches what’s already standard in libraries like scikit image. This makes foundational morphology algorithms easier to use, reliable, and reproducible across different users and codebases.
Lam, Lee, and Suen, “Thinning Methodologies—A Comprehensive Survey,” IEEE TPAMI, 1992. Diamond SE is used in skeletonization/thinning algorithms. IEEE Link
Not the correct IEEE link...
“Efficient Implementation of Morphological Operations with Arbitrary Structuring Elements,” IEEE, 1994. Efficient algorithms for diamond SE. (https://ieeexplore.ieee.org/document/296353)
Quick Google Scholar returns nothing...
scikit-image library --- Offers diamond SE as a built-in primitive.
This would deserve a direct link instead of reading a full paper, false url or non-existing paper...
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Description
Overview
This PR introduces support for a new diamond-shaped structuring element in OpenCV’s morphology module. The addition enables users to perform morphological operations (e.g., erosion, dilation, opening, closing) using a diamond-shaped kernel, which is defined by Manhattan distance from the center.
Detailed Changes
Motivation
Diamond-shaped structuring elements are widely used in image processing, particularly where neighborhood connectivity is defined by Manhattan distance (e.g., certain denoising, thinning, or skeletonization tasks). By supporting this shape natively, OpenCV expands its flexibility and applicability to new classes of problems.
Backward Compatibility
This change is fully backward compatible and does not alter any existing functionality or APIs. It only adds a new option for kernel shape selection.
Usage Example
cv::Mat diamondKernel = cv::getStructuringElement(cv::MORPH_DIAMOND, cv::Size(5, 5));
Testing
Checklist
Added new enum values for the diamond shape.
Implemented diamond kernel logic in morphology dispatch.
Closing
This PR fills a gap in OpenCV’s morphological toolkit by providing a commonly-used structuring element. Please let me know if further changes or documentation are needed.