创建 Mask Inside ROI 选择

Create Mask Inside ROI selection

你好,我想把圈起来的眼睛变成白色。我知道我们不能删除眼睛所以我想遮住它,但我想不出办法。下面是我的代码。

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()

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

为了遮住眼睛,将cv2.circle方法中的thickness参数改为-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/

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