python实现LBP纹理特征提取
时间: 2023-07-11 09:53:32 浏览: 128
LBP.rar_LBP高程纹理_纹理特征 遥感_遥感图像_遥感图像 LBP_遥感特征提取
好的,下面是Python实现LBP纹理特征提取的代码:
```python
import cv2
import numpy as np
#计算LBP值
def calc_lbp(img):
rows, cols = img.shape
lbp_img = np.zeros_like(img)
for i in range(1, rows-1):
for j in range(1, cols-1):
center = img[i, j]
code = 0
code |= (img[i-1, j-1] >= center) << 7
code |= (img[i-1, j] >= center) << 6
code |= (img[i-1, j+1] >= center) << 5
code |= (img[i, j+1] >= center) << 4
code |= (img[i+1, j+1] >= center) << 3
code |= (img[i+1, j] >= center) << 2
code |= (img[i+1, j-1] >= center) << 1
code |= (img[i, j-1] >= center) << 0
lbp_img[i, j] = code
return lbp_img
#计算LBP直方图特征
def calc_lbp_hist(lbp_img):
hist, _ = np.histogram(lbp_img.ravel(), 256, [0, 256])
return hist
#计算LBP特征向量
def calc_lbp_feature(img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
lbp_img = calc_lbp(gray_img)
hist = calc_lbp_hist(lbp_img)
feature = hist.astype('float32')
feature /= (feature.sum() + 1e-7)
return feature
#测试
img = cv2.imread('test.jpg')
feature = calc_lbp_feature(img)
print(feature)
```
这段代码中,calc_lbp函数用于计算LBP值,calc_lbp_hist函数用于计算LBP直方图特征,calc_lbp_feature函数用于计算LBP特征向量。最后,我们读入一张测试图片,计算其LBP特征向量并输出。
阅读全文