Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded corners dilation (Image Processing)

I want to perform an Dilation operation while having rounded corners. Something like this : Example


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.

enter image description here

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.

like image 261
SaulGoodMan Avatar asked Oct 29 '25 09:10

SaulGoodMan


1 Answers

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")

circle

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")

output

like image 166
SaulGoodMan Avatar answered Oct 31 '25 13:10

SaulGoodMan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!