对图像均值滤波并计算PSNR,SSIM的 python代码
时间: 2023-09-16 22:09:12 浏览: 158
以下是Python中对图像进行均值滤波并计算PSNR和SSIM的示例代码:
```
import cv2
import numpy as np
from skimage.measure import compare_ssim
# 读取图像
img = cv2.imread('image.jpg')
# 进行均值滤波
kernel = np.ones((5, 5), np.float32) / 25
img_smooth = cv2.filter2D(img, -1, kernel)
# 计算PSNR
mse = np.mean((img - img_smooth) ** 2)
psnr = 10 * np.log10(255 ** 2 / mse)
# 计算SSIM
ssim = compare_ssim(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
cv2.cvtColor(img_smooth, cv2.COLOR_BGR2GRAY),
gaussian_weights=True,
sigma=1.5,
use_sample_covariance=False)
# 输出结果
print("PSNR:", psnr)
print("SSIM:", ssim)
```
注意,这里的输入图像文件名应该替换为你自己的图像文件名。
相关问题
给我评价两幅图像的ssim以及PSNR的Python代码
以下是给出两幅图像的 SSIM 和 PSNR 的 Python 代码:
```python
import cv2
import numpy as np
def ssim(image1, image2):
"""Calculate the Structural Similarity Index (SSIM) between two images."""
# Set constants
C1 = (0.01 * 255) ** 2
C2 = (0.03 * 255) ** 2
# Convert the images to grayscale
img1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
img2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Calculate means and variances
mu1 = cv2.GaussianBlur(img1, (11, 11), 1.5)
mu2 = cv2.GaussianBlur(img2, (11, 11), 1.5)
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.GaussianBlur(img1 ** 2, (11, 11), 1.5) - mu1_sq
sigma2_sq = cv2.GaussianBlur(img2 ** 2, (11, 11), 1.5) - mu2_sq
sigma12 = cv2.GaussianBlur(img1 * img2, (11, 11), 1.5) - mu1_mu2
# Calculate SSIM
num = (2 * mu1_mu2 + C1) * (2 * sigma12 + C2)
den = (mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)
ssim = np.mean(num / den)
return ssim
def psnr(image1, image2):
"""Calculate the Peak Signal-to-Noise Ratio (PSNR) between two images."""
mse = np.mean((image1 - image2) ** 2)
if mse == 0:
return float('inf')
else:
max_pixel = 255.0
psnr = 20 * np.log10(max_pixel / np.sqrt(mse))
return psnr
```
其中,`image1` 和 `image2` 分别为两幅图像的 numpy 数组。使用方法如下:
```python
# Load the two images
image1 = cv2.imread('image1.jpg')
image2 = cv2.imread('image2.jpg')
# Calculate SSIM and PSNR
ssim_value = ssim(image1, image2)
psnr_value = psnr(image1, image2)
# Print the results
print('SSIM:', ssim_value)
print('PSNR:', psnr_value)
```
请注意,这些度量标准仅作为图像质量的参考,它们并不能完全描述图像的质量。因此,建议在实际应用中结合其他度量标准进行分析。
请用python代码实现计算psnr和ssim
好的,下面是使用Python实现计算PSNR和SSIM的代码:
```python
import cv2
import numpy as np
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return float('inf')
PIXEL_MAX = 255.0
return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))
def ssim(img1, img2):
C1 = (0.01 * 255) ** 2
C2 = (0.03 * 255) ** 2
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1 ** 2
mu2_sq = mu2 ** 2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1 ** 2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2 ** 2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))
return np.mean(ssim_map)
# 读取图片
img1 = cv2.imread('img1.png')
img2 = cv2.imread('img2.png')
# 计算PSNR和SSIM
print('PSNR:', psnr(img1, img2))
print('SSIM:', ssim(img1, img2))
```
其中,`psnr()`函数计算PSNR,`ssim()`函数计算SSIM。需要注意的是,需要安装OpenCV库才能运行该代码。