如何在opencv中用圆形边框模糊脸部-Python?

Rah*_*hul 1 python opencv python-3.x

我在 OpenCV 中模糊了脸部,如下所示:

图像

我使用了这段代码:

face = cv2.medianBlur(face, 100) 
img[top:bottom,left:right] = face
Run Code Online (Sandbox Code Playgroud)

但我想让脸边框像这样变圆(不需要完美)

输出图像

Bar*_*raa 5

首先,创建一个蒙版图像。为此,请在黑色图像上的脸部位置绘制一个白色圆圈。

其次,模糊整个图像。

第三,仅在蒙版 > 0 的情况下将模糊内容复制到原始图像。

p1 = (65, 65)
w, h = 100, 100
p2 = (p1[0] + w, p1[1] + h)


circle_center = ((p1[0] + p2[0])// 2, (p1[1] + p2[1]) // 2)
circle_radius = int(math.sqrt(w * w + h * h) // 2)
mask_img = np.zeros(img.shape, dtype='uint8')
cv2.circle(mask_img, circle_center, circle_radius, (255, 255, 255), -1)

img_all_blurred = cv2.medianBlur(img, 99)
img_face_blurred = np.where(mask_img > 0, img_all_blurred, img)
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述