当前位置: 首页 > news >正文

奇异值分解(SVD)的应用——图像压缩

SVD方法是模型降阶的一类重要方法,本征正交分解(POD)和平衡截断(BT)都属于SVD类方法。

要想深入了解模型降阶技术,我们可以先从SVD的应用入手,做一个直观的了解。

1. SVD的定义和分类

我们想寻找一个A的逼近:Ak,使得rank(Ak) = k < n,且|A - Ak|最小。

下面的定理(也称为Schmidt-Mirsky, Eckart-Young定理)说明矩阵A的低秩逼近可以用SVD实现:

2. SVD在图像压缩中的应用

原始图片, rank=720:

绘制其R,G,B的奇异值:

压缩图片,rank=144:

压缩图片,rank=72:

代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as imageA = image.imread("svd-image-compression-img.jpg")# Each pixel (typically) consists of 3 bytes — for the red, green and blue components of the color, respectively. 
# So, if we want to efficiently store the image, we need to somehow efficiently encode 3 matrices R, G and B 
# for each color component, respectively.
# We can extract the 3 color component matrices as briefly mentioned above as follows:
# 0xff代表十进制数值255
R = A[:,:,0] / 0xff
G = A[:,:,1] / 0xff
B = A[:,:,2] / 0xff# Now, we compute the SVD decomposition:
R_U, R_S, R_VT = np.linalg.svd(R)
G_U, G_S, G_VT = np.linalg.svd(G)
B_U, B_S, B_VT = np.linalg.svd(B)# polt the singular values
xaxis = np.arange(0, len(R_S))
plt.plot(xaxis, R_S, label='R_S')
plt.plot(xaxis, G_S, label='G_S')
plt.plot(xaxis, B_S, label='B_S')
plt.legend()relative_rank = 0.1
max_rank = int(relative_rank * min(R.shape[0], R.shape[1]))
print("max rank = %d" % max_rank)  # 144def read_as_compressed(U, S, VT, k):Ak = np.zeros((U.shape[0], VT.shape[1]))for i in range(k):U_i = U[:,[i]]VT_i = np.array([VT[i]])Ak += S[i] * (U_i @ VT_i)return Ak## Actually, it is easier and more efficient to perform the same operation 
## with a lower-rank matrix multiplication.
# def read_as_compressed(U, S, VT, k):
#     return (U[:,:k] @ np.diag(S[:k])) @ VT[:k]R_compressed = read_as_compressed(R_U, R_S, R_VT, max_rank)
G_compressed = read_as_compressed(G_U, G_S, G_VT, max_rank)
B_compressed = read_as_compressed(B_U, B_S, B_VT, max_rank)compressed_float = np.dstack((R_compressed, G_compressed, B_compressed))
compressed = (np.minimum(compressed_float, 1.0) * 0xff).astype(np.uint8)# Plot
plt.figure()
plt.imshow(A)plt.figure()
plt.imshow(compressed)image.imsave("compressed.jpg", compressed)

参考资料:

[A.C. Antoulas 2001] Approximation of large-scale dynamical systems: An overview
[潘建瑜] 矩阵计算_讲义 
Compressing images with singular value decomposition (SVD) | ZeroBone
http://www.lryc.cn/news/301730.html

相关文章:

  • RTDETR改进系列指南
  • 类和结构体的区别
  • 利用Excel模拟投币试验
  • WebService接口测试
  • 语音唤醒——
  • typeScript 类型推论
  • JavaScript 设计模式之代理模式
  • JavaScript 对象判断
  • Android下SF合成流程重学习之onMessageInvalidate
  • 基于SpringBoot+WebSocket+Spring Task的前后端分离外卖项目-订单管理(十七)
  • 【Java多线程进阶】JUC常见类以及CAS机制
  • Python算法100例-1.7 最佳存款方案
  • ADO世界之FIRST
  • 【COMP337 LEC 5-6】
  • 力扣72. 编辑距离(动态规划)
  • linux tree命令找不到:如何使用Linux Tree命令查看文件系统结构
  • OJ_最大逆序差
  • MyBatis-Plus 实体类里写正则让字段phone限制为手机格式
  • K8S之运用污点、容忍度设置Pod的调度约束
  • Sora爆火,普通人的10个赚钱机会
  • 【C++】C++入门—初识构造函数 , 析构函数,拷贝构造函数,赋值运算符重载
  • 沁恒CH32V30X学习笔记04--外部中断
  • 基础IO[三]
  • Leetcode 392 判断子序列
  • 基于微信小程序的校园跑腿系统的研究与实现,附源码
  • VTK Python PyQt 监听键盘 控制 Actor 移动 变色
  • 力扣 第 124 场双周赛 解题报告 | 珂学家 | 非常规区间合并
  • 2024年华为OD机试真题-生成哈夫曼树-Java-OD统一考试(C卷)
  • 【实战】二、Jest难点进阶(二) —— 前端要学的测试课 从Jest入门到TDD BDD双实战(六)
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置