当前位置:网站首页>机器学习笔记 - SVD奇异值分解(3) 在图像上应用 SVD
机器学习笔记 - SVD奇异值分解(3) 在图像上应用 SVD
2022-04-21 13:43:00 【bashendixie5】
上一篇我们看到了SVD分解为旋转、缩放、旋转等三个步骤。
https://blog.csdn.net/bashendixie5/article/details/124302156
https://blog.csdn.net/bashendixie5/article/details/124302156 在这个例子中,我们将使用 SVD 从图像中提取更重要的特征。可以直观看到SVD对视觉效果的影响。
让我们首先在 python 中加载图像并将其转换为 Numpy 数组。 然后将其转换为灰度,以使每个像素具有一维。 矩阵的形状对应于填充有强度值的图像的尺寸:每个像素 1 个单元。
from PIL import Image
plt.style.use('classic')
img = Image.open('test_svd.jpg')
# convert image to grayscale
imggray = img.convert('LA')
# convert to numpy array
imgmat = np.array(list(imggray.getdata(band=0)), float)
# Reshape according to orginal image dimensions
imgmat.shape = (imggray.size[1], imggray.size[0])
plt.figure(figsize=(9, 6))
plt.imshow(imgmat, cmap='gray')
plt.show()

我们测试 SVD 对图片的影响! 让我们开始提取左奇异向量、奇异值和右奇异向量:
U, D, V = np.linalg.svd(imgmat)
我们看一下得到数据的形状
原图的形状为
(669, 1000)
U.shape
(669, 669)
D.shape
(669,)
V.shape
(1000, 1000)
D是需要放入对角矩阵的奇异值。 另外V 不需要转置。
奇异向量和奇异值按照与解释的更多方差相对应的第一个进行排序。出于这个原因,仅使用前几个奇异向量和奇异值将提供图像主要元素的重建。
我们可以从U、D、V等重建图像,但是数量可以选择。例如,对于 2 个奇异值,我们将有:

在下面这个例子中,我们从两个奇异值重建了 699 x 1000 像素的图像。
reconstimg = np.matrix(U[:, :2]) * np.diag(D[:2]) * np.matrix(V[:2, :])
plt.imshow(reconstimg, cmap='gray')
plt.show()

下面我们将使用不同数量的奇异值绘制重建。
for i in [5, 10, 15, 20, 30, 50]:
reconstimg = np.matrix(U[:, :i]) * np.diag(D[:i]) * np.matrix(V[:i, :])
plt.imshow(reconstimg, cmap='gray')
title = "n = %s" % i
plt.title(title)
plt.show()
n=5

n=10

......
n=50

版权声明
本文为[bashendixie5]所创,转载请带上原文链接,感谢
https://blog.csdn.net/bashendixie5/article/details/124303530
边栏推荐
- 二叉树创建及其线索化
- Forced to choose an outsourcing company
- Example: use C # NET teaches you to do WeChat official account development (7) -- location message for general message processing.
- Filter string only retains the alphabetic characters in the string (10 points). Please write a function fun. The function is to enter a string, filter the string, only retain the alphabetic characters
- The monomer test uses assert Assert that (expected, matcher)
- 【栈和队列专题】—— 双队列模拟栈
- Stm32cupemx installation
- leetcode:824. Goat Latin [simple string manipulation]
- Could not load dynamic library ‘libcusolver.so.11‘
- idea自动生成单元测类
猜你喜欢

Establishment of binary tree and its cueing

Forced to choose an outsourcing company

Last online class can be "distracted" by AI analysis. Intel's emotional detection AI is on fire

Exercise questions and answers of basic theories and relevant laws and regulations in 2022 supervision engineer examination

前馈神经网络

idea自动生成单元测类

Benchmark cell detection scheme based on violence matching threshold

Open mmlab / mmpose installation and use tutorial

實現隨機標簽,字體大小、顏色隨機顯示

实现随机标签,字体大小、颜色随机显示
随机推荐
运动耳机什么样的舒服、运动健身耳机推荐
Work function of boost ASIO
After the completion of hundreds of millions of yuan of financing, smart bank plans to land urban intelligent driving products in more than 100 cities
A great peripheral driver library! (based on stm32f4)
被迫选择了到了外包公司
哈夫曼編碼
浅析 联邦学习是什么?
Programmers burst out their salary, with a monthly salary of 15000 before tax
Tailwind核心理念——响应式设计
MySQL analysis on how to reduce conflict and improve performance of row lock
Color gradient (columns, rings, etc.)
STM32驱动ST7789V2 tft屏幕
Exercise questions and answers of basic theories and relevant laws and regulations in 2022 supervision engineer examination
Open mmlab / mmpose installation and use tutorial
HCIP之路OSPF拓展配置
Example: use C # NET teaches you to do WeChat official account development (7) -- location message for general message processing.
Maze walking (BFS)
flex项目属性
过滤字符串只保留串中的字母字符 (10 分)请编写一个函数fun,函数的功能是:输入一个字符串,过滤此串,只保留串中的字母字符,并统计新生成串中包含的字母个数。
北京大学ACM Problems 1012:Maya Calendar