import cv2 import numpy as np import os import csv ################################################################################ print 'Set Image Directory' imgFile = 'images/skin_color_database/skin_color_database_1/' ################################################################################ # define variables for histogram colorRange = 64 # skin color counts countSkin = np.zeros(colorRange ** 3, np.double) # non-skin color counts countNonSkin = np.zeros(colorRange ** 3, np.double) # label sets true if it's a label image, otherwise sets false if it's an original image label = True ################################################################################ print 'Skin Color Histogram' for fileName in os.listdir(imgFile): if label == True: # load a label image print 'Label File Name:',fileName imgLabel = cv2.imread(imgFile + fileName) # convert color space from bgr to gray imgLabel = cv2.cvtColor(imgLabel, cv2.COLOR_BGR2GRAY) else: # load an original image print 'Original File Name:',fileName img = cv2.imread(imgFile + fileName) ############################################################################ # count pixel color values if label == False: # get image shape rows,cols,channels = img.shape for r in range(rows): for c in range(cols): # get values from rgb color space B = img.item(r,c,0) / 4 G = img.item(r,c,1) / 4 R = img.item(r,c,2) / 4 color = B * 64 * 64 + G * 64 + R if imgLabel.item(r,c) == 255: countSkin[color] += 1 else: countNonSkin[color] += 1 label = not label ################################################################################ # data combination # ---------countSkin------------ # ---------countNonSkin--------- data = [countSkin, countNonSkin] ################################################################################ # write csv file csvFile = open('skin_nonskin.csv','wb') writer = csv.writer(csvFile) for row in data: writer.writerow([row[col] for col in range(colorRange ** 3)]) ################################################################################ print 'Goodbye!'
import os import cv2 import csv import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d.axes3d import Axes3D ################################################################################ print 'read csv file' VRange = 64 * 64 * 64 print VRange countSkin = np.zeros(VRange, np.double) countNonSkin = np.zeros(VRange, np.double) csvFile = open('skin_nonskin.csv', "rb") reader = csv.reader(csvFile) rowNum = 0 for row in reader: colNum = 0 for col in row: # restore countSkin and countNonSkin modNum = rowNum - rowNum / 2 * 2 if modNum == 1: countNonSkin[((rowNum - 1) * VRange + colNum)] = float(col) if modNum == 0: countSkin[((rowNum - 1) * VRange + colNum)] = float(col) colNum += 1 rowNum += 1 ################################################################################ print 'Skin Color Database Result' # count print 'count of skin pixels:',countSkin print 'count of non-skin pixels:',countNonSkin ################################################################################ print 'prior probability' skinPix = sum(countSkin) nskinPix = sum(countNonSkin) PCs = skinPix / (skinPix + nskinPix) # P(Cs) PCns = 1 - PCs # P(Cns) print 'probability of skin class:',PCs print 'probability of non-skin class',PCns ################################################################################ print 'conditional probability variables' PVCs = np.zeros(VRange, np.double) # P(V|Cs) PVCns = np.zeros(VRange, np.double) # P(V|Cns) for i in range(VRange): # pixel color probability given skin color PVCs[i] = countSkin[i] / skinPix # pixel color probability given non-skin color PVCns[i] = countNonSkin[i] / nskinPix ################################################################################ print 'posterior probability' PCsV = np.zeros(VRange, np.double) # P(Cs|V) for i in range(VRange): # skin probability given pixel color PCsV[i] = (PVCs[i] * PCs) / (PVCs[i] * PCs + PVCns[i] * PCns + 0.00000001) ################################################################################ print 'determine skin distribution' # pixel color range v = range(VRange) # skin class threshold theta = [0.6,0.7,0.8,0.9] # color of a single class rgbColor = np.zeros(3, np.uint8) # rgb color value skinPix = np.zeros(VRange, np.uint8) # prepare for 3d plot fig = plt.figure() ax = fig.add_subplot(111, projection = '3d') for i in v: # split 3 channels(64b * 64b * 64b) B = i / (64 * 64) G = (i - B * 64 * 64) / 64 R = i - B * 64 * 64 - G * 64 # given probability as threshold if PCsV[i] > theta[3]: skinPix[i] = 4 ax.scatter(R, G, B, c='r', marker='o') else: skinPix[i] = 0 ################################################################################ print 'display distribution of skin color' ax.set_xlabel('Red') ax.set_ylabel('Green') ax.set_zlabel('Blue') plt.show() ################################################################################ print 'Goodbye!'<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
OpenCV Using Python——RGB颜色空间中的统计肤色模型
原文地址:http://blog.csdn.net/shadow_guo/article/details/43669637