对于同一幅图像,生成另外3幅不同亮度的图像(共4幅图像) 2. 获得4幅图像的LBP图像,及其LBP直方图 3. 计算另外3幅图像的LBP直方图与原图像LBP直方图的误差值(MSE)
时间: 2024-02-27 07:56:11 浏览: 19
好的,以下是我使用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值。