参考资料:
python skimage库LBP提取特征local_binary_pattern参数解释_local_binary_pattern函数_friedrichor的博客-CSDN博客
LBP特征笔记_亦枫Leonlew的博客-CSDN博客
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from skimage.feature import local_binary_patternimg = cv.imread("../SampleImages/titanfall.jpg", cv.IMREAD_COLOR)
plt.imshow(img[:,:,::-1])#转换为灰度图
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
plt.imshow(img_gray, plt.cm.gray)#LBP特征提取
#可以使用PIL Image,也可以使用skimage
#本示例使用skimage(scikit-image包,基于scipy的图像处理包),首先要确保
#skimage包已经安装,可以使用pip install scikit-image来进行安装
#1. 设置需要的参数
# LBP算法中半径参数
radius = 1
# 邻域像素点个数
n_points = 8 * radius
#原始LBP特征提取
lbp = local_binary_pattern(img_gray, 8, 1)
#圆形LBP特征提取
lbp_ror = local_binary_pattern(img_gray, n_points, radius, method="ror")
#旋转不变LBP特征提取
lbp_var = local_binary_pattern(img_gray, n_points, radius, method="var")
#等价特征
lbp_uniform = local_binary_pattern(img_gray, n_points, radius, method="nri_uniform")fig,axes = plt.subplots(nrows=2, ncols=2, figsize=(16,16), dpi=100)
axes[0][0].set_title("LBP")
axes[0][0].imshow(lbp, plt.cm.gray)
axes[0][1].set_title("LBP ROR")
axes[0][1].imshow(lbp_ror, plt.cm.gray)
axes[1][0].set_title("LBP VAR")
axes[1][0].imshow(lbp_var, plt.cm.gray)
axes[1][1].set_title("LBP NRI_UNIFORM")
axes[1][1].imshow(lbp_uniform, plt.cm.gray)