编写程序实现矩阵的SVD分解,编写一个综合程序,实现基于SVD分解的图片压缩、降噪。并根据图像质量评价指标PSNR、SSIM评估压缩、降噪结果。画出PSNR、SSIM随奇异值保留数量的变化关系曲线,以及其他结果展示
时间: 2024-04-29 17:25:43 浏览: 142
首先,我们需要实现矩阵的SVD分解。下面是一个示例代码:
```python
import numpy as np
def svd(matrix):
U, s, V = np.linalg.svd(matrix, full_matrices=True)
return U, s, V
# 示例代码
matrix = np.array([[1, 2], [3, 4], [5, 6]])
U, s, V = svd(matrix)
print(U)
print(s)
print(V)
```
接下来,我们可以使用SVD分解来实现图片压缩和降噪。下面是一个示例代码:
```python
import cv2
import numpy as np
def compress_image(image, k):
U, s, V = svd(image)
compressed_s = np.zeros_like(s)
compressed_s[:k] = s[:k]
compressed_image = U.dot(np.diag(compressed_s)).dot(V)
return compressed_image.astype(np.uint8)
def denoise_image(image, k):
U, s, V = svd(image)
denoised_s = np.zeros_like(s)
denoised_s[k:] = s[k:]
denoised_image = U.dot(np.diag(denoised_s)).dot(V)
return denoised_image.astype(np.uint8)
# 示例代码
image = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
compressed_image = compress_image(image, 100)
denoised_image = denoise_image(image, 100)
cv2.imwrite('compressed.png', compressed_image)
cv2.imwrite('denoised.png', denoised_image)
```
最后,我们可以使用PSNR和SSIM评估压缩和降噪结果。下面是一个示例代码:
```python
import cv2
import numpy as np
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
def evaluate(image1, image2):
psnr = peak_signal_noise_ratio(image1, image2)
ssim = structural_similarity(image1, image2)
return psnr, ssim
# 示例代码
original_image = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
compressed_image = cv2.imread('compressed.png', cv2.IMREAD_GRAYSCALE)
denoised_image = cv2.imread('denoised.png', cv2.IMREAD_GRAYSCALE)
psnr1, ssim1 = evaluate(original_image, compressed_image)
psnr2, ssim2 = evaluate(original_image, denoised_image)
print('Compressed PSNR:', psnr1, 'SSIM:', ssim1)
print('Denoised PSNR:', psnr2, 'SSIM:', ssim2)
```
最后,我们可以画出PSNR和SSIM随奇异值保留数量的变化关系曲线。下面是一个示例代码:
```python
import cv2
import numpy as np
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
import matplotlib.pyplot as plt
def evaluate(image1, image2):
psnr = peak_signal_noise_ratio(image1, image2)
ssim = structural_similarity(image1, image2)
return psnr, ssim
def compress_image(image, k):
U, s, V = svd(image)
compressed_s = np.zeros_like(s)
compressed_s[:k] = s[:k]
compressed_image = U.dot(np.diag(compressed_s)).dot(V)
return compressed_image.astype(np.uint8)
def denoise_image(image, k):
U, s, V = svd(image)
denoised_s = np.zeros_like(s)
denoised_s[k:] = s[k:]
denoised_image = U.dot(np.diag(denoised_s)).dot(V)
return denoised_image.astype(np.uint8)
# 示例代码
original_image = cv2.imread('lena.png', cv2.IMREAD_GRAYSCALE)
psnrs1, ssims1 = [], []
psnrs2, ssims2 = [], []
for k in range(1, 500, 10):
compressed_image = compress_image(original_image, k)
denoised_image = denoise_image(original_image, k)
psnr1, ssim1 = evaluate(original_image, compressed_image)
psnr2, ssim2 = evaluate(original_image, denoised_image)
psnrs1.append(psnr1)
ssims1.append(ssim1)
psnrs2.append(psnr2)
ssims2.append(ssim2)
plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.plot(psnrs1, label='Compressed')
plt.plot(psnrs2, label='Denoised')
plt.title('PSNR')
plt.legend()
plt.subplot(122)
plt.plot(ssims1, label='Compressed')
plt.plot(ssims2, label='Denoised')
plt.title('SSIM')
plt.legend()
plt.show()
```
这里我们使用了matplotlib库来画出随奇异值保留数量变化的PSNR和SSIM曲线。
阅读全文