Repository: opencv
Issue: Morphological openings are not anti extensive (because dilation or opening is not well implemented)
Labels: bug,category: imgproc

### System Information

OpenCV version: 4.7.0.72
Operating System / Platform: OSX Ventura 13.2.1
Python version: 3.9.6 

### Detailed description

One fundamental property of morphological opening is anti-extensivity, i.e. that, when opening a shape A by a structuring element B, then the result R should **always** be included in A ([Wikipedia](https://en.wikipedia.org/wiki/Opening_(morphology))).
In OpenCV, the result of morphological opening is not always included in the original shape. I think this is a big issue because it does not happen in other softwares like Matlab with image processing toolbox.

I think the issue lies in the implementation of the dilation in OpenCV. The definition of morphological dilation is quite different from the morphological erosion: it should perform a maximum instead of a minimum, but also flip the structuring element given as input (https://en.wikipedia.org/wiki/Dilation_(morphology)). **This flipping is mandatory by definition** of the dilation, and has nothing to do with the anchor point problem in even sized structuring element. 

If the flipping is not performed, then:

- the opening cannot be anti extensive, although it should be , 
- the closing cannot be extensive, although it should be ([(Wikipedia](https://en.wikipedia.org/wiki/Closing_(morphology)) 
- dilation and erosion are not more dual operations ([Lecture, property 5.10](https://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MORSE/morph1.pdf))

I performed many tests on dilation in OpenCV with non symmetrical structuring elements, and could not find the same results between OpenCV and other softwares like Matlab or Octave (which produce expected results). I came to the conclusion that OpenCV failed to perform the flip because, when I do it manually, I find the same results between OpenCV and Matlab or Octave. However, the flip should be performed by the function itself and not be done by the user manually.
Consequently, when using non symmetrical structuring elements, OpenCV morphological opening is not anti extensive, the closing is not extensive, and dilations and erosions are not dual. However, these properties should be respected whatever structuring element we use.

In order to avoid overflowing this topic with too many code, I give here an example showing that the result of the opening is not anti extensive.

I should add that this issue only happens when using non symmetrical structuring elements (not equal to their flip). When using squares, crosses, lines, or disks, then the problem does not arise because flipping the structuring element gives the same result as not flipping it. However, when using non symmetrical structuring element (which is very rare in morphology), the issue happens.

### Steps to reproduce

``` python
import numpy
import cv2

A = numpy.uint8([[0,0,0,0,0],[0,1,1,0,0],[0,1,1,1,0],[0,1,1,1,0],[0,0,0,0,0]])
B = numpy.uint8([[1,1,0],[0,1,1],[1,0,0]])

R = cv2.morphologyEx(A, cv2.MORPH_OPEN, B)

print(A)
print("***")
print(R)
```
The output is
```
[[0 0 0 0 0]
 [0 1 1 0 0]
 [0 1 1 1 0]
 [0 1 1 1 0]
 [0 0 0 0 0]]
***
[[0 0 0 0 0]
 [0 0 0 1 0]
 [0 1 1 0 0]
 [0 0 1 1 0]
 [0 0 0 0 0]]
```
Clearly, in the output, a pixel which was initially at the value 0 in A became equal to 1 in R (second row from top, fourth column from left). We don't have anti extensivity as R is not included in A.


### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)