Python学习--手撸LBP实现过程

因为课题的原因,最近在分析自己的想法与思路的时候,准备先从头开始复现一下LBP特征值的提取过程,平常提取LBP特征往往都是调用Scikit-Image库,直接一句话就能完成,如下:

from skimage import feature
lbp =  feature.local_binary_pattern(img, 8, 1, method='nri_uniform')

然后再根据提取的lbp特征直接画直方图就行了。方便是方便,但还是动手做一下比较靠谱。

实验代码

LBP的分析以前也写过,具体参考这里LBP纹理特征整理 & 文献阅读整理
下面就不过多介绍了,直接上代码:

import numpy as np
import cv2
from PIL import Image
from pylab import *


class LBP:
    def __init__(self):
        # revolve_map为旋转不变模式的36种特征值从小到大进行序列化编号得到的字典
        self.revolve_map = {0: 0, 1: 1, 3: 2, 5: 3, 7: 4, 9: 5, 11: 6, 13: 7, 15: 8, 17: 9, 19: 10, 21: 11, 23: 12, 25: 13, 27: 14, 29: 15, 31: 16, 37: 17, 39: 18, 43: 19, 45: 20, 47: 21, 51: 22, 53: 23,55: 24,59: 25, 61: 26, 63: 27, 85: 28, 87: 29, 91: 30, 95: 31, 111: 32, 119: 33, 127: 34, 255: 35}
        # uniform_map为等价模式的58种特征值从小到大进行序列化编号得到的字典
        self.uniform_map = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 6: 5, 7: 6, 8: 7, 12: 8,14: 9, 15: 10, 16: 11, 24: 12, 28: 13, 30: 14, 31: 15, 32: 16, 48: 17, 56: 18, 60: 19, 62: 20, 63: 21, 64: 22, 96: 23, 112: 24,120: 25, 124: 26, 126: 27, 127: 28, 128: 29, 129: 30, 131: 31, 135: 32,143: 33, 159: 34, 191: 35, 192: 36, 193: 37, 195: 38, 199: 39, 207: 40,223: 41, 224: 42, 225: 43, 227: 44, 231: 45, 239: 46, 240: 47, 241: 48,243: 49, 247: 50, 248: 51, 249: 52, 251: 53, 252: 54, 253: 55, 254: 56,255: 57}

    # 图像的LBP原始特征计算算法:将图像指定位置的像素与周围8个像素比较
    # 比中心像素大的点赋值为1,比中心像素小的赋值为0,返回得到的二进制序列
    def calute_basic_lbp(self, image_array, i, j):
        sum = []
        if image_array[i - 1, j - 1] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i - 1, j] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i - 1, j + 1] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i, j - 1] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i, j + 1] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i + 1, j - 1] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i + 1, j] > image_array[i, j]:
            sum.append(1)
        else:
            sum.append(0)
        if image_array[i + 1, j
  • 10
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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特征向量。你可以根据需要调整参数以适应不同的图像和应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值