在 ROI 选择内创建蒙版

问题描述 投票:0回答:1

嗨,我正在尝试将圈出的眼睛变成白色。我知道我们无法删除眼睛,所以我想掩盖它,但我想不出办法。下面是我的代码。

import cv2
import os
cascPathface = os.path.dirname(
cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
cascPatheyes = os.path.dirname(
cv2.__file__) + "/data/haarcascade_eye_tree_eyeglasses.xml"

faceCascade = cv2.CascadeClassifier(cascPathface)
eyeCascade = cv2.CascadeClassifier(cascPatheyes)

while True:
img = cv2.imread('man1.png')
newImg = cv2.resize(img, (600,600))
gray = cv2.cvtColor(newImg, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,
                                     scaleFactor=1.1,
                                     minNeighbors=5,
                                     minSize=(60, 60),
                                     flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in faces:
    cv2.rectangle(newImg, (x, y), (x + w, y + h),(0,255,0), 2)
    faceROI = newImg[y:y+h,x:x+w]
    eyes = eyeCascade.detectMultiScale(faceROI)

    
    for (x2, y2, w2, h2) in eyes:
        eye_center = (x + x2 + w2 // 2, y + y2 + h2 // 2)
        radius = int(round((w2 + h2) * 0.25))
        frame = cv2.circle(newImg, eye_center, radius, (255, 0, 0), 4)

    # Display the resulting frame
cv2.imshow('Image', newImg)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

last = cv2.imwrite('faces_detected.png', faceROI)
cv2.destroyAllWindows()

这是我希望眼睛是白色的图像:

python detection face-detection roi
1个回答
1
投票

为了遮盖眼睛,将cv2.circle方法中的厚度参数更改为-1。这将以指定的颜色填充圆圈。

将代码从

frame = cv2.circle(newImg, eye_center, radius, (255, 0, 0), 4)
更改为
frame = cv2.circle(newImg, eye_center, radius, (255, 0, 0), -1)

参考:https://www.geeksforgeeks.org/python-opencv-cv2-circle-method/

如果您觉得该解决方案有帮助,请点赞。

© www.soinside.com 2019 - 2024. All rights reserved.