首页 > 代码库 > 简述人脸特异性识别&&一个基于LBP和SVM的人脸识别小例子

简述人脸特异性识别&&一个基于LBP和SVM的人脸识别小例子

 

原谅我用图片,MAC在Safari里给文章进行图文排版太麻烦啦~

本文适合初入计算机视觉和模式识别方向的同学们观看~

文章写得匆忙,加上博主所知甚少,有不妥和勘误请指出并多多包涵。

本文Demo的代码由HZK编写,特征点由月神和YK选择和训练.

转载请注明 copyleft by sciencefans, 2014

为了方便大家学习,附上高维LBP的核心代码

  1 ###################################################  2 #  3 #  4 #    NO LONGER USEFUL....  5 #  6 #  7 ###################################################  8 def lbp_ave(img, radius, x, y):  9     pix_count = 0 10     accu = 0 11     for i in range(x, x+radius): 12         for j in range(y, y+radius): 13             if(i >= 0 and i < img.shape[0] and j>=0 and j < img.shape[1]): 14                 pix_count += 1 15                 accu += img[i][j] 16     if(pix_count != 0): 17         return accu/pix_count 18     return 0 19  20 ################################################### 21 # 22 # 23 #    Now dolbp is a simple eight-point lbp calculating function 24 # 25 #    it will be updated to multi-point scalable-radius version later 26 # 27 # 28 ################################################### 29 def dolbp(img, x, y, radius, numsamplepoints): 30     lbpval = 0 31     threshold = img[x][y] 32     if threshold <= img[x - 1][y]: 33         lbpval += 128 34     if threshold <= img[x - 1][y - 1]: 35         lbpval += 64 36     if threshold <= img[x][y - 1]: 37         lbpval += 32 38     if threshold <= img[x + 1][y - 1]: 39         lbpval += 16 40     if threshold <= img[x + 1][y]: 41         lbpval += 8 42     if threshold <= img[x + 1][y + 1]: 43         lbpval += 4 44     if threshold <= img[x][y + 1]: 45         lbpval += 2 46     if threshold <= img[x - 1][y + 1]: 47         lbpval += 1 48  49     return lbpval 50 def isuniform(val, numpoints): 51     bit = 0 52     lastbit = 0 53     changecount = 0 54     if val & (1 << (numpoints-1)): 55         bit = 1 56     else: 57         bit = 0 58      59     for i in range(0, numpoints-1)[::-1]: 60         if val & (1 << i): 61             bit = 1 62         else: 63             bit = 0 64         if bit != lastbit: 65             changecount += 1 66         lastbit = bit 67      68     if changecount <= 2: 69         return True 70     else: 71         return False 72  73 def lbpfe_uniform(histogram, numpoints): 74     notuniformcount = 0 75     histogram_uniformed = [] 76     for i in range(2**numpoints): 77         if histogram[i] >= 0:#make sure the number of this bin is minimal 78             if isuniform(i, numpoints) == True: 79                 histogram_uniformed.append(histogram[i]) 80             else: 81                 notuniformcount += histogram[i] 82     return histogram_uniformed 83  84 #if a bin 85 def lbpfe_minimalize(histogram): 86     return histogram 87  88 def lbpfe_normalize(histogram): 89     accu = 0 90     normalized = [] 91     for i in histogram: 92         accu += i 93     if accu == 0: 94         for i in histogram: 95             normalized.append(float(0)) 96     else: 97         for i in histogram: 98             normalized.append(float(i)/accu) 99     return normalized100 101 #x,y is the position of the cell‘s lower-left pixel102 def lbpfe_calccell(img, x, y, cellsize, isminimalized, radius, numsamplepoints):103     #Generate the histogram list104     histogram_raw = []105     #add zeros to the list106     for i in range(2**numsamplepoints):107         histogram_raw.append(0)108     #do advanced LBP for each pixel in the cell109     for i in range(x, x+cellsize-1):110         for j in range(y, y+cellsize-1):111             histogram_raw[dolbp(img, i, j, radius, numsamplepoints)] += 1112             113     #make it minimal114     # if a bin iis meaningless, it will be set to -1115     if isminimalized == True:116         histogram_minimalized = lbpfe_minimalize(histogram_raw)117     else:118         histogram_minimalized = histogram_raw119     #Make it uniform120     histogram_uniformed = lbpfe_uniform(histogram_minimalized, numsamplepoints)121     #Normalize it!122     histogram_normalized = lbpfe_normalize(histogram_uniformed)123     # Well done!!!!!!!!124     return histogram_normalized125 ###################################################126 #127 #    LBP feature extraction function.......128 #129 #    img: input greyscale image array130 #    point: vectors of interest [[x1,y1],[x2,y2],......]131 #    cellsize: ^-^132 #    isminimalized: 133 #134 #    there are only 58 uniform modes, so there are 59 categories135 #136 #    the cells is numbered like this:137 #138 #    ^ 13   14   15   16139 #    |  9   10   11   12140 #    |  5    6    7    8141 #    y  1    2    3    4142 #       x------------->143 #144 ###################################################145 def lbpfe(img, points, cellsize, isminimalized=False, radius=1.0, numsamplepoints=8):146     histogram_combined = []147     histogram_nomalized = []148     #for each interest point:149     for point in points:150         #for each cell:151         histogram_nomalized = []152         for i in [-2, -1, 0, 1]:153             for j in [-2, -1, 0, 1]:154                 histogram_nomalized.extend(lbpfe_calccell(img, int(point[0]) + i * cellsize, int(point[1]) + j * cellsize, cellsize, isminimalized, radius, numsamplepoints))155                 #print i, j, ‘len h_n=‘, len(histogram_nomalized)156         #store the result of each point to a list157         histogram_combined.extend(histogram_nomalized)158     return histogram_combined