opencv椭圆检测python_pythonopencv肤⾊检测的实现⽰例1 椭圆肤⾊检测模型
原理:将RGB图像转换到YCRCB空间,肤⾊像素点会聚集到⼀个椭圆区域。先定义⼀个椭圆模型,然后将每个RGB像素点转换到YCRCB 空间⽐对是否再椭圆区域,是的话判断为⽪肤。
mab培训YCRCB颜⾊空间
椭圆模型
代码
def ellip_detect(image):
"""
:param image: 图⽚路径
:return: None
"""
img = cv2.imread(image,cv2.IMREAD_COLOR)
网店营销
skinCrCbHist = np.zeros((256,256), dtype= np.uint8 )
cv2.ellip(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.bitwi_and(img,img,mask= skin)
cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
cv2.imshow("cutout",dst)
cv2.waitKey()yoomiii
效果
arms
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)
compliment
dst = cv2.bitwi_and(img, img, mask=skin)
cv2.namedWindow("perate", cv2.WINDOW_NORMAL)
cv2.imshow("perate", dst)
cv2.waitKey()
效果
3 基于YCrCb颜⾊空间Cr, Cb范围筛选法
原理
say it right类似于第⼆种⽅法,只不过是对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
el:
在线中英翻译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.bitwi_and(img,img,mask=skin)
cv2.namedWindow("cutout",cv2.WINDOW_NORMAL)
cv2.imshow("cutout",dst)
cv2.waitKey()
效果
alp4 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
el:
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.bitwi_and(img, img, mask=skin)
cv2.namedWindow("cutout", cv2.WINDOW_NORMAL)
cv2.imshow("cutout", dst)
cv2.waitKey()
效果
>2317