一.原始LBP:
1.LBPPics.py 文件:
main 输入图片
LBP_Cal(src) 计算值
#-------------------------------------------------------------------------------
# Name: LBPPics
# Purpose:
#
# Author: Lemon
#
# Created: 30/08/2020
# Copyright: (c) dell 2020
# Licence: MIT<licence>
#-------------------------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
import cv2 as cv
def LBP_Cal(src):
dst = np.zeros(src.shape,dtype = src.dtype)
for row in range(1,src.shape[0]-1):
for col in range(1,src.shape[1]-1):
center = src[row,col]
LBPtemp = 0
LBPtemp |= (src[row-1,col-1] >= center) << 7
LBPtemp |= (src[row-1,col ] >= center) << 6
LBPtemp |= (src[row-1,col+1] >= center) << 5
LBPtemp |= (src[row ,col-1] >= center) << 4
LBPtemp |= (src[row ,col+1] >= center) << 3
LBPtemp |= (src[row+1,col-1] >= center) << 2
LBPtemp |= (src[row+1,col ] >= center) << 1
LBPtemp |= (src[row+1,col+1] >= center) << 0
dst[row,col] = LBPtemp
return dst
def main():
src_ori = cv.imread("LBP_ORI.jpg")
src = cv.cvtColor(src_ori,cv.COLOR_RGB2GRAY)
dst = LBP_Cal(src)
cv.imwrite("lbp_dst.jpg",dst)
rate = np.zeros((1,256))
for row in range(0,dst.shape[0]-1):
for col in range(0,dst.shape[1]-1):
rate[0,dst[row,col]] = rate[0,dst[row,col]] + 1
np.save("rates.npy",rate)
np.save("LBPs.npy",dst)
if __name__ == '__main__':
main()
2.histogram.py文件:
导入LBPPics.py文件中导出的npy数据
计算出直方图
#-------------------------------------------------------------------------------
# Name: histogram
# Purpose:
#
# Author: Lemon
#
# Created: 30/08/2020
# Copyright: (c) dell 2020
# Licence: MIT<licence>
#-------------------------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
def main():
rate = np.load("rates.npy")
x = np.zeros(256)
for i in range(1,256):
x[i] = i
sum_LBP = sum(rate[0,:])
plt.bar(x, rate[0,:]/sum_LBP,align = 'center')
plt.show()
if __name__ == '__main__':
main()
二.Uniform_LBP:
1.Uniform_LBP.py文件:
main中输入图片,导出npy文件
#-------------------------------------------------------------------------------
# Name: Uniform_LBP
# Purpose:
#
# Author: Lemon
#
# Created: 30/08/2020
# Copyright: (c) dell 2020
# Licence: MIT<licence>
#-------------------------------------------------------------------------------
import numpy as np
import cv2
import os
np.set_printoptions(threshold = 1e6)
def arry_58():
l = np.zeros(58, dtype=int)
m = []
a = -1
for i in range(256):
bit = '{:08b}'.format(i) # 二进制化
arry = [] # 二进制生成数组
count = 0 # 计数变化次数
# 把字符串变为数组方便统计变化次数
for x in range(len(bit)):
arry.append(int(bit[x]))
# print(arry)
first = arry[0] # 数组第一个为first,与之后的比较
for j in range(1, len(arry)):
if arry[j] != first: # 如果变化,计数单位加1
count += 1
first = arry[j] # 并且把变化的值重新赋值
# print(count)
if count <= 2: # 如果变化次数小于2,则依次排序
a += 1
# print(i)
l[a] = i
l = l.tolist()
return l
def uniform_LBP(img):
h, w = img.shape # 图像的长和宽
dst = np.zeros((h - 2, w - 2), dtype=img.dtype) # 新建一张图
# dst = np.zeros(img.shape, dtype=img.dtype) # 新建一张图
arry58 = arry_58()
for i in range(1, h - 1):
for j in range(1, w - 1):
center = img[i][j]
code = []
count = 0
code7 = img[i - 1][j - 1]
if code7 >= center:
code7 = 1
else:
code7 = 0
code.append(code7)
code6 = img[i - 1][j]
if code6 >= center:
code6 = 1
else:
code6 = 0
code.append(code6)
code5 = img[i - 1][j + 1]
if code5 >= center:
code5 = 1
else:
code5 = 0
code.append(code5)
code4 = img[i][j + 1]
if code4 >= center:
code4 = 1
else:
code4 = 0
code.append(code4)
code3 = img[i + 1][j + 1]
if code3 >= center:
code3 = 1
else:
code3 = 0
code.append(code3)
code2 = img[i + 1][j]
if code2 >= center:
code2 = 1
else:
code2 = 0
code.append(code2)
code1 = img[i + 1][j - 1]
if code1 >= center:
code1 = 1
else:
code1 = 0
code.append(code1)
code0 = img[i][j - 1]
if code0 >= center:
code0 = 1
else:
code0 = 0
code.append(code0)
LBP = code7*128 + code6*64 + code5*32 + code4*16 + code3*8 + code2*4 + code1*2 + code0*1
#print("LBP值为:",LBP)
#print("8位编码为:",code)
first = code[0] # 数组第一个为first,与之后的比较
for x in range(1, len(code)):
if code[x] != first: # 如果变化,计数单位加1
count += 1
first = code[x] # 并且把变化的值重新赋值
#print("跳变次数",count)
if count > 2: # 如果变化次数大于3,则归为59位
dst[i - 1][j - 1] = 58
else: # 否则,按原来的数计算
loca = arry58.index(LBP) # 获取位置
dst[i - 1][j - 1] = loca
return dst
if __name__ == '__main__':
gray = cv2.imread('LBP.jpg', cv2.IMREAD_GRAYSCALE)
print(gray.shape)
a = uniform_LBP(gray)
Uniform_rate = np.zeros(59)
for i in range(0,a.shape[0]):
for j in range(0,a.shape[0]):
Uniform_rate[a[i,j]] += 1
np.save("uniform_rate.npy",Uniform_rate)
print(a.shape)
cv2.imshow('uniform_lbp', a)
cv2.waitKey(0)
2.Uniform_LBP_histogram.py文件:
导入Uniform_LBP.py最后导出的数据,画直方图
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Lemon
#
# Created: 30/08/2020
# Copyright: (c) dell 2020
# Licence: MIT<licence>
#-------------------------------------------------------------------------------
import numpy as np
from matplotlib import pyplot as plt
def main():
Uniform_LBP = np.load("uniform_rate.npy")
print(Uniform_LBP)
x = np.zeros(59)
for i in range(0,59):
x[i]=i
Uniform_sum = sum(Uniform_LBP)
plt.bar(x,Uniform_LBP/Uniform_sum)
plt.show()
if __name__ == '__main__':
main()
三.图片分割
此分割图片后计算LBP并导出LBP数据,把数据导入histogram.py可以画直方图
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Lemon
#
# Created: 30/08/2020
# Copyright: (c) dell 2020
# Licence: MIT<licence>
#-------------------------------------------------------------------------------
from matplotlib import pyplot as plt
import numpy as np
import cv2 as cv
from LBPpics import LBP_Cal
def main():
src_ori = cv.imread("LBP.jpg")
src_gray = cv.cvtColor(src_ori,cv.COLOR_RGB2GRAY)
list_dst = []
for i in range(0,8):
for j in range(0,8):
src_1 = src_gray[(0+i*64):(64+i*64),(0+j*64):(64+j*64)]
dst = LBP_Cal(src_1)
list_dst.append(dst)
array_dst = list_dst[0]
for i in range(1,len(list_dst)):
array_dst = np.vstack((array_dst,list_dst[i]))
print(array_dst.shape)
np.save("splide_dst.npy",array_dst)
if __name__ == '__main__':
main()