导入数据
import cv2 import numpy as np img = cv2.imread("./Pratheepan_Dataset/FacePhoto/03.jpg") #显示图像 #cv2.imshow("06Apr03Face",img) #cv2.waitKey(0) print("YCbCr-RGB Skin Model") rows,cols,channels = img.shape # convert color space from rgb to ycbcr imgYcc = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) #转换为YCBCR图像 imgCBCR = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB)
YCbCr-RGB Skin Model
批量的读取图片
import os picture = [] for filename in os.listdir(r"./Pratheepan_Dataset/FacePhoto/"): #listdir的参数是文件夹的路径 filenames = './Pratheepan_Dataset/FacePhoto/'+filename #print(filenames) img = cv2.imread(filenames,1) imgCBCR = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB) #print(imgCBCR.shape) picture.append(imgCBCR) Y = np.array([]) CB = np.array([]) CR = np.array([]) for i in range(len(picture)): a = picture[i][:,:,0].flatten() b = picture[i][:,:,1].flatten() c = picture[i][:,:,2].flatten() Y = np.hstack((Y,a)) CR = np.hstack((CR,b)) CB = np.hstack((CB,c)) print(Y[:10],CB[:10],CR[:10])
[204. 188. 193. 192. 194. 198. 196. 194. 198. 198.] [113. 112. 112. 112. 112. 112. 113. 114. 114. 114.] [136. 136. 136. 136. 136. 136. 135. 135. 136. 136.]
import os picture = [] for filename in os.listdir(r"./Ground_Truth/GroundT_FacePhoto/"): #listdir的参数是文件夹的路径 filenames = './Ground_Truth/GroundT_FacePhoto/'+filename #print(filenames) img = cv2.imread(filenames,cv2.IMREAD_GRAYSCALE) #print(img.shape) picture.append(img) lable = np.array([]) for i in range(len(picture)): a = picture[i][:,:].flatten() lable = np.hstack((lable,a)) print(lable.shape)
(5029741,)
在立体空间绘制散点分布
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D i = 10000 j = 200000 Y1 = Y[i:j] CB1 = CB[i:j] CR1 = CR[i:j] fig = plt.figure(figsize=(9,9)) ax = fig.add_subplot(111, projection='3d') ax.scatter(CR1[lable[i:j]==255.0],CB1[lable[i:j]==255.0],Y1[lable[i:j]==255.0], marker="o") #ax.scatter(CB1[lable[i:j]==0.0],CR1[lable[i:j]==0.0],Y1[lable[i:j]==0.0], marker="o") ax.set_xlabel('CR Label') ax.set_ylabel('CB Label') ax.set_zlabel('Y Label') plt.show()
将所有的数据拼成一个矩阵(n,3)
Y_1 = Y.reshape(len(Y),-1) CB_1 = Y.reshape(len(CB),-1) CR_1 = Y.reshape(len(CR),-1) X = np.hstack((np.hstack((Y_1,CB_1)),CR_1)) y = lable
利用贝叶斯分类器进行决策
from sklearn.naive_bayes import GaussianNB from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X,y/255., test_size=0.1, random_state=42) #建立模型 clf = GaussianNB() #使用训练集对模型进行训练 clf.fit(X_train,y_train) #使用测试集数据检验模型准确率 print("tsst_data1的准确率",clf.score(X_test,y_test))
tsst_data1的准确率 0.7102877876634027
预测
import cv2 import numpy as np img = cv2.imread("./11.jpg") #显示 #cv2.imshow("06Apr03Face",img) #cv2.waitKey(0) #转化为imgCBCR imgCBCR = cv2.cvtColor(img,cv2.COLOR_BGR2YCR_CB) Y_pre = imgCBCR[:,:,0].flatten() CR_pre = imgCBCR[:,:,1].flatten() CB_pre = imgCBCR[:,:,2].flatten()
Y_2 = Y_pre.reshape(len(Y_pre),-1) CB_2 =CB_pre.reshape(len(CB_pre),-1) CR_2 =CR_pre.reshape(len(CR_pre),-1) X_2 = np.hstack((np.hstack((Y_2,CB_2)),CR_2)) c = clf.predict(X_2) img1 = c.reshape(img[:,:,0].shape) cv2.imshow("06Apr03Face",img1) cv2.waitKey(0)
这是简单的实现没有考虑背景色,导致查准率比较低,下一步作业朝向这方面更改,努力中