OpenCV修改ROI区域外背景为蓝色时仍显示黑色的问题求解
问题原因
- 最初尝试给单通道的mask赋值三个通道的颜色值,维度不匹配所以运行报错。
- 第二次尝试中
mask-roi的逻辑错误,在mask上绘制白色圆形后,mask本身已经是「人脸区域为255、其余为0」的单通道掩码,不需要减roi,直接取反就能得到背景区域的掩码。
正确修改代码
import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 注意如果摄像头索引1打不开可以换成0 video = cv2.VideoCapture(1) while True: ret, frame = video.read() if not ret or not video.isOpened(): break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6) # 提前生成蓝色背景图,和原图像尺寸、通道数完全一致 blue_bg = np.full_like(frame, (255, 0, 0)) # 初始化单通道掩码,全黑 mask = np.zeros(frame.shape[:2], dtype="uint8") # 把所有检测到的人脸区域都在mask上画成白色 for (x, y, w, h) in faces: center_coordinates = x + w // 2, y + h // 2 radius = w // 2 cv2.circle(mask, center_coordinates, radius, (255,255,255), -1) # 分别提取人脸区域和背景区域 face_area = cv2.bitwise_and(frame, frame, mask=mask) # 掩码取反,得到背景区域的掩码 bg_area = cv2.bitwise_and(blue_bg, blue_bg, mask=cv2.bitwise_not(mask)) # 叠加两个区域得到最终结果 result = cv2.add(face_area, bg_area) cv2.imshow('result', result) if cv2.waitKey(30) & 0xff == 27: break video.release() cv2.destroyAllWindows()
关键修改说明
- 将蓝色背景生成、掩码初始化移到人脸遍历循环外,避免重复生成相同尺寸的数组浪费性能,同时可以支持多个人脸同时检测的场景。
- 使用
cv2.bitwise_not(mask)直接对掩码取反,得到圆形区域外的背景掩码,不需要额外做减法操作。 - 统一遍历所有人脸绘制掩码后再做图像叠加,避免多个人脸时只显示最后一张人脸的问题。
内容的提问来源于stack exchange,提问作者Rebecca L
相关产品推荐
相关产品推荐

