python opencv膚色檢測的實現示例

1 橢圓膚色檢測模型

原理:將RGB圖像轉換到YCRCB空間,膚色像素點會聚集到一個橢圓區域。先定義一個橢圓模型,然後將每個RGB像素點轉換到YCRCB空間比對是否再橢圓區域,是的話判斷為皮膚。

YCRCB顏色空間

橢圓模型

代碼

def ellipse_detect(image):
  """
  :param image: 圖片路徑
  :return: None
  """
  img = cv2.imread(image,cv2.IMREAD_COLOR)
  skinCrCbHist = np.zeros((256,256), dtype= np.uint8 )
  cv2.ellipse(skinCrCbHist ,(113,155),(23,15),43,0, 360, (255,255,255),-1)
 
  YCRCB = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
  (y,cr,cb)= cv2.split(YCRCB)
  skin = np.zeros(cr.shape, dtype=np.uint8)
  (x,y)= cr.shape
  for i in range(0,x):
    for j in range(0,y):
      CR= YCRCB[i,j,1]
      CB= YCRCB[i,j,2]
      if skinCrCbHist [CR,CB]>0:
        skin[i,j]= 255
  cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  cv2.imshow(image, img)
  dst = cv2.bitwise_and(img,img,mask= skin)
  cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  cv2.imshow("cutout",dst)
  cv2.waitKey()

效果

2 YCrCb顏色空間的Cr分量+Otsu法閾值分割算法

原理

針對YCRCB中CR分量的處理,將RGB轉換為YCRCB,對CR通道單獨進行otsu處理,otsu方法opencv裡用threshold

代碼

def cr_otsu(image):
  """YCrCb顏色空間的Cr分量+Otsu閾值分割
  :param image: 圖片路徑
  :return: None
  """
  img = cv2.imread(image, cv2.IMREAD_COLOR)
  ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
 
  (y, cr, cb) = cv2.split(ycrcb)
  cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
  _, skin = cv2.threshold(cr1,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
 
  cv2.namedWindow("image raw", cv2.WINDOW_NORMAL)
  cv2.imshow("image raw", img)
  cv2.namedWindow("image CR", cv2.WINDOW_NORMAL)
  cv2.imshow("image CR", cr1)
  cv2.namedWindow("Skin Cr+OTSU", cv2.WINDOW_NORMAL)
  cv2.imshow("Skin Cr+OTSU", skin)
 
  dst = cv2.bitwise_and(img, img, mask=skin)
  cv2.namedWindow("seperate", cv2.WINDOW_NORMAL)
  cv2.imshow("seperate", dst)
  cv2.waitKey()

效果

3 基於YCrCb顏色空間Cr, Cb范圍篩選法

 原理

類似於第二種方法,隻不過是對CR和CB兩個通道綜合考慮

代碼

def crcb_range_sceening(image):
  """
  :param image: 圖片路徑
  :return: None
  """
  img = cv2.imread(image,cv2.IMREAD_COLOR)
  ycrcb=cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
  (y,cr,cb)= cv2.split(ycrcb)
 
  skin = np.zeros(cr.shape,dtype= np.uint8)
  (x,y)= cr.shape
  for i in range(0,x):
    for j in range(0,y):
      if (cr[i][j]>140)and(cr[i][j])<175 and (cr[i][j]>100) and (cb[i][j])<120:
        skin[i][j]= 255
      else:
        skin[i][j] = 0
  cv2.namedWindow(image,cv2.WINDOW_NORMAL)
  cv2.imshow(image,img)
  cv2.namedWindow(image+"skin2 cr+cb",cv2.WINDOW_NORMAL)
  cv2.imshow(image+"skin2 cr+cb",skin)
 
  dst = cv2.bitwise_and(img,img,mask=skin)
  cv2.namedWindow("cutout",cv2.WINDOW_NORMAL)
  cv2.imshow("cutout",dst)
 
  cv2.waitKey()

效果

4 HSV顏色空間H,S,V范圍篩選法

原理

還是轉換空間然後每個通道設置一個閾值綜合考慮,進行二值化操作。

代碼

def hsv_detect(image):
  """
  :param image: 圖片路徑
  :return: None
  """
  img = cv2.imread(image,cv2.IMREAD_COLOR)
  hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
  (_h,_s,_v)= cv2.split(hsv)
  skin= np.zeros(_h.shape,dtype=np.uint8)
  (x,y)= _h.shape
 
  for i in range(0,x):
    for j in range(0,y):
      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):
        skin[i][j] = 255
      else:
        skin[i][j] = 0
  cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  cv2.imshow(image, img)
  cv2.namedWindow(image + "hsv", cv2.WINDOW_NORMAL)
  cv2.imshow(image + "hsv", skin)
  dst = cv2.bitwise_and(img, img, mask=skin)
  cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  cv2.imshow("cutout", dst)
  cv2.waitKey()

效果

示例

import cv2
import numpy as np
 
 
def ellipse_detect(image):
  """
  :param image: img path
  :return: None
  """
  img = cv2.imread(image, cv2.IMREAD_COLOR)
  skinCrCbHist = np.zeros((256, 256), dtype=np.uint8)
  cv2.ellipse(skinCrCbHist, (113, 155), (23, 15), 43, 0, 360, (255, 255, 255), -1)
 
  YCRCB = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
  (y, cr, cb) = cv2.split(YCRCB)
  skin = np.zeros(cr.shape, dtype=np.uint8)
  (x, y) = cr.shape
  for i in range(0, x):
    for j in range(0, y):
      CR = YCRCB[i, j, 1]
      CB = YCRCB[i, j, 2]
      if skinCrCbHist[CR, CB] > 0:
        skin[i, j] = 255
  cv2.namedWindow(image, cv2.WINDOW_NORMAL)
  cv2.imshow(image, img)
  dst = cv2.bitwise_and(img, img, mask=skin)
  cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
  cv2.imshow("cutout", dst)
  cv2.waitKey()
 
 
 
if __name__ == '__main__':
  ellipse_detect('./test.png')

 到此這篇關於python opencv膚色檢測的實現示例的文章就介紹到這瞭,更多相關opencv 膚色檢測內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: