创建一个面具,在OpenCV的蟒蛇删除内部轮廓

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

这是我从我的代码有结果:enter image description here

我利用等高发在我的脸上这款面膜,我在代码显示波纹管。 该项目的最终结果是要删除的脸,表明我不是定义它尚未背景。 我的问题是:有没有什么办法,使口罩与此相对,我就可以使用这样的cv2.imshow('My Image',cmb(foreground,background,mask))只显示在背景面具下的前景? (这里的问题是,我必须具备的屏蔽,这种形式的视频,但我希望它是实时) 或者是其他方式,可我莫名其妙地删除帧的像素(或下)我的柜台? 这是我的代码:

from imutils.video import VideoStream
from imutils import face_utils
import datetime
import argparse
import imutils
import time
import dlib
import cv2
import numpy as np

# path to facial landmark predictor
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True)

print("[INFO] loading facial landmark predictor...")
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])

# grab the indexes of the facial landmarks
(lebStart, lebEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eyebrow"]
(rebStart, rebEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eyebrow"]
(jawStart, jawEnd) = face_utils.FACIAL_LANDMARKS_IDXS["jaw"]

# initialize the video stream and allow the cammera sensor to warmup
print("[INFO] camera sensor warming up...")
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream, resize it to
    # have a maximum width of 400 pixels, and convert it to
    # grayscale
    frame = vs.read()
    frame = imutils.resize(frame, width=400)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale frame
    rects = detector(gray, 0)

    # loop over the face detections
    for rect in rects:
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)

        # extract the face coordinates, then use the
        faceline = shape[jawStart:lebEnd]

        # compute the convex hull for face, then
        # visualize each of the face
        facelineHull = cv2.convexHull(faceline)

        mask = np.zeros(frame.shape,dtype='uint8')
        cv2.drawContours(frame, [facelineHull], -1, (0, 0, 0),thickness=cv2.FILLED)
        cv2.drawContours(frame, [facelineHull], -1, (0, 255, 0))

    # show the frame
    cv2.imshow("Frame", frame)
    # cv2.imshow("Frame", mask)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break


# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
python opencv mask contour
2个回答
1
投票

假设你的面具是一个二进制掩码,你可以做到以下几点:

def cmb(foreground,background,mask):
    result = background.copy()
    result[mask] = foreground[mask]
    return result

我没有测试此代码,但我希望这个想法遇到。您反转为背景的面具,独自离开了面具的前景。应用此,向每个帧就万事大吉了,你有你的蒙面图像。

编辑:根据意见调整代码。当然,这种解决办法是比我原来写清楚得多。该功能保持不变,但。


0
投票

这是我从帧删除脸上的解决方案(这是快,但再次感谢@meetaig的帮助)

mask = np.zeros(frame.shape,dtype='uint8')
mask = cv2.drawContours(mask, [facelineHull], -1, (255 , 255 , 255),thickness=cv2.FILLED)
mask = cv2.bitwise_not(mask)
img2gray = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
result= cv2.bitwise_and(frame,frame,mask=mask)

如果我表现出的结果,现在它会奏效。

cv2.imshow("Frame", result)
© www.soinside.com 2019 - 2024. All rights reserved.