#!/usr/bin/env python
import cv2
import numpy as np
import matplotlib.pylab as plt
imname = 'C:\\Users\\71429\\Desktop\\imglab\\input_image\\right10.jpg'
img = cv2.imread(imname, cv2.IMREAD_COLOR)
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb) # 把图像转换到YUV色域
(y, cr, cb) = cv2.split(ycrcb) # 图像分割, 分别获取y, cr, br通道图像
# 高斯滤波, cr 是待滤波的源图像数据, (5,5)是值窗口大小, 0 是指根据窗口大小来计算高斯函数标准差
cr1 = cv2.GaussianBlur(cr, (5, 5), 0) # 对cr通道分量进行高斯滤波
# 根据OTSU算法求图像阈值, 对图像进行二值化
_, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#cv2.imshow("image CR", cr1)
cv2.imshow("Skin Cr+OSTU", skin1)
# 设置卷积核
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
opening = cv2.morphologyEx(skin1, cv2.MORPH_OPEN, kernel)
#显示效果
cv2.imshow('result',opening)
cv2.waitKey(100)
# 图像膨胀
dilate_result = cv2.dilate(opening, kernel)
# 图像腐蚀
erode_img = cv2.erode(dilate_result, kernel)
cv2.imshow("img", erode_img)
dilate_result = cv2.dilate(erode_img, kernel)
cv2.imshow("img", dilate_result)
cv2.waitKey(0)