import matplotlib.pyplot as plt from PIL import Image # 读取图像 image = np.array(Image.open('0.jpg')) # 将图像数据归一化到 [0, 1] 范围内 image_norm = image.astype('float32') / 255.0 # 压缩图像 compressed_image = compress_image(image_norm, n_clusters=16) # 显示压缩前后的图像 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) ax[0].imshow(image_norm) ax[0].set_title('Original Image') ax[1].imshow(compressed_image) ax[1].set_title('Compressed Image') plt.show()加上计算PSNR的代码
时间: 2024-02-16 13:59:51 浏览: 20
可以使用以下代码计算压缩后的图像与原始图像之间的 PSNR 值:
```
import numpy as np
import math
# 计算 PSNR
def psnr(img1, img2):
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return float('inf')
return 20 * math.log10(1.0 / math.sqrt(mse))
# 计算压缩前后图像的 PSNR
psnr_value = psnr(image_norm, compressed_image)
print("PSNR: {:.2f} dB".format(psnr_value))
```
这将在显示压缩前后的图像后,输出压缩前后图像的 PSNR 值。
相关问题
将这个代码import matplotlib.pyplot as plt from PIL import Image # 读取图像 image = np.array(Image.open('image.jpg')) # 压缩图像 compressed_image = compress_image(image, n_clusters=16) # 显示压缩前后的图像 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) ax[0].imshow(image) ax[0].set_title('Original Image') ax[1].imshow(compressed_image) ax[1].set_title('Compressed Image') plt.show()显示的图像保存为图像文件
可以使用以下代码将显示的图像保存为图像文件:
```
from PIL import Image
# 将图像数组转换为PIL图像对象
compressed_image = Image.fromarray(compressed_image)
# 保存图像文件
compressed_image.save('compressed_image.jpg')
```
这将把压缩后的图像保存为名为 "compressed_image.jpg" 的图像文件。
import numpy as np from sklearn.cluster import KMeans def compress_image(image, n_clusters): # 转换为一维向量 data = image.reshape(-1, 3) # 用 KMeans 算法将像素点聚类到 n_clusters 个簇中 kmeans = KMeans(n_clusters=5, random_state=0).fit(data) # 将每个像素点替换为所属簇的中心像素值 compressed_data = np.array([kmeans.cluster_centers_[label] for label in kmeans.labels_]) # 将压缩后的一维向量转换回原图像的形状 compressed_image = compressed_data.reshape(image.shape) return compressed_imageimport matplotlib.pyplot as plt from PIL import Image # 读取图像 image = np.array(Image.open('0.jpg')) # 将图像数据归一化到 [0, 1] 范围内 image = image.astype('float32') / 255.0 # 压缩图像 compressed_image = compress_image(image_norm, n_clusters=16) # 显示压缩前后的图像 fig, ax = plt.subplots(1, 2, figsize=(10, 5)) ax[0].imshow(image) ax[0].set_title('Original Image') ax[1].imshow(compressed_image) ax[1].set_title('Compressed Image') plt.show()加上计算压缩率的功能
可以在 compress_image 函数中添加计算压缩率的功能,具体实现如下:
```
import numpy as np
from sklearn.cluster import KMeans
def compress_image(image, n_clusters):
# 转换为一维向量
data = image.reshape(-1, 3)
# 用 KMeans 算法将像素点聚类到 n_clusters 个簇中
kmeans = KMeans(n_clusters=n_clusters, random_state=0).fit(data)
# 将每个像素点替换为所属簇的中心像素值
compressed_data = np.array([kmeans.cluster_centers_[label] for label in kmeans.labels_])
# 将压缩后的一维向量转换回原图像的形状
compressed_image = compressed_data.reshape(image.shape)
# 计算压缩率
compressed_size = compressed_data.nbytes
original_size = image.nbytes
compression_ratio = compressed_size / original_size
return compressed_image, compression_ratio
import matplotlib.pyplot as plt
from PIL import Image
# 读取图像
image = np.array(Image.open('0.jpg'))
# 将图像数据归一化到 [0, 1] 范围内
image = image.astype('float32') / 255.0
# 压缩图像
n_clusters = 5
compressed_image, compression_ratio = compress_image(image, n_clusters)
# 显示压缩前后的图像
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(image)
ax[0].set_title('Original Image')
ax[1].imshow(compressed_image)
ax[1].set_title('Compressed Image')
# 显示压缩率
plt.figtext(0.5, 0.9, f'Compression Ratio: {compression_ratio:.2%}', ha='center', fontsize=12)
plt.show()
```
在上述代码中,计算压缩率的代码为:
```
compressed_size = compressed_data.nbytes
original_size = image.nbytes
compression_ratio = compressed_size / original_size
```
其中,`compressed_data.nbytes` 表示压缩后的图像数据的字节数,`image.nbytes` 表示原始图像数据的字节数,压缩率即为两者之比。