I want to perform an Dilation operation while having rounded corners. Something like this :
What I tried :
import numpy as np
import cv2
img = cv2.imread(test.jpg)
kernel = np.array([[0,1,0],
[1,1,1],
[0,1,0]], dtype=np.uint8)
img_d = cv2.dilate(img, kernel, iterations=45)
My Question is : Can we get rounded corners just by changing the kernel or should we add a further processing step to achieve this ?
NOTE : My goal is not to create a rounded square... I used this example just to explain the idea of getting rounded corners with a dilation operation.
CodePudding user response:
I figured out a way thanks to "Christoph Rackwitz" comment.
The idea is pretty simple.
We need to use a bigger kernel with a circle shape and reduce the number of Dilation iterations.
import numpy as np
import cv2
kernel = np.zeros((100,100), np.uint8)
cv2.circle(kernel, (50,50), 50, 255, -1)
plt.imshow(kernel, cmap="gray")
And then use this kernel with just one iteration :
img = cv2.imread(test.jpg)
img_d = cv2.dilate(img, kernel, iterations=1)
plt.imshow(kernel, cmap="gray")
CodePudding user response:
OpenCV also allows you choose a kernel of your shape and size with cv2.getStructuringElement
.