Why is the structuring element asymmetric in OpenCV?
Asked Answered
N

1

3

Why is the structuring element asymmetric in OpenCV?

cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(4,4))

returns

array([[0, 0, 1, 0],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=uint8)

Why isn't it

array([[0, 1, 1, 0],
       [1, 1, 1, 1],
       [1, 1, 1, 1],
       [0, 1, 1, 0]], dtype=uint8)

instead?

Odd-sized structuring elements are also asymmetric with respect to 90-degree rotations:

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)

What's the purpose of that?

Newtonnext answered 4/12, 2017 at 14:31 Comment(2)
you should use a structuring element of odd size: e,g,: ksize(5,5) if you want it to be symmetric around its centerExpiatory
@Expiatory thanks, updatedNewtonnext
U
3

There's no purpose for it other than it's one of many possible interpolations for such a shape. In the case of the ellipse with size 5, if it were full it would just be the same as the MORPH_RECT and if the same two were removed from the sides as from the top it would be a diamond. Either way, the way it's actually implemented in the source code is what you would expect---it creates a circle via the distance function and takes near integers to get the binary pixels. Search that file for cv::getStructuringElement and you'll find the implementation, it's nothing too fancy.

If you think an update to this function should be made, then open up a PR on GitHub with the implemented version, or an issue to discuss it first. I think a successful contribution would be easy here and I'd venture that the case for symmetry is strong. One would expect the result of a symmetric image being processed with an elliptical kernel wouldn't depend on orientation of the image.

Ullage answered 4/12, 2017 at 15:20 Comment(2)
thanks; still unclear where the actual asymmetry comes from in the two cases above. PIL assumes that (0,0) pixel's coords are (0.5, 0.5). Perhaps CV2 does this differently, leading to the surprising results? (AFAICT, this might explain the first asymmetry, but no the second)Newtonnext
OpenCV doesn't have a standard for interpolating sub-pixel measurements AFAIK. I believe it's up to each function (and thus it's author) on how sub-pixel measurements are quantized. But do have a look at the source code and try running it and print the outputs as it goes along.Ullage

© 2022 - 2024 — McMap. All rights reserved.