如何在带有圆形边框的opencv中模糊人脸?

2024-04-26 02:44:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Image

我使用了以下代码:

face = cv2.medianBlur(face, 100) 
img[top:bottom,left:right] = face

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

Output image


Tags: 代码rightimgtopcv2leftopencvface
3条回答
import cv2
import matplotlib.pyplot as plt
import numpy as np

img = cv2.imread('image.jpg')
h, w, c = img.shape

plt.imshow(img)
plt.show()

c_mask = np.zeros((h,w), np.uint8)
cv2.circle(c_mask,(w//2,h//2),100,1,thickness=-1)

mask = cv2.bitwise_and(img, img, mask=c_mask)

plt.imshow(mask)
plt.show()

img_mask = img - mask

plt.imshow(img_mask)
plt.show()

blur = cv2.blur(img,(17, 17))


plt.imshow(blur)
plt.show()

mask2 = cv2.bitwise_and(blur, blur, mask=c_mask) # mask

plt.imshow(mask2)
plt.show()

final_img = img_mask + mask2

print(np.max(final_img))

plt.imshow(final_img)
plt.show()

enter image description here

首先,创建一个遮罩图像。为此,在黑色图像上的面位置绘制一个白色圆圈

第二,模糊整个图像

第三,仅将模糊内容复制到遮罩所在的原始图像>;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)

输出:

enter image description here

您可以模糊整个图像,然后使用您喜欢的任何遮罩将结果复制到源

相关问题 更多 >