计算图像的LBP图以及LBP直方图
时间: 2023-09-15 12:17:46 浏览: 119
LBP(Local Binary Pattern)是一种用于图像分析的局部特征描述符。它可以对图像进行纹理分析,用于图像分类、人脸识别等领域。以下是计算LBP图和LBP直方图的步骤:
1. 计算LBP图:
对于给定的图像,选择一个像素点和以该点为中心的8个像素点。将这9个像素点的灰度值与中心点的灰度值比较,如果周边像素点的灰度值大于等于中心点的灰度值,就将该像素点对应的二进制位设为1,否则设为0。将这8个二进制位串连接起来,得到该像素点的LBP值。将这个过程对图像中的所有像素点执行一遍,得到LBP图。
2. 计算LBP直方图:
对于LBP图中的每一个像素点,它的LBP值都属于256种可能的取值之一(8个二进制位的组合方式)。因此,我们可以将LBP图中出现的每个LBP值都统计一下,形成一个256维的向量,即LBP直方图。直方图中每一维表示该LBP值在图像中出现的次数。
以上就是计算LBP图和LBP直方图的步骤。需要注意的是,LBP算法对图像的灰度不敏感,因此在计算LBP图之前通常需要将图像转换为灰度图。
相关问题
对于同一幅图像,生成另外3幅不同亮度的图像(共4幅图像) 2. 获得4幅图像的LBP图像,及其LBP直方图 3. 计算另外3幅图像的LBP直方图与原图像LBP直方图的误差值(MSE)
好的,以下是我使用Python代码实现上述要求的过程:
```python
import cv2
import numpy as np
from matplotlib import pyplot as plt
# 载入原始图像
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 生成3幅不同亮度的图像
img1 = cv2.add(img, 50)
img2 = cv2.add(img, 100)
img3 = cv2.add(img, 150)
# 生成4幅图像的LBP图像和LBP直方图
images = [img, img1, img2, img3]
histograms = []
for i, image in enumerate(images):
# 计算LBP图像
lbp = cv2.LBP(image, 8, 2, cv2.LBP_UNIFORM)
# 计算LBP直方图
hist, _ = np.histogram(lbp.ravel(), bins=256, range=(0, 255))
# 将LBP直方图归一化
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
# 将LBP直方图添加到列表中
histograms.append(hist)
# 显示LBP图像和LBP直方图
plt.subplot(2, 4, i+1), plt.imshow(lbp, cmap='gray')
plt.title('LBP Image %d' % (i+1))
plt.subplot(2, 4, i+5), plt.plot(hist)
plt.title('LBP Histogram %d' % (i+1))
# 计算误差值(MSE)
mse1 = np.mean((histograms[0] - histograms[1])**2)
mse2 = np.mean((histograms[0] - histograms[2])**2)
mse3 = np.mean((histograms[0] - histograms[3])**2)
print('MSE 1: %f' % mse1)
print('MSE 2: %f' % mse2)
print('MSE 3: %f' % mse3)
plt.show()
```
请注意,此处我使用了Python中的matplotlib库来显示生成的LBP图像和LBP直方图。如果您没有安装该库,请先使用以下命令安装:
```bash
pip install matplotlib
```
另外,我假设您已经将原始图像保存在名为“image.jpg”的文件中,并将其放置在当前工作目录中。如果您的文件名或路径不同,请相应地更改代码中的文件名和路径。
运行上述代码后,您将会看到4幅图像的LBP图像和LBP直方图,以及3幅图像的LBP直方图与原始图像LBP直方图的MSE值。
用python写计算图像LBP特征的代码
以下是使用Python计算图像LBP特征的代码:
```python
import cv2
import numpy as np
def get_lbp_feature(img):
# 将图像转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 初始化LBP特征图
lbp = np.zeros_like(gray)
# 计算LBP特征
for i in range(1, gray.shape[0]-1):
for j in range(1, gray.shape[1]-1):
center = gray[i, j]
code = 0
code |= (gray[i-1, j-1] > center) << 7
code |= (gray[i-1, j] > center) << 6
code |= (gray[i-1, j+1] > center) << 5
code |= (gray[i, j+1] > center) << 4
code |= (gray[i+1, j+1] > center) << 3
code |= (gray[i+1, j] > center) << 2
code |= (gray[i+1, j-1] > center) << 1
code |= (gray[i, j-1] > center) << 0
lbp[i-1, j-1] = code
# 统计直方图
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, 257), range=(0, 256))
# 对直方图进行归一化处理
hist = hist.astype("float")
hist /= (hist.sum() + 1e-7)
# 返回LBP特征向量
return hist
if __name__ == '__main__':
# 读取图像
img = cv2.imread('test.jpg')
# 计算LBP特征
feature = get_lbp_feature(img)
# 输出LBP特征向量
print(feature)
```
注:该代码计算的是8邻域的LBP特征,其中`test.jpg`为待计算LBP特征的图像文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)