Reputation: 173
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)
Image used : test.jpg
I tried multiple kernels (with different sizes 3x3, 5x5...) but I didn't succeed to get rounded corners.
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.
Upvotes: 4
Views: 1430
Reputation: 21203
OpenCV also allows you choose a kernel of your shape and size with cv2.getStructuringElement
. From this page you can choose either a rectangle, ellipse or cross shaped kernels.
Since you needed rounded corners I chose the ellipse kernel cv2.MORPH_ELLIPSE
of size 25 x 25
:
img = cv2.imread('test.jpg', 0)
th_img = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
img_dil = cv2.dilate(th_img, kernel, iterations=5)
Not exactly the result you were hoping for but here is how it looks:
Upvotes: 2
Reputation: 173
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")
Upvotes: 5