其中的直方图的绘画部分要注意,只写了部分的直方图代码
###直接调用skimag库中的函数BLP提取纹理
from skimage import data
import matplotlib.pyplot as plt
import cv2
from skimage.feature import local_binary_pattern
image = data.camera()
plt.subplot(2,3,1)
plt.title('original')
plt.imshow(image,cmap='gray')
#不同方法的BLP展示
radius = 3# LBP算法中范围半径的取值
n_points = 8 * radius#领域内采样点数
#选择四种不同的LBP方法提取纹理
method = 'default'#灰度但不是旋转不变的
lbp = local_binary_pattern(image, n_points, radius,method)#LBP表达式
plt.subplot(232)
plt.imshow(lbp, plt.cm.gray)
plt.title('default BLP')
method = 'uniform'#改进的灰度和旋转不变
lbp = local_binary_pattern(image, n_points, radius,method)
plt.subplot(233)
plt.imshow(lbp, plt.cm.gray)
plt.title('uniform BLP')
method = 'var'#旋转但不是灰度不变的
lbp = local_binary_pattern(image, n_points, radius,method)
plt.subplot(234)
plt.imshow(lbp, plt.cm.gray)
plt.title('var BLP')
method = 'ror'#灰度和旋转不变
lbp = local_binary_pattern(image, n_points, radius,method)
plt.subplot(235)
plt.imshow(lbp, plt.cm.gray)
plt.title('ror BLP')
##加了一个边缘图像对比纹理图像
edges = filters.sobel(image)
plt.subplot(236)
plt.imshow(edges, plt.cm.gray)
plt.title('edge')
##############BLP圆形算子提取纹理,程序unit16
import numpy as np
import cv2
import matplotlib.pyplot as plt
import math
from skimage import data
def circular_LBP(src, radius, n_points):
height = src.shape[0]
width = src.shape[1]
# dst = np.zeros([height, width], dtype=np.uint8)
dst = src.copy()
src.astype(dtype=np.float32)
dst.astype(dtype=np.float32)
neighbours = np.zeros((1, n_points), dtype=np.uint8)
lbp_value = np.zeros((1, n_points), dtype=np.uint8)
for x in range(radius, width - radius ):
for y in range(radius, height - radius ):
lbp = 0.
for n in range(n_points):
theta = float(2 * np.pi * n) / n_points
x_n = x + radius * np.cos(theta)#计算采样点的坐标
y_n = y - radius * np.sin(theta)
# 向下取整
x1 = int(math.floor(x_n))
y1 = int(math.floor(y_n))
# 向上取整
x2 = int(math.ceil(x_n))
y2 = int(math.ceil(y_n))
# 将坐标映射到0-1之间
tx = np.abs(x - x1)
ty = np.abs(y - y1)
# 根据0-1之间的x,y的权重计算公式计算权重
w1 = (1 - tx) * (1 - ty)
w2 = tx * (1 - ty)
w3 = (1 - tx) * ty
w4 = tx * ty
# 根据双线性插值公式计算第k个采样点的灰度值
neighbour = src[y1, x1] * w1 + src[y2, x1] * w2 + src[y1, x2] * w3 + src[y2, x2] * w4
neighbours[0, n] = neighbour
center = src[y, x]
# ######确定中心值
for n in range(n_points):
if neighbours[0, n] > center:
lbp_value[0, n] = 1
else:
lbp_value[0, n] = 0
# 取出所有的中心值
for n in range(n_points):
lbp += lbp_value[0, n] * 2**n
# print('lbp_value[0, n] * 2**n : {}'.format(lbp_value[0, n] * 2**n))
# LBP值归一化处理,并把灰度值转到0-255
dst[y, x] = int(lbp / (2**n_points-1) * 255)
return dst
def show_basic_hist(a): #画lbp的直方图
hist = cv.calcHist([a],[0],None,[256],[0,256])
#hist = cv.normalize(hist,hist)
plt.plot(hist, color='r')
plt.xlim([0,256])
img2 = cv.imread("D:\honeycombed_0169.jpg",0)
plt.title('original hist')
show_basic_hist(img2)
if __name__ == '__main__':
img = data.camera()
dst18 = circular_LBP(img, radius=1, n_points=8)
dst38 = circular_LBP(img, radius=3, n_points=8)
dst316 = circular_LBP(img, radius=3, n_points=16)
plt.subplot(1,4,1)
plt.title('original')
plt.imshow(img,cmap='gray')
plt.subplot(1,4,2)
plt.title('dst18')
plt.imshow(dst18)
plt.subplot(1,4,3)
plt.title('dst38')
plt.imshow(dst38)
plt.subplot(1,4,4)
plt.title('dst316')
plt.imshow(dst316)
##########Untitled15 2.Gabor滤波器提取纹理特征
import cv2
import numpy as np
import pylab as pl
from PIL import Image
#构建Gabor滤波器
def build_filters():
filters = []
ksize = [7,9,11,13,15,17] #gabor有尺度 6个
lamda = np.pi/2.0 # 设置波长
for theta in np.arange(0,np.pi,np.pi/4): #gabor方向 0 45 90 135
for k in range(6):
kern= cv2.getGaborKernel((ksize[k],ksize[k]),1.0,theta,lamda,0.5,0,ktype=cv2.CV_32F)#生成Gabor 滤波核,即模板
kern/= 1.5*kern.sum()#规范化内核,确保内核不会改变整体亮度
filters.append(kern)
for temp in range(len(filters)):
plt.subplot(4, 6, temp + 1)
plt.imshow(filters[temp])
plt.show()
return filters
#滤波过程
def process(img,filters):
accum = np.zeros_like(img)#构造矩阵
for kern in filters:
fimg = cv2.filter2D(img,cv2.CV_8UC3,kern)#自定义卷积核实现卷积 np.maximum(accum,fimg,accum)#依次比较取最大
return accum
#特征图生成并显示
def getGabor(img,filters):
image = Image.open(img)
img_ndarray = np.asarray(image)#图像转换成数组
res = [] #滤波结果
for i in range(len(filters)):
res1 = process(img_ndarray,filters[i])
res.append(np.asarray(res1))
pl.figure(2)
for temp in range(len(res)):
pl.subplot(4,6,temp+1) #画4*6格子
pl.imshow(res[temp],cmap='gray')
show_basic_hist(res[temp])
pl.show()
return res
def show_basic_hist(a): #画Garbor的直方图
hist = cv.calcHist([a],[0],None,[256],[0,256])
#hist = cv.normalize(hist,hist)
plt.plot(hist, color='r')
plt.xlim([0,256])
plt.show()
img2 = cv.imread("D:\honeycombed_0169.jpg",0)
show_basic_hist(img2)
plt.title('original hist')
if __name__ == '__main__':
filters = build_filters()
getGabor('D:\img.jpg',filters)
实验结果
LBP的方法展示,原始图像有三张,分别为camera、模板、蜂窝,结果展示了三种图像在LBP提取纹理方法下的直方图。
模式识别作业,基于python提取图像纹理特征
于 2022-10-19 19:21:43 首次发布