改进基于人脸检测的肤色过滤器(Python OpenCV)

2024-09-27 20:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python OpenCV实现一个自适应肤色过滤器,它使用haarcascades来检测一个直立的脸,然后过滤面部ROI以去除眉毛、眼镜等非皮肤特征,以获得平均肤色(以RGB为单位)。然后我将图像转换为HSV并提取接近我获得的平均值的HSV值。这是我的代码:

import cv2
import numpy as np
from functions import *
def nothing(x):
    pass
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
cv2.namedWindow('Video')
cv2.moveWindow('Video',5,5)
cv2.namedWindow('HSV_Thresh')
cv2.moveWindow('HSV_Thresh',655,5)
cv2.createTrackbar('tval', 'Video', 29, 255, nothing)
cv2.createTrackbar('htoler', 'HSV_Thresh', 17, 100, nothing)
cv2.createTrackbar('stoler', 'HSV_Thresh', 25, 100, nothing)
cv2.createTrackbar('vtoler', 'HSV_Thresh', 84, 100, nothing)

kernel = np.ones((5, 5), np.uint8)# 5X5 erosion kernel
bavg=0
ravg=0
gavg=0
while True: 
    tval1=cv2.getTrackbarPos('tval', 'Video')#thresh value to remove non skin components from face
    htoler_val=cv2.getTrackbarPos('htoler', 'HSV_Thresh')
    stoler_val=cv2.getTrackbarPos('stoler', 'HSV_Thresh')
    vtoler_val=cv2.getTrackbarPos('vtoler', 'HSV_Thresh')
    ret,img=cap.read()#Read from source
    img[0:100,0:100] = [255,255,255]
    thresh_hsv_toler=img    
    faces = face_cascade.detectMultiScale(img, 1.3, 5)
        for (x,y,w,h) in faces:
        bavg=0
        ravg=0
        gavg=0
        numpix=0
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        roi_face = img[y:y+h, x:x+w]
        #avg_col=img[100,100]
        rect_face=img[y:y+h-h/8,x+w/7:x+w-w/5]#extract only skin features from remaining bg
        mask=cv2.inRange(rect_face,(tval1,tval1,tval1),(255,255,255))
        mask=cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
        tone=cv2.subtract(mask,rect_face)
        tone=cv2.subtract(mask,tone)
        (rows,cols,col)=tone.shape # 480 rows and 640 cols; 3 values for RGB img
        for i in range(rows): #note the presence of colon
            for j in range(cols):
                if (tone[i,j,0]!=0 and tone[i,j,0]!=0 and tone[i,j,0]!=0):
                    bavg=bavg+tone[i,j,0]
                    gavg=gavg+tone[i,j,1]
                    ravg=ravg+tone[i,j,2]
                    numpix=numpix+1
        bavg=bavg/numpix
        gavg=gavg/numpix
        ravg=ravg/numpix
        '''print "bavg="+str(bavg)
        print "gavg="+str(gavg)
        print "ravg="+str(ravg)
        print "numpix="+str(numpix)'''
        cv2.circle(img, (50,50), 20, (bavg,gavg,ravg), 50)#get obtained average colour on screen


        cv2.imshow('skin_mask', tone)
        hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
        thresh_hsv_toler=cv2.inRange(hsv,(hsv[50,50,0]-htoler_val,hsv[50,50,1]-stoler_val,hsv[50,50,2]-vtoler_val),(hsv[50,50,0]+htoler_val,hsv[50,50,1]+stoler_val,hsv[50,50,2]+vtoler_val))

        thresh_hsv_toler=cv2.dilate(thresh_hsv_toler, kernel, iterations=1)
        thresh_hsv_toler=cv2.cvtColor(thresh_hsv_toler,cv2.COLOR_GRAY2BGR)#superimposing binary mask on image
        hsv_filter=cv2.subtract(thresh_hsv_toler,img)
        hsv_filter=cv2.subtract(thresh_hsv_toler,hsv_filter)



        cv2.imshow('HSV_Thresh', hsv_filter)


    if(cv2.waitKey(10) & 0xFF == ord('b')):
            break
    cv2.imshow('Video', img)

输出如下:

output

如何在不添加第二个过滤器的情况下减少结果中的噪声?谢谢


Tags: imgmaskvalcv2hsvfacetonethresh

热门问题