python - CVLIB - 如何在原始图像中添加模糊的子面?
问题描述
朋友们,我需要实现一个代码,从给定的图像中模糊面孔(我不是开发人员,所以这对我来说真的很难)。我发现我可以使用 OpenCV 和 cvlib 来做到这一点,并找到了一个示例代码(来自 cvlib 的存储库),它完成了部分工作。
我知道我需要获取子面并将面部模糊应用于它们,我可以做到,但现在我不知道如何将模糊的面部添加到原始图像中。有人可以帮我吗?
import cvlib as cv
import sys
from cv2 import cv2
import os
# read input image
image = cv2.imread('path')
# apply face detection
faces, confidences = cv.detect_face(image)
print(faces)
print(confidences)
# loop through detected faces
for face,conf in zip(faces,confidences):
(startX,startY) = face[0],face[1]
(endX,endY) = face[2],face[3]
subFace = image[startY:endY,startX:endX]
subFace = cv2.GaussianBlur(subFace,(23, 23), 30)
# display output
# press any key to close window
cv2.imshow("face_detection", image)
cv2.waitKey()
cv2.imshow("face_detection", subFace)
# release resources
cv2.destroyAllWindows()
解决方案
我终于想出了如何做到这一点:
import cvlib as cv
import sys
from cv2 import cv2
import os
# read input image
image = cv2.imread('path')
# apply face detection
faces, confidences = cv.detect_face(image)
# print the array with the coordinates and the confidence
print(faces)
print(confidences)
# loop through detected faces
for face,conf in zip(faces,confidences):
(startX,startY) = face[0],face[1]
(endX,endY) = face[2],face[3]
# get the subface
subFace = image[startY:endY,startX:endX]
# apply gaussian blur over subfaces
subFace = cv2.GaussianBlur(subFace,(23, 23), 30)
# add the subfaces to de original image
image[startY:startY+subFace.shape[0], startX:startX+subFace.shape[1]] = subFace
cv2.imshow("face_detection", image)
cv2.waitKey()
# save output
cv2.imwrite("face_detection.jpg", image)
# release resources
cv2.destroyAllWindows()
推荐阅读
- php - 如何从 laravel 的 eloquent 集合中获取外键
- android - 返回屏幕时如何在不禁用所有动画的情况下停止底部工作表进入动画
- css - 文本后面的文本下划线偏移
- python - 将 Python 输出打印到 Excel 工作表
- java - 如何从 JsonUnit 获取断言结果?
- excel - Rows.count 有什么问题?
- bash - 在 shell 脚本中,如何将 stdout 重定向到控制台,并将 stdout 和 stderr 重定向到文件?
- javascript - 向频道发布主动消息时如何在机器人中设置“您和版主可以回复”?
- javascript - 用画布反应问题 - 错误计算鼠标的位置
- tomcat - 如何解决检测到的 xampp tomcat 问题?