Python比较两张图片的亮度、对比度、饱和度、锐度、sigma值、gamma值
时间: 2023-09-06 12:07:48 浏览: 297
要比较两张图片的亮度、对比度、饱和度、锐度、sigma值和gamma值,你可以使用Python中的图像处理库如OpenCV或PIL来实现。以下是使用OpenCV库进行比较的示例代码:
```python
import cv2
import numpy as np
def compare_images(img1, img2):
# 转换图片为Lab颜色空间
lab1 = cv2.cvtColor(img1, cv2.COLOR_BGR2LAB)
lab2 = cv2.cvtColor(img2, cv2.COLOR_BGR2LAB)
# 计算亮度、对比度和饱和度的均值和标准差
l_mean1, l_std1 = cv2.meanStdDev(lab1[:,:,0])
l_mean2, l_std2 = cv2.meanStdDev(lab2[:,:,0])
c_mean1, c_std1 = cv2.meanStdDev(lab1[:,:,1])
c_mean2, c_std2 = cv2.meanStdDev(lab2[:,:,1])
s_mean1, s_std1 = cv2.meanStdDev(lab1[:,:,2])
s_mean2, s_std2 = cv2.meanStdDev(lab2[:,:,2])
# 计算锐度
lap1 = cv2.Laplacian(img1, cv2.CV_64F).var()
lap2 = cv2.Laplacian(img2, cv2.CV_64F).var()
# 计算sigma值
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
sigma1 = np.std(gray1)
sigma2 = np.std(gray2)
# 计算gamma值
gamma1 = np.mean(((img1 / 255) ** 2.2))
gamma2 = np.mean(((img2 / 255) ** 2.2))
# 打印结果
print("亮度均值差异: ", np.abs(l_mean1 - l_mean2))
print("亮度标准差差异: ", np.abs(l_std1 - l_std2))
print("对比度均值差异: ", np.abs(c_mean1 - c_mean2))
print("对比度标准差差异: ", np.abs(c_std1 - c_std2))
print("饱和度均值差异: ", np.abs(s_mean1 - s_mean2))
print("饱和度标准差差异: ", np.abs(s_std1 - s_std2))
print("锐度差异: ", np.abs(lap1 - lap2))
print("Sigma值差异: ", np.abs(sigma1 - sigma2))
print("Gamma值差异: ", np.abs(gamma1 - gamma2))
# 读取图片
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 比较图片
compare_images(img1, img2)
```
请确保将`image1.jpg`和`image2.jpg`替换为你要比较的实际图片路径。该代码将计算并打印出两张图片之间的亮度、对比度、饱和度、锐度、sigma值和gamma值的差异。
阅读全文