因为课题的原因,最近在分析自己的想法与思路的时候,准备先从头开始复现一下LBP特征值的提取过程,平常提取LBP特征往往都是调用Scikit-Image库,直接一句话就能完成,如下:
from skimage import feature
lbp = feature.local_binary_pattern(img, 8, 1, method='nri_uniform')
然后再根据提取的lbp特征直接画直方图就行了。方便是方便,但还是动手做一下比较靠谱。
实验代码
LBP的分析以前也写过,具体参考这里LBP纹理特征整理 & 文献阅读整理
下面就不过多介绍了,直接上代码:
import numpy as np
import cv2
from PIL import Image
from pylab import *
class LBP:
def __init__(self):
# revolve_map为旋转不变模式的36种特征值从小到大进行序列化编号得到的字典
self.revolve_map = {0: 0, 1: 1, 3: 2, 5: 3, 7: 4, 9: 5, 11: 6, 13: 7, 15: 8, 17: 9, 19: 10, 21: 11, 23: 12, 25: 13, 27: 14, 29: 15, 31: 16, 37: 17, 39: 18, 43: 19, 45: 20, 47: 21, 51: 22, 53: 23,55: 24,59: 25, 61: 26, 63: 27, 85: 28, 87: 29, 91: 30, 95: 31, 111: 32, 119: 33, 127: 34, 255: 35}
# uniform_map为等价模式的58种特征值从小到大进行序列化编号得到的字典
self.uniform_map = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 6: 5, 7: 6, 8: 7, 12: 8,14: 9, 15: 10, 16: 11, 24: 12, 28: 13, 30: 14, 31: 15, 32: 16, 48: 17, 56: 18, 60: 19, 62: 20, 63: 21, 64: 22, 96: 23, 112: 24,120: 25, 124: 26, 126: 27, 127: 28, 128: 29, 129: 30, 131: 31, 135: 32,143: 33, 159: 34, 191: 35, 192: 36, 193: 37, 195: 38, 199: 39, 207: 40,223: 41, 224: 42, 225: 43, 227: 44, 231: 45, 239: 46, 240: 47, 241: 48,243: 49, 247: 50, 248: 51, 249: 52, 251: 53, 252: 54, 253: 55, 254: 56,255: 57}
# 图像的LBP原始特征计算算法:将图像指定位置的像素与周围8个像素比较
# 比中心像素大的点赋值为1,比中心像素小的赋值为0,返回得到的二进制序列
def calute_basic_lbp(self, image_array, i, j):
sum = []
if image_array[i - 1, j - 1] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i - 1, j] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i - 1, j + 1] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i, j - 1] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i, j + 1] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i + 1, j - 1] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i + 1, j] > image_array[i, j]:
sum.append(1)
else:
sum.append(0)
if image_array[i + 1, j