python opencv肤色检测的实现示例

时间:2021-10-29 12:57:49

1 椭圆肤色检测模型

原理:将RGB图像转换到YCRCB空间,肤色像素点会聚集到一个椭圆区域。先定义一个椭圆模型,然后将每个RGB像素点转换到YCRCB空间比对是否再椭圆区域,是的话判断为皮肤。

YCRCB颜色空间

python opencv肤色检测的实现示例python opencv肤色检测的实现示例

椭圆模型

python opencv肤色检测的实现示例

代码

  1. def ellipse_detect(image):
  2. """
  3. :param image: 图片路径
  4. :return: None
  5. """
  6. img = cv2.imread(image,cv2.IMREAD_COLOR)
  7. skinCrCbHist = np.zeros((256,256), dtype= np.uint8 )
  8. cv2.ellipse(skinCrCbHist ,(113,155),(23,15),43,0, 360, (255,255,255),-1)
  9.  
  10. YCRCB = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
  11. (y,cr,cb)= cv2.split(YCRCB)
  12. skin = np.zeros(cr.shape, dtype=np.uint8)
  13. (x,y)= cr.shape
  14. for i in range(0,x):
  15. for j in range(0,y):
  16. CR= YCRCB[i,j,1]
  17. CB= YCRCB[i,j,2]
  18. if skinCrCbHist [CR,CB]>0:
  19. skin[i,j]= 255
  20. cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  21. cv2.imshow(image, img)
  22. dst = cv2.bitwise_and(img,img,mask= skin)
  23. cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  24. cv2.imshow("cutout",dst)
  25. cv2.waitKey()

效果

python opencv肤色检测的实现示例

2 YCrCb颜色空间的Cr分量+Otsu法阈值分割算法

原理

针对YCRCB中CR分量的处理,将RGB转换为YCRCB,对CR通道单独进行otsu处理,otsu方法opencv里用threshold

代码

  1. def cr_otsu(image):
  2. """YCrCb颜色空间的Cr分量+Otsu阈值分割
  3. :param image: 图片路径
  4. :return: None
  5. """
  6. img = cv2.imread(image, cv2.IMREAD_COLOR)
  7. ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
  8.  
  9. (y, cr, cb) = cv2.split(ycrcb)
  10. cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
  11. _, skin = cv2.threshold(cr1,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  12.  
  13. cv2.namedWindow("image raw", cv2.WINDOW_NORMAL)
  14. cv2.imshow("image raw", img)
  15. cv2.namedWindow("image CR", cv2.WINDOW_NORMAL)
  16. cv2.imshow("image CR", cr1)
  17. cv2.namedWindow("Skin Cr+OTSU", cv2.WINDOW_NORMAL)
  18. cv2.imshow("Skin Cr+OTSU", skin)
  19.  
  20. dst = cv2.bitwise_and(img, img, mask=skin)
  21. cv2.namedWindow("seperate", cv2.WINDOW_NORMAL)
  22. cv2.imshow("seperate", dst)
  23. cv2.waitKey()

效果

python opencv肤色检测的实现示例

3 基于YCrCb颜色空间Cr, Cb范围筛选法

原理

类似于第二种方法,只不过是对CR和CB两个通道综合考虑

代码

  1. def crcb_range_sceening(image):
  2. """
  3. :param image: 图片路径
  4. :return: None
  5. """
  6. img = cv2.imread(image,cv2.IMREAD_COLOR)
  7. ycrcb=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
  8. (y,cr,cb)= cv2.split(ycrcb)
  9.  
  10. skin = np.zeros(cr.shape,dtype= np.uint8)
  11. (x,y)= cr.shape
  12. for i in range(0,x):
  13. for j in range(0,y):
  14. if (cr[i][j]>140)and(cr[i][j])<175 and (cr[i][j]>100) and (cb[i][j])<120:
  15. skin[i][j]= 255
  16. else:
  17. skin[i][j] = 0
  18. cv2.namedWindow(image,cv2.WINDOW_NORMAL)
  19. cv2.imshow(image,img)
  20. cv2.namedWindow(image+"skin2 cr+cb",cv2.WINDOW_NORMAL)
  21. cv2.imshow(image+"skin2 cr+cb",skin)
  22.  
  23. dst = cv2.bitwise_and(img,img,mask=skin)
  24. cv2.namedWindow("cutout",cv2.WINDOW_NORMAL)
  25. cv2.imshow("cutout",dst)
  26.  
  27. cv2.waitKey()

效果

python opencv肤色检测的实现示例

4 HSV颜色空间H,S,V范围筛选法

原理

还是转换空间然后每个通道设置一个阈值综合考虑,进行二值化操作。

代码

  1. def hsv_detect(image):
  2. """
  3. :param image: 图片路径
  4. :return: None
  5. """
  6. img = cv2.imread(image,cv2.IMREAD_COLOR)
  7. hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
  8. (_h,_s,_v)= cv2.split(hsv)
  9. skin= np.zeros(_h.shape,dtype=np.uint8)
  10. (x,y)= _h.shape
  11.  
  12. for i in range(0,x):
  13. for j in range(0,y):
  14. if(_h[i][j]>7) and (_h[i][j]<20) and (_s[i][j]>28) and (_s[i][j]<255) and (_v[i][j]>50 ) and (_v[i][j]<255):
  15. skin[i][j] = 255
  16. else:
  17. skin[i][j] = 0
  18. cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  19. cv2.imshow(image, img)
  20. cv2.namedWindow(image + "hsv", cv2.WINDOW_NORMAL)
  21. cv2.imshow(image + "hsv", skin)
  22. dst = cv2.bitwise_and(img, img, mask=skin)
  23. cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  24. cv2.imshow("cutout", dst)
  25. cv2.waitKey()

效果

python opencv肤色检测的实现示例

示例

  1. import cv2
  2. import numpy as np
  3.  
  4. def ellipse_detect(image):
  5. """
  6. :param image: img path
  7. :return: None
  8. """
  9. img = cv2.imread(image, cv2.IMREAD_COLOR)
  10. skinCrCbHist = np.zeros((256, 256), dtype=np.uint8)
  11. cv2.ellipse(skinCrCbHist, (113, 155), (23, 15), 43, 0, 360, (255, 255, 255), -1)
  12.  
  13. YCRCB = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
  14. (y, cr, cb) = cv2.split(YCRCB)
  15. skin = np.zeros(cr.shape, dtype=np.uint8)
  16. (x, y) = cr.shape
  17. for i in range(0, x):
  18. for j in range(0, y):
  19. CR = YCRCB[i, j, 1]
  20. CB = YCRCB[i, j, 2]
  21. if skinCrCbHist[CR, CB] > 0:
  22. skin[i, j] = 255
  23. cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  24. cv2.imshow(image, img)
  25. dst = cv2.bitwise_and(img, img, mask=skin)
  26. cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  27. cv2.imshow("cutout", dst)
  28. cv2.waitKey()
  29.  
  30. if __name__ == '__main__':
  31. ellipse_detect('./test.png')

到此这篇关于python opencv肤色检测的实现示例的文章就介绍到这了,更多相关opencv 肤色检测内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_40893939/article/details/84527037