Skip to content

Add Support for Diamond-Shaped Structuring Element in Morphological Operations #27441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

KAVYANSHTYAGI
Copy link
Contributor

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

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

cv::Mat diamondKernel = cv::getStructuringElement(cv::MORPH_DIAMOND, cv::Size(5, 5));

Testing

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.

@vpisarev
Copy link
Contributor

@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.

Copy link
Contributor Author

@KAVYANSHTYAGI KAVYANSHTYAGI left a comment

Choose a reason for hiding this comment

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.

@asmorkalov asmorkalov added this to the 4.12.0 milestone Jun 27, 2025
@asmorkalov asmorkalov merged commit 5ee8919 into opencv:4.x Jun 27, 2025
52 of 55 checks passed
@s-trinh
Copy link
Contributor

s-trinh commented Jun 27, 2025

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy