繁体   English   中英

创建一个遮罩并在opencv python中删除内部轮廓

[英]create a mask and delete inside contour in opencv python

这是我从代码中得到的结果: 在此处输入图像描述

正如我在代码中展示的波纹一样,我已经用轮廓制作了这个面具。
该项目的最终结果是删除面孔并显示背景(我尚未定义)
我的问题是:有没有办法用此计数器制作遮罩,所以我可以使用类似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()

假设您的掩码是二进制掩码,则可以执行以下操作:

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

我没有测试此代码,但我希望这个想法能够解决。 您将背景遮罩反转,而将前景遮罩保留下来。 您将其应用到每个帧和蒙版,就得到了蒙版图像。

编辑:根据评论调整代码。 当然,该解决方案比我最初写的要清晰得多。 但是,功能保持不变。

这是我从帧中删除面部的解决方案(速度更快,但再次感谢@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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM