在 OpenCV 中设置 ROI 之外的背景颜色

Set background color outside ROI in OpenCV

我成功显示了视频源,我正在尝试将 ROI 外区域的背景颜色从黑色更改为蓝色,但屏幕仍然显示黑色背景。请帮我解决问题。任何帮助将不胜感激。

原代码

import numpy as np
from cv2 import cv2
'''
ML object detection algo(haarcascade)used to identify objects. 
the XML file consists of trained Haar Cascade models.
'''
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#initialize video from the webcam
video = cv2.VideoCapture(1)

while True:
# ret tells if the camera works properly. Frame is an actual frame from the video feed
    ret, frame= video.read()
    # print(cv2.VideoCapture(0).isOpened())
    # make sure port is working and read the image
    if frame is not None and video.isOpened():
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Detect the faces within the subregions of the image in scales
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6)
        # Draw the rectangle around each face
        for (x, y, w, h) in faces:
            #Use the coordinates to find the center of the face and from that point draw a rectangle of radius w/2 or h/2.
            center_coordinates = x + w // 2, y + h // 2
            radius = w // 2 # or can be h / 2 or can be anything based on your requirements
            #background color(black) 
            mask=np.zeros(frame.shape[:2] , dtype="uint8") 
         
            # Draw the desired region to crop out in white 
            cv2.circle(mask, center_coordinates, radius, (255,255,255),-1)    
            masked=cv2.bitwise_and(frame,frame,mask=mask)
            cv2.imshow('mask applied',masked)
        if cv2.waitKey(30) & 0xff==27:
            break
video.release()
cv2.destroyAllWindows()

以上代码在黑色背景上检测并显示圆形蒙版中的人脸。但如前所述,圆形ROI外的背景颜色应该是蓝色。

我尝试用下面的代码替换 mask=np.zeros(frame.shape[:2], dtype="uint8") 但失败了。 Frame.shape[0:2]甚至不包括频道,我一开始不知道如何更改颜色。

mask=np.ones(frame.shape[0:2], dtype="uint8")
mask[:,:,0]=255
mask[:,:,1]=0
mask[:,:,2]=0

我也试过创建一个圆形蒙版图像,然后将它放在另一个图像上,结果发现它导致了同样的问题。

import numpy as np
from cv2 import cv2
'''
ML object detection algo(haarcascade)used to identify objects. 
the XML file consists of trained Haar Cascade models.
'''
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#initialize video from the webcam
video = cv2.VideoCapture(1)
print(cv2.VideoCapture(1).isOpened())

while True:
# ret tells if the camera works properly. Frame is an actual frame from the video feed
    ret, frame= video.read()
    # print(cv2.VideoCapture(0).isOpened())
    # make sure port is working and read the image
    if frame is not None and video.isOpened():
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Detect the faces within the subregions of the image in scales
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6)
        # Draw the rectangle around each face
        for (x, y, w, h) in faces:
            #Use the coordinates to find the center of the face and from that point draw a rectangle of radius w/2 or h/2.
            center_coordinates = x + w // 2, y + h // 2
            radius = w // 2 # or can be h / 2 or can be anything based on your requirements
            #background color(black) 
            mask=np.zeros(frame.shape[:2] , dtype="uint8") 
            # create blue colored background
            color = np.full_like(frame, (255,0,0))
           
            # Draw the desired region to crop out in white 
            roi=cv2.circle(mask, center_coordinates, radius, (255,255,255),-1)    
            masked=cv2.bitwise_and(frame,frame,mask=mask)
            mask_blue=cv2.bitwise_and(color,color,mask=mask-roi)
            # combine the two masked images
            result = cv2.add(masked,mask_blue)
            cv2.imshow('result',result)
        if cv2.waitKey(30) & 0xff==27:
            break
video.release()
cv2.destroyAllWindows()

我已经根据您的要求修改了您的代码如下。这里我多加了一行

masked[np.where((masked==[0,0,0]).all(axis=2))]=[255,0,0]

在这里您可以将黑色区域的像素值更改为任何特定颜色。

import numpy as np
import cv2
'''
ML object detection algo(haarcascade)used to identify objects. 
the XML file consists of trained Haar Cascade models.
'''
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +'haarcascade_frontalface_default.xml')
#initialize video from the webcam
video = cv2.VideoCapture(0)
print(cv2.VideoCapture(0).isOpened())

while True:
# ret tells if the camera works properly. Frame is an actual frame from the video feed
    ret, frame= video.read()
    # print(cv2.VideoCapture(0).isOpened())
    # make sure port is working and read the image
    if frame is not None and video.isOpened():
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # Detect the faces within the subregions of the image in scales
        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6)
        # Draw the rectangle around each face
        for (x, y, w, h) in faces:
            #Use the coordinates to find the center of the face and from that point draw a rectangle of radius w/2 or h/2.
            center_coordinates = x + w // 2, y + h // 2
            radius = w // 2 # or can be h / 2 or can be anything based on your requirements
            #background color(black) 
            mask=np.zeros(frame.shape[:2] , dtype="uint8") 
            
            # Draw the desired region to crop out in white 
            roi=cv2.circle(mask, center_coordinates, radius, (255,255,255),-1)    
            masked=cv2.bitwise_and(frame,frame,mask=mask)
            masked[np.where((masked==[0,0,0]).all(axis=2))]=[255,0,0]
            cv2.imshow('result',masked)
        if cv2.waitKey(30) & 0xff==27:
            break
video.release()
cv2.destroyAllWindows()