LBP改进算法DLBP
时间: 2023-12-06 07:38:45 浏览: 114
超级详细全面的LBP特征讲解
DLBP是一种改进的LBP算法,它引入了一个方向梯度直方图(Directional Gradient Histogram,DGH)来解决LBP算法的旋转不变性问题。DLBP算法的步骤如下:
1.将图像分成若干个小块,对于每个小块,计算其DGH。
2.对于每个像素,计算其梯度方向,并将其分到最近的8个方向之一。
3.对于每个像素,计算其与周围8个像素的灰度值差,并将其转化为一个8位二进制数。
4.将每个像素的二进制数转化为一个十进制数,作为该像素的LBP值。
5.将每个小块中所有像素的LBP值组成一个直方图,作为该小块的DGH。
6.将所有小块的DGH拼接起来,作为整个图像的DLBP特征向量。
下面是DLBP算法的Python实现代码:
```python
import cv2
import numpy as np
def get_dgh(img, cell_size):
h, w = img.shape[:2]
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
ang = np.int32(ang / (np.pi / 4) + 0.5) % 8
dgh = np.zeros((cell_size, cell_size, 8), dtype=np.float32)
for i in range(cell_size):
for j in range(cell_size):
x1 = int(i * h / cell_size)
x2 = int((i + 1) * h / cell_size)
y1 = int(j * w / cell_size)
y2 = int((j + 1) * w / cell_size)
for k in range(x1, x2):
for l in range(y1, y2):
dgh[i, j, ang[k, l]] += mag[k, l]
return dgh.flatten()
def get_dlbp(img, cell_size):
h, w = img.shape[:2]
lbp = np.zeros((h, w), dtype=np.uint8)
for i in range(1, h - 1):
for j in range(1, w - 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[i, j] = code
dlbp = np.zeros((cell_size * cell_size * 8), dtype=np.float32)
for i in range(cell_size, h - cell_size, cell_size):
for j in range(cell_size, w - cell_size, cell_size):
dgh = get_dgh(lbp[i - cell_size:i + cell_size, j - cell_size:j + cell_size], cell_size)
dlbp[(i // cell_size - 1) * (w // cell_size - 2) * 8 + (j // cell_size - 1) * 8:(i // cell_size - 1) * (w // cell_size - 2) * 8 + (j // cell_size - 1) * 8 + 8] = dgh
return dlbp
img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
dlbp = get_dlbp(img, 8)
print(dlbp)
```
阅读全文