欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python opencv膚色檢測(cè)的實(shí)現(xiàn)示例

 更新時(shí)間:2020年12月21日 14:14:32   作者:George593  
這篇文章主要介紹了python opencv膚色檢測(cè)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1 橢圓膚色檢測(cè)模型

原理:將RGB圖像轉(zhuǎn)換到Y(jié)CRCB空間,膚色像素點(diǎn)會(huì)聚集到一個(gè)橢圓區(qū)域。先定義一個(gè)橢圓模型,然后將每個(gè)RGB像素點(diǎn)轉(zhuǎn)換到Y(jié)CRCB空間比對(duì)是否再橢圓區(qū)域,是的話判斷為皮膚。

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法閾值分割算法

原理

針對(duì)YCRCB中CR分量的處理,將RGB轉(zhuǎn)換為YCRCB,對(duì)CR通道單獨(dú)進(jìn)行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范圍篩選法

 原理

類似于第二種方法,只不過是對(duì)CR和CB兩個(gè)通道綜合考慮

代碼

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范圍篩選法

原理

還是轉(zhuǎn)換空間然后每個(gè)通道設(shè)置一個(gè)閾值綜合考慮,進(jìn)行二值化操作。

代碼

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')

 到此這篇關(guān)于python opencv膚色檢測(cè)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)opencv 膚色檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python pytorch模型轉(zhuǎn)onnx模型的全過程(多輸入+動(dòng)態(tài)維度)

    python pytorch模型轉(zhuǎn)onnx模型的全過程(多輸入+動(dòng)態(tài)維度)

    這篇文章主要介紹了python pytorch模型轉(zhuǎn)onnx模型的全過程(多輸入+動(dòng)態(tài)維度),本文給大家記錄記錄了pt文件轉(zhuǎn)onnx全過程,簡單的修改即可應(yīng)用,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-03-03
  • 分享15?個(gè)python中的?Scikit-Learn?技能

    分享15?個(gè)python中的?Scikit-Learn?技能

    這篇文章主要介紹了分享15?個(gè)python中的?Scikit-Learn?技能,Scikit-Learn?是一個(gè)非常棒的?python?庫,用于實(shí)現(xiàn)機(jī)器學(xué)習(xí)模型和統(tǒng)計(jì)建模,有降維、特征選擇、特征提取、集成技術(shù)等特征,下文相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-03-03
  • 解析Anaconda創(chuàng)建python虛擬環(huán)境的問題

    解析Anaconda創(chuàng)建python虛擬環(huán)境的問題

    這篇文章主要介紹了Anaconda創(chuàng)建python虛擬環(huán)境,包括虛擬環(huán)境管理、虛擬環(huán)境中python包管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Sublime開發(fā)python程序的示例代碼

    Sublime開發(fā)python程序的示例代碼

    本篇文章主要介紹了Sublime開發(fā)python程序的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Python接口測(cè)試文件上傳實(shí)例解析

    Python接口測(cè)試文件上傳實(shí)例解析

    這篇文章主要介紹了Python接口測(cè)試文件上傳實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python自定義元類的實(shí)例講解

    Python自定義元類的實(shí)例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于Python自定義元類的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-03-03
  • 最新評(píng)論