it shouldn’t be flipped for one operation and not the other. either it’s flipped, which is then a convolution, or it’s not, so it’s a correlation.
what you mean is that you want its center to be shifted. that’s a matter of taste and not necessarily required inherently by either operation.
some people seem to be doing this shifting business to fix the offset caused by even-sized kernels, where the center doesn’t fall on an element exactly. so for one of erosion/dilation, they go one half left, and for the other operation they go one half right, and that cancels out.
if you want to shift the center, just pass an anchor
.
I’d also recommend that you convert your arrays to numpy arrays before calling. makes the code neater.
import numpy as np
import cv2 as cv
el = np.uint8([[0,1,1]])
# this is lopsided, so there's no way around having to think about where the anchor is
im = np.zeros((3,5), dtype=np.uint8)
im[0, 1:2] = 1 # one set
im[1, 1:3] = 1 # two set
im[2, 1:4] = 1 # three set
r = cv.erode(im, el * 255, anchor=(1,0)) # anchor on the center (left 1)
s = cv.dilate(r, el * 255, anchor=(2,0)) # anchor on the right (right 1)
print(im)
print(r)
print(s) # half-element shifts cancel each other out
[[0 1 0 0 0]
[0 1 1 0 0]
[0 1 1 1 0]]
[[0 0 0 0 0]
[0 1 0 0 0]
[0 1 1 0 0]]
[[0 0 0 0 0]
[0 1 1 0 0]
[0 1 1 1 0]]