psnr = batch_PSNR(Out, ISource, 1.) print('去噪噪PSNR', psnr)怎么结合到一起
时间: 2024-01-16 15:02:52 浏览: 85
你可以这样将它们结合起来:
```python
import torch
# 假设 Out 和 ISource 都是 torch.Tensor 类型
mse = torch.mean(torch.pow(Out - ISource, 2))
psnr = 10 * torch.log10(1.0 / mse)
print('去噪噪PSNR', psnr)
```
这里使用了 PyTorch 内置的函数计算MSE(均方误差),然后用它计算PSNR。最后打印出PSNR的值。你可以将这段代码添加到你的程序中,用于计算PSNR。
相关问题
from bm3d import bm3d_rgb from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr from PIL import Image import argparse import os import torch import numpy as np from torchvision.utils import save_image def main(): imagename = './test_image1/(1271).jpg' save_dir = 'test_result' save_path = 'noise' y = np.array(Image.open(imagename)) / 255 noise_type = 'g3' noise_var = 0.02 seed = 0 noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape) z = np.atleast_3d(y) + np.atleast_3d(noise) y_est = bm3d_rgb(z, psd) psnr = get_psnr(y, y_est) print("PSNR:", psnr) y_est = np.minimum(np.maximum(y_est, 0), 1) z_rang = np.minimum(np.maximum(z, 0), 1) z_rang = torch.from_numpy(np.transpose(z_rang, (2, 0, 1))).float() y_est = torch.from_numpy(np.transpose(y_est, (2, 0, 1))).float() denoise_img_path = os.path.join(save_dir, 'denoised.jpg') save_image(y_est, denoise_img_path) noise_img_path = os.path.join(save_path, 'noise.jpg') save_image(z_rang, noise_img_path) if __name__ == '__main__': main()改为对灰度图处理
可以将代码中的`bm3d_rgb`函数改为`bm3d`函数,同时将读入图像的方式改为读入灰度图像即可。具体代码如下:
```
from bm3d import bm3d
from experiment_funcs1 import get_experiment_noise, get_psnr, get_cropped_psnr
from PIL import Image
import argparse
import os
import torch
import numpy as np
from torchvision.utils import save_image
def main():
imagename = './test_image1/(1271).jpg'
save_dir = 'test_result'
save_path = 'noise'
y = np.array(Image.open(imagename).convert('L')) / 255
noise_type = 'g3'
noise_var = 0.02
seed = 0
noise, psd, kernel = get_experiment_noise(noise_type, noise_var, seed, y.shape)
z = y + noise
y_est = bm3d(z, psd)
psnr = get_psnr(y, y_est)
print("PSNR:", psnr)
y_est = np.minimum(np.maximum(y_est, 0), 1)
z_rang = np.minimum(np.maximum(z, 0), 1)
z_rang = torch.from_numpy(z_rang).unsqueeze(0).float()
y_est = torch.from_numpy(y_est).unsqueeze(0).float()
denoise_img_path = os.path.join(save_dir, 'denoised.jpg')
save_image(y_est, denoise_img_path)
noise_img_path = os.path.join(save_path, 'noise.jpg')
save_image(z_rang, noise_img_path)
if __name__ == '__main__':
main()
```
from PIL import Image import numpy as np import io # 读取原始图像和压缩后图像 original_img = Image.open('test.jpg') compressed_img = Image.open('test_compressed.jpg') # 将图像转换为 NumPy 数组 original_img_arr = np.array(original_img) compressed_img_arr = np.array(compressed_img) # 计算原始图像大小 original_size = original_img_arr.nbytes # 计算压缩后图像大小 compressed_size = compressed_img_arr.nbytes # 计算压缩率 compression_ratio = compressed_size / original_size # 计算峰值信噪比(PSNR) mse = np.mean((original_img_arr - compressed_img_arr) ** 2) psnr = 10 * np.log10(255**2 / mse) # 计算结构相似性指数(SSIM) from skimage.metrics import structural_similarity as ssim ssim_score = ssim(original_img_arr, compressed_img_arr, multichannel=True) # 计算峰值信噪比改进比(PSNR-HVS) from skimage.metrics import peak_signal_noise_ratio as psnr_hvs psnr_hvs_score = psnr_hvs(original_img_arr, compressed_img_arr, data_range=original_img_arr.max()) # 计算多样性信噪比(MS-SSIM) from skimage.metrics import multi_scale_ssim as ms_ssim ms_ssim_score = ms_ssim(original_img_arr, compressed_img_arr, data_range=original_img_arr.max(), win_size=11) # 计算复杂度压缩比(CPC) cpc = psnr / compression_ratio # 输出七种压缩率 print(f"Compression ratio: {compression_ratio:.4f}") print(f"Peak Signal-to-Noise Ratio (PSNR): {psnr:.2f}") print(f"Structural Similarity Index (SSIM): {ssim_score:.4f}") print(f"Peak Signal-to-Noise Ratio - HVS (PSNR-HVS): {psnr_hvs_score:.2f}") print(f"Multi-Scale Structural Similarity (MS-SSIM): {ms_ssim_score:.4f}") print(f"Complexity-Compression Ratio (CPC): {cpc:.2f}") print(f"Original size: {original_size:,}") print(f"Compressed size: {compressed_size:,}")ValueError: operands could not be broadcast together with shapes (417,556,3) (418,558,3)
这个错误通常是由于两个数组的形状不匹配导致的。在您的情况下,您正在尝试将形状为(417,556,3)和(418,558,3)的两个数组进行广播操作,但它们的形状不兼容。
根据您提供的代码,错误可能出现在以下几行中:
```python
original_img_arr = np.array(original_img)
compressed_img_arr = np.array(compressed_img)
```
这两行代码将 PIL.Image 对象转换为 NumPy 数组。如果两个图像的大小不同,则它们的数组形状也不同,这可能导致广播错误。
您可以尝试查看这两张图像的大小,看看它们是否匹配。或者,您可以尝试调整图像的大小,使它们具有相同的形状,然后再将它们转换为 NumPy 数组。
例如,您可以使用 PIL 库中的 resize() 函数来调整图像的大小:
```python
original_img = original_img.resize((width, height))
compressed_img = compressed_img.resize((width, height))
```
其中,width 和 height 是您想要的新图像的宽度和高度。然后,您可以将这两张图像转换为 NumPy 数组,并继续计算 PSNR、SSIM 等指标。
阅读全文