Python Opencv实践 - LBP特征提取

参考资料:

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_pattern

img = 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)

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MB-LBP(Multi-Band Local Binary Pattern)是一种用于纹理特征描述的图像处理算法。它基于Local Binary Pattern(LBP)算法,通过对图像进行多尺度分析和多频带分解来提取更丰富的纹理信息。 在Python中,你可以使用一些图像处理库来实现MB-LBP算法,例如OpenCV和scikit-image。下面是一个使用OpenCV进行MB-LBP特征提取的简单示例代码: ```python import cv2 import numpy as np def mb_lbp(image): # 将图像转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 定义LBP算子的参数 radius = 1 neighbors = 8 # 定义多尺度和多频带的参数 scales = [1, 2, 4] bands = [0.5, 1, 2] # 存储MB-LBP特征 features = [] # 对每个尺度和频带进行MB-LBP特征提取 for scale in scales: for band in bands: # 根据尺度和频带参数对图像进行缩放和滤波 resized = cv2.resize(gray, None, fx=scale, fy=scale) filtered = cv2.GaussianBlur(resized, (0, 0), sigmaX=band) # 计算LBP特征 lbp = np.zeros_like(filtered) for i in range(neighbors): x = int(radius * np.cos(2 * np.pi * i / neighbors)) y = int(radius * np.sin(2 * np.pi * i / neighbors)) lbp += (filtered >= filtered[y:y+filtered.shape[0], x:x+filtered.shape[1]]) # 将LBP特征转换为直方图 hist, _ = np.histogram(lbp, bins=np.arange(2**neighbors + 1), density=True) # 将直方图添加到特征列表中 features.extend(hist) return features # 读取图像 image = cv2.imread('image.jpg') # 提取MB-LBP特征 features = mb_lbp(image) # 打印特征向量的长度 print(len(features)) ``` 在这个示例中,`mb_lbp`函数接受一个图像作为输入,并返回提取的MB-LBP特征向量。你可以根据需要调整参数以适应不同的图像和应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦枫Leonlew

希望这篇文章能帮到你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值