如何计算旋转不变均匀LBP特征时设置Cellsizes为【32 32】然后显示LBP图谱和LBP特征直方图
时间: 2024-02-20 22:56:52 浏览: 74
计算旋转不变均匀LBP特征时,需要使用OpenCV库中的函数`cv2.spatial_histogram()`来计算LBP特征直方图。下面是计算LBP特征直方图的代码:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg', 0)
# 计算LBP特征
radius = 1
n_points = 8 * radius
lbp = cv2.equalizeHist(cv2.LBP(img, n_points, radius, cv2.LBP_UNIFORM))
# 设置cell大小为32x32
cell_size = (32, 32)
h, w = img.shape
n_cells_x = w // cell_size[0]
n_cells_y = h // cell_size[1]
# 计算每个cell的LBP特征直方图
histogram = np.zeros((n_cells_x, n_cells_y, 256), dtype=np.float32)
for i in range(n_cells_x):
for j in range(n_cells_y):
cell = lbp[j * cell_size[1]:(j + 1) * cell_size[1], i * cell_size[0]:(i + 1) * cell_size[0]]
hist = cv2.spatial_histogram(cell, 256, (0, 255), True)
histogram[i, j, :] = hist
# 显示LBP图谱
cv2.imshow('LBP', lbp)
# 显示LBP特征直方图
hist_img = np.zeros((n_cells_y * cell_size[1], n_cells_x * cell_size[0]), dtype=np.uint8)
for i in range(n_cells_x):
for j in range(n_cells_y):
hist = histogram[i, j, :]
hist /= np.sum(hist)
hist *= 255
hist_img[j * cell_size[1]:(j + 1) * cell_size[1], i * cell_size[0]:(i + 1) * cell_size[0]] = hist.astype(np.uint8)
cv2.imshow('LBP histogram', hist_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在上述代码中,首先读取图像并计算LBP特征。然后将图像分割成大小为32x32的cell,计算每个cell的LBP特征直方图,最后将LBP图谱和LBP特征直方图显示出来。注意,在计算LBP特征直方图时,需要使用`cv2.spatial_histogram()`函数,并且需要将直方图归一化到0~255范围内。
阅读全文