python opencv肤色检测的实现示例
作者:George593 时间:2023-06-13 20:31:58
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')
来源:https://blog.csdn.net/weixin_40893939/article/details/84527037
标签:opencv,肤色检测
0
投稿
猜你喜欢
python基于scrapy爬取京东笔记本电脑数据并进行简单处理和分析
2023-08-05 03:18:06
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
2022-01-12 14:02:31
MySQL无法启动几种常见问题小结
2024-01-18 06:05:32
MySQL慢SQL语句常见诱因以及解决方法
2024-01-18 12:36:25
Python实现二叉树的最小深度的两种方法
2022-05-24 03:30:17
基于matplotlib xticks用法详解
2022-10-09 12:51:31
Dreamwaver 常见问答解答
2009-07-05 18:51:00
mysql 数据库备份的多种实现方式总结
2024-01-18 23:03:25
Go数组与切片轻松掌握
2024-02-12 19:55:36
asp.net中如何调用sql存储过程实现分页
2024-01-27 23:49:57
关于JSON以及JSON在PHP中的应用技巧
2023-11-16 00:03:38
Vue3+TS+Vite+NaiveUI搭建一个项目骨架实现
2024-05-28 15:55:14
Python3逻辑运算符与成员运算符
2021-03-29 18:59:14
IE中雅黑字体给布局带来的变化
2008-06-13 11:22:00
Python中pandas dataframe删除一行或一列:drop函数详解
2021-07-09 16:46:47
MySQL每天自动增加分区的实现
2024-01-23 16:18:37
为你总结一些php信息函数
2023-10-28 09:46:59
跟我学习javascript的定时器
2024-05-11 09:32:09
PHP在网页中动态生成PDF文件详细教程
2024-05-08 09:34:56
客户认同的就是好商品
2009-08-31 16:41:00