(结构法)
Local binary patterns
方形邻域
圆形邻域
等价模式
根据8位二进制分为两类
第一类:等价模式类(占大部分)
第二类:混合模式类
256类
58类
1、人脸识别
2、图片相似度
模块&包
函数
参数解析:
image-进行LBP特征提取的灰度图像
P-邻域点的个数,通常为8
R-圆形邻域的半径通常为1
method-决定LBP向量的性质,默认的具有灰度尺度不变的特性
灰度尺度不变和旋转不变: * 'ror': extension of default implementation which is gray scale and rotation invariant.
* 'uniform': improved rotation invariance with uniform patterns and finer quantization of the angular space which is gray scale and rotation invariant.
* 'nri_uniform': non rotation-invariant uniform patterns variant which is only gray scale invariant [2]_.
* 'var': rotation invariant variance measures of the contrast of local image texture which is rotation but not gray scale invariant.
返回值
数据集的地址:https://pan.baidu.com/s/1bpjC7Vp
这篇博文是数字图像处理的大作业.
题目描述:给定40张不同风格的纹理图片,大小为512*512,要求将每张图片分为大小相同的9块,利用其中的5块作为训练集,剩余的4块作为测试集,构建适当的模型实现图片的分类.
图片如下图所示:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVR
from skimage import feature as skft
def loadPicture():
train_index = 0
test_index = 0
train_data = np.zeros( (200,171,171) )
test_data = np.zeros( (160,171,171) )
train_label = np.zeros( (200) )
test_label = np.zeros( (160) )
for i in np.arange(40):
image = mpimg.imread('picture/'+str(i)+'.tiff')
data = np.zeros( (513,513) )
data[0:image.shape[0],0:image.shape[1]] = image
#切割后的图像位于数据的位置
index = 0
#将图片分割成九块
for row in np.arange(3):
for col in np.arange(3):
if index<5:
train_data[train_index,:,:] = data[171*row:171*(row+1),171*col:171*(col+1)]
train_label[train_index] = i
train_index+=1
else:
test_data[test_index,:,:] = data[171*row:171*(row+1),171*col:171*(col+1)]
test_label[test_index] = i
test_index+=1
index+=1
return train_data,test_data,train_label,test_label
radius = 1
n_point = radius * 8
def texture_detect():
train_hist = np.zeros( (200,256) )
test_hist = np.zeros( (160,256) )
for i in np.arange(200):
#使用LBP方法提取图像的纹理特征.
lbp=skft.local_binary_pattern(train_data[i],n_point,radius,'default')
#统计图像的直方图
max_bins = int(lbp.max() + 1)
#hist size:256
train_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins))
for i in np.arange(160):
lbp = skft.local_binary_pattern(test_data[i],n_point,radius,'default')
#统计图像的直方图
max_bins = int(lbp.max() + 1)
#hist size:256
test_hist[i], _ = np.histogram(lbp, normed=True, bins=max_bins, range=(0, max_bins))
return train_hist,test_hist
train_data,test_data,train_label,test_label= loadPicture()
train_hist,test_hist = texture_detect()
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
score = OneVsRestClassifier(svr_rbf,-1).fit(train_hist, train_label).score(test_hist,test_label)
print(score)
(统计法)
Gray-level co-occurrence matrix
步长n
方向α
四个方向
矩阵阶数
角二阶矩(能量)ASM
相关性COR
对比度CON
差异性DISL
熵ENT
反差分矩阵HOMO
相邻像素相关性的表现
共生矩阵对角线元素反应灰度变化程度
优点:
缺点:
步骤
代码skimage和skimage.feature包
import numpy as np
from skimage.feature import greycomatrix, greycoprops
from skimage import io, color, img_as_ubyte
img = io.imread('colorful_lena.jpg')
gray = color.rgb2gray(img)
image = img_as_ubyte(gray)#变成8位无符号整型
#这一步类似于数据压缩,因为8位图像含有256个灰度级,这样会导致计算灰度共生矩阵是的计算量过大,因此将其进行压缩成16级,将灰度进行分区
bins = np.array([0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 255]) #16-bit
inds = np.digitize(image, bins)#返回的是一个和image大小一样的矩阵,只是矩阵元素表示的是image中元素在bins中的区间位置,小于0为0,0-16为1,以此类推
max_value = inds.max()+1
matrix_coocurrence = greycomatrix(inds, #需要进行共生矩阵计算的numpy矩阵
[1],#步长
[0, np.pi/4, np.pi/2, 3*np.pi/4],#方向角度
levels=max_value, #共生矩阵阶数
normed=False, symmetric=False)
#P[i,j,d,theta]返回的是一个四维矩阵,各维代表不同的意义
# GLCM properties
def contrast_feature(matrix_coocurrence):
contrast = greycoprops(matrix_coocurrence, 'contrast')
return "Contrast = ", contrast
def dissimilarity_feature(matrix_coocurrence):
dissimilarity = greycoprops(matrix_coocurrence, 'dissimilarity')
return "Dissimilarity = ", dissimilarity
def homogeneity_feature(matrix_coocurrence):
homogeneity = greycoprops(matrix_coocurrence, 'homogeneity')
return "Homogeneity = ", homogeneity
def energy_feature(matrix_coocurrence):
energy = greycoprops(matrix_coocurrence, 'energy')
return "Energy = ", energy
def correlation_feature(matrix_coocurrence):
correlation = greycoprops(matrix_coocurrence, 'correlation')
return "Correlation = ", correlation
def asm_feature(matrix_coocurrence):
asm = greycoprops(matrix_coocurrence, 'ASM')
return "ASM = ", asm
print(contrast_feature(matrix_coocurrence))
print(dissimilarity_feature(matrix_coocurrence))
print(homogeneity_feature(matrix_coocurrence))
print(energy_feature(matrix_coocurrence))
print(correlation_feature(matrix_coocurrence))
print(asm_feature(matrix_coocurrence))
https://www.cnblogs.com/dwdxdy/archive/2012/05/31/2528906.html
纹理特征提取方法:LBP, 灰度共生矩阵
LBP原理介绍以及算法实现
图像特征提取(纹理特征)
https://www.cnblogs.com/8335IT/p/5648445.html
python实现LBP方法提取图像纹理特征实现分类
纹理特征提取方法——灰度共生矩阵(GLCM )