机器学习图像特征提取—LBP纹理特征提取原理及代码实现

目录

1 简介

2 具体原理和改进

3 LBP代码的实现


1 简介

       LBP(局部二值模式)是一种用来描述图像局部纹理特征的算子;LBP算子定义为在3*3的窗口内,以窗口中心像素为阈值,将相邻的8个像素的灰度值与其进行比较,若周围像素值大于中心像素值,则该像素点的位置被标记为1,否则为0。这样,3*3邻域内的8个点经比较可产生8位二进制数(通常转换为十进制数即LBP码,共256种),即得到该窗口中心像素点的LBP值,并用这个值来反映该区域的纹理信息。

2 具体原理和改进

        这部分我没有自己整理,因为我感觉比较乱,内容也比较多,具体可以参考博客LBP特征_羊肉串串魅力无穷的博客-CSDN博客_lbp特征,他里面描述的很详细,可以供我们参考学习。

3 LBP代码的实现

     该代码利用的是skimage库中的local_binary_pattern函数实现LBP的特征提取,该函数的原型为:
skimage.feature.local_binary_pattern(image, P, R, method=‘default’)
image:灰度图像的像素矩阵
P:选取中心像素周围的像素点的个数
R:选取的区域的半径
method : {‘default’, ‘ror’, ‘uniform’, ‘var’}

Method to determine the pattern.

    ‘default’: original local binary pattern which is gray scale but notrotation invariant.

    ‘ror’: extension of default implementation which is gray scale androtation invariant.

    ‘uniform’: improved rotation invariance with uniform patterns andfiner quantization of the angular space which is gray scale and
rotation invariant.

    ‘nri_uniform’: non rotation-invariant uniform patterns variantwhich is only gray scale invariant .

    ‘var’: rotation invariant variance measures of the contrast of localimage texture which is rotation but not gray scale invariant.

具体LBP算法代码实现:

#局部二值模式,是局部信息提取中的一种方法,它具有旋转不变性和灰度不变性等显著的优点。
from skimage.transform import rotate
from skimage.feature import local_binary_pattern
from skimage import data, io
from skimage.color import label2rgb
import skimage
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2

# settings for LBP
radius = 3
n_points = 8 * radius

# 读取图像
image = cv2.imread('photo/2-5-1250.bmp')
#显示到plt中,需要从BGR转化到RGB,若是cv2.imshow(win_name, image),则不需要转化
#print(image.shape) #图片通道数
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.subplot(131)
plt.imshow(image1,cmap='gray')

# 转换为灰度图显示
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#print(image.shape)
plt.subplot(132)
plt.imshow(image, cmap='gray')
cv2.namedWindow("gray_image",cv2.WINDOW_GUI_NORMAL)
cv2.imshow("gray_image",image)
# 处理
lbp = local_binary_pattern(image, n_points,radius,method='uniform')
"""
'default':原始的局部二值模式,它是灰度但不是旋转不变的。
'ror':扩展灰度和旋转不变的默认实现。
'uniform':改进的旋转不变性和均匀的模式以及角度空间的更精细的量化,灰度和旋转不变。
'nri_uniform':非旋转不变的均匀图案变体,它只是灰度不变的R199。
'VAR':局部对比度的旋转不变方差度量,图像纹理是旋转但不是灰度不变的。
"""
plt.subplot(133)
plt.imshow(lbp)
plt.show()

print(lbp.shape)
# print(lbp)
cv2.namedWindow("lbp_image",cv2.WINDOW_GUI_NORMAL)
cv2.imshow("lbp_image",lbp)
n_bins = int(lbp.max() + 1)
lbp_hist, _ = np.histogram(lbp, density=True, bins=n_bins, range=(0, n_bins))#绘制直方图,百分比normed=True,像素个数normed=flase
#lbp_hist, _ = cv2.calcHist([lbp],[0],None,[256],[0,255])#像素个数
plt.plot(lbp_hist)
# print(lbp_hist)
# print(lbp_hist.shape)
plt.show()
cv2.waitKey(0)

参考来源:LBP特征_羊肉串串魅力无穷的博客-CSDN博客_lbp特征

LBP特征提取算子光照不变性和旋转不变性的具体解释与detectMultiScale参数说明【DataWhale学习记录】_MarToony|名角的博客-CSDN博客_lbp旋转不变性

猜你喜欢

转载自blog.csdn.net/weixin_42795788/article/details/124187078