LBP统计直方图分为整体统计和分块统计
1、整体统计
即将生成的LBP特征进行整幅图像的统计,这样做的缺点时丢失了位置信息,但是维度较小。
实现代码:
假设生成的LBP特征如下面所示,大小为6×18
14 15 31 30 6 19 16 1 227 225 201 189 54 0 33 193 255 255
16 0 25 0 3 243 224 193 225 249 252 20 32 225 229 207 159 52
248 252 252 70 207 239 231 231 239 255 60 32 235 253 134 131 185 0
254 60 4 7 131 143 7 143 159 62 36 67 249 24 4 227 251 76
61 48 0 129 249 144 1 139 31 62 2 235 255 124 4 231 251 16
188 124 226 255 252 100 195 221 28 14 71 255 190 52 0 199 255 112
现在对上面的数据进行统计,使用整体统计,就会输出一个256维的向量,统计每个数字出现多少次,首先将上面的数字拷贝到txt文件中,方便我们读取,代码如下:
import numpy as np
np.set_printoptions(threshold = 1e6)
def Lbp(image):
l = np.zeros(256, dtype=int)
for a in range(6):
for b in range(18):
l[image[a][b]] = l[image[a][b]] + 1
lbp2 = np.array(l)
lbp2 = lbp2.reshape(1, -1)
return lbp2
if __name__ == '__main__':
image = np.loadtxt(r'./1.txt', delimiter=" ", dtype=int)
LBP_zhifangtu = Lbp(image)
print(LBP_zhifangtu)
运行结果如下:
[[6 2 1 1 3 0 1 2 0 0 0 0 0 0 2 1 3 0 0 1 1 0 0 0 1 1 0 0 1 0 1 2 2 1 0 0
1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 1 0 0 0 0 0 2 1 2 0 0 0 0 1 0 0 1 1
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 1 0 2 0 0 1 0 0 0 0 1 0 0 0 2
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 1 1 1 0 0 2 0 1 0 0 0 1 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 1 3 1 2 0 1 0 3 0 0 0 2 0 0 0 2 0 0 0 1 0 0 0 0 1 3 0 2
4 1 1 7]]
2、分块统计
分块统计是指将LBP特征图分成几块,假设一张LBP直方图为36×36,那么取子模块大小为6×6窗口大小,依次统计直方图,共计可以统计出36个0-255的统计直方图,将这36个直方图依次连接起来,就为最终的LBP统计直方图。
我们的数据依然是上面的数据,大小为6×18:
14 15 31 30 6 19 16 1 227 225 201 189 54 0 33 193 255 255
16 0 25 0 3 243 224 193 225 249 252 20 32 225 229 207 159 52
248 252 252 70 207 239 231 231 239 255 60 32 235 253 134 131 185 0
254 60 4 7 131 143 7 143 159 62 36 67 249 24 4 227 251 76
61 48 0 129 249 144 1 139 31 62 2 235 255 124 4 231 251 16
188 124 226 255 252 100 195 221 28 14 71 255 190 52 0 199 255 112
现在假设选择窗口大小为6×6,依次提取直方图,然后组合为统计直方图,则统计直方图维度为1×3×256
实现代码:
import numpy as np
import os
np.set_printoptions(threshold = 1e6)
# 设定窗口大小
gao = 6
kuan = 6
def Lbp(image):
cude = []
h, w = image.shape
for i in range(0, int(h/gao)):
for j in range(0, int(w/kuan)):
m = image[i*gao:i*gao+gao, j*kuan:j*kuan+kuan]
l = np.zeros(256, dtype=int)
for a in range(gao):
for b in range(kuan):
l[m[a][b]] = l[m[a][b]] + 1
cude.append(l)
lbp = np.array(cude)
lbp1 = []
for m in range(0, int(int(h/gao)*int(h/kuan))):
for i in lbp[m]:
lbp1.append(i)
lbp2 = np.array(lbp1)
lbp2 = lbp.reshape(1,-1)
return lbp2
if __name__ == '__main__':
image = np.loadtxt(r'./1.txt', delimiter=" ", dtype=int)
LBP_zhifangtu = Lbp(image)
print(LBP_zhifangtu)
运行结果如下:
[[3 0 0 1 1 0 1 1 0 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 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