我尝试使用以下脚本仅模糊脸部,但最终模糊了整个图像。关于如何更改脚本以仅模糊脸部的任何建议?
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('beatles.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detecting faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# To show the detected faces
cv2.imshow('img',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
# Blurring of just the faces in a picture
skaldetlykkes = cv2.filter2D(img, -1, k)
cv2.imshow("Ladetskje", skaldetlykkes)
cv2.waitKey (5000)
cv2.destroyAllWindows()
以下代码示例将仅在面部应用框过滤器:
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Blur the faces, i.e. only the ROIs defined by faces
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
for (x, y, w, h) in faces:
img[y:y+h,x:x+w] = cv2.filter2D(img[y:y+h,x:x+w], -1, k)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()
我是一名优秀的程序员,十分优秀!