【原】特征/SVD分解(图像压缩)/PCA降维简介

说明:实际上EVD(特征分解)是SVD的一种特殊情况;逆是伪逆的特殊情况?,这在最小二乘当中有应用。

在“8点法”求解本质矩阵当中会有SVD分解,在3D到3D空间转换中,算法icp有SVD解法。SVD作为一种分解矩阵的方法,

有着广泛应用。

一、特征分解(手写word截图)

1 %% Matlab验证代码
2 a=[1 2 3;2 1 3;3 3 6]
3 [x,y]=eig(a) %% x矩阵每一列代表 lamda123 对应的特征向量
4 diag(y) %% y矩阵的对角元素是对应特征值lamda123

二、 SVD分解和图像压缩

数学概念;参考:  https://www.cnblogs.com/xugenpeng/p/4839336.html?from=singlemessage&isappinstalled=0

                 

1 A = [1 2; 0 0; 0 0]
2 [U, S, V] = svd(A);

和手动计算结果一致。

图像压缩实例

  1 #include<iostream>
  2 using namespace std;
  3 
  4 #include<opencv2/opencv.hpp>
  5 using namespace cv;
  6 
  7 void printMat(Mat& matrix)
  8 {
  9     for (int j = 0; j < matrix.rows; j++)
 10     {
 11         for (int i = 0; i < matrix.cols; i++)
 12         {
 13             cout << matrix.ptr<double>(j)[i] << ", ";
 14         }
 15         cout << endl;
 16     }
 17     cout << endl;
 18 }
 19 
 20 // input:  image : ...
 21 //         radio : 压缩比率
 22 // ouput:  Mat : 压缩后的灰度图
 23 Mat compressJPG(Mat image, double radio)
 24 {
 25     SVD svd_1st(image, SVD::MODIFY_A);
 26     Mat W_ = Mat::zeros(svd_1st.w.rows, svd_1st.w.rows, CV_64F);
 27 
 28     // 压缩比例:radio = m*n/(r*(m+n+1))
 29     // r = m*n / (radio*(m+n+1))
 30     double m = double(image.rows);
 31     double n = double(image.cols);
 32     double r = m * n/(radio*(m + n + 1));
 33     int r_ = int(r);
 34     if (r_  >= svd_1st.w.rows)
 35     {
 36         cout << "errors in setting radio!" << endl;
 37         return Mat();
 38     }
 39     //for (int i = 0; i < svd_1st.w.rows; i++)
 40     for (int i = 0; i < r_; i++)
 41     {
 42         W_.ptr<double>(i)[i] = svd_1st.w.ptr<double>(i)[0];
 43     }
 44 
 45     Mat image_compressed = svd_1st.u * W_ * svd_1st.vt;
 46     image_compressed.convertTo(image_compressed, CV_8U);
 47     return image_compressed;
 48 }
 49 
 50 int main()
 51 {
 52     // <1> test SVD API of opencv 
 53     Mat A = (Mat_<double>(3, 2) << 1, 2, 0, 0, 0, 0);
 54     cout << "原矩阵 A = " << endl;
 55     printMat(A);
 56     Mat W, U, Vt;
 57     SVD::compute(A,W,U,Vt, SVD::MODIFY_A);
 58 
 59     cout << "奇异值矩阵 W =" << endl;
 60     printMat(W);
 61     cout << "左奇异值矩阵 U =" << endl;
 62     printMat(U); //U是 3X2. rank(U) = 2; to save space, U has's 2 cols.
 63     cout << "右奇异值矩阵(自动转置) Vt =" << endl;
 64     printMat(Vt);
 65 
 66     // <2> recover the matrix 'A'
 67     Mat W_ = Mat::zeros(W.rows, W.rows, CV_64F); // 构建奇异值(方)矩阵,不保留0行 
 68     for (int i = 0; i < W.rows; i++)
 69     {
 70         W_.ptr<double>(i)[i] = W.ptr<double>(i)[0];
 71     }
 72     Mat A_ = U*W_*Vt;
 73     cout << "恢复之后:" << endl;
 74     printMat(A_);
 75 
 76     //*******************************************************************************************************
 77     // 压缩图像例子
 78     // 假设原矩阵 A 是 m x n;奇异值个数为r(也就是rank(A) = r)
 79     // 那么压缩比例:radio = m*n/(r*(m+n+1))
 80     // 分母分别是 r*m : 左奇异矩阵元素个数; r*n : 右奇异矩阵元素个数;  r: 奇异值矩阵元素个数
 81 
 82     Mat image = imread("2.jpg", 0);
 83     imshow("image", image);
 84     image.convertTo(image, CV_64F);
 85 
 86     Mat image_1st = compressJPG(image, 0.6);
 87     if (!image_1st.empty())
 88     {
 89         imshow("radio = 0.6", image_1st);
 90         imwrite("image_1st.jpg", image_1st);
 91     }
 92     
 93     Mat image_2st = compressJPG(image, 1);
 94     if (!image_2st.empty())
 95     {
 96         imshow("radio = 1", image_2st);
 97         imwrite("image_2st.jpg", image_2st);
 98     }
 99 
100     Mat image_3st = compressJPG(image, 3);
101     if (!image_3st.empty())
102     {
103         imshow("radio = 3", image_3st);
104         imwrite("image_3st.jpg", image_3st);
105     }
106     
107     Mat image_4st = compressJPG(image, 5);
108     if (!image_4st.empty())
109     {
110         imshow("radio = 5", image_4st);
111         imwrite("image_4st.jpg", image_4st);
112     }
113 
114     Mat image_5st = compressJPG(image, 7);
115     if (!image_5st.empty())
116     {
117         imshow("radio = 7", image_5st);
118         imwrite("image_5st.jpg", image_5st);
119     }
120 
121     Mat image_6st = compressJPG(image, 9);
122     if (!image_6st.empty())
123     {
124         imshow("radio = 9", image_6st);
125         imwrite("image_6st.jpg", image_6st);
126     }
127 
128     Mat image_7st = compressJPG(image, 20);
129     if (!image_7st.empty())
130     {
131         imshow("radio = 20", image_7st);
132         imwrite("image_7st.jpg", image_7st);
133     }
134 
135     waitKey(0);    
136     return 1;
137 }

 这里比率设置不正确,返回一个提示!

可以看到压缩比率越大,图像失真越明显。

可以看到压缩比率增大,图像体积变小,别小看一张图减小十几kb大小,对于视频网站来讲.......毋庸置疑,开源节流!

楼主不是专门研究图像压缩的,就不深究了,据说微信能将一张由iphone拍的照片(10Mb左右)压缩到几百kb,图像依然很清晰!

三、PCA降维

参考:https://zhuanlan.zhihu.com/p/21580949

给出PCA的一般数学步骤:

顺便给出一道例题:

依据上述步骤,我们来解一道题

投影后如图;

(顺便:如果用 特征值 2/5 对应的特征向量作为矩阵P;

可以看到,如果映射到一维数轴上,是有信息丢失的。你看下图,是主城成分,二维数据映射到一维空间,)


  工程上,数据维度太高,其中又有许多冗余成分;这样处理起来效率不高。这时候,我们可以对数据经行降维。

PCA,即:主成成分分析,回想SVD分解图像压缩实例。我们保留最大的几个奇异值,就可以压缩数据的同时,还能最大化减少信息的损失。同理,通过多种线性变换,我们可以将原始数据降维,往往原始数据对应的协方差矩阵的多个奇异值/特征值中,将最大的几个值所对应的特征向量作为线性变换矩阵,可以起到降维作用。如上图,我们便是将最大特征值 2 对应的特征向量 作为矩阵P(P是什么?看上述PCA计算步骤)。你可以看看参考链接,另外为了更好理解PCA,你可以去看看有关PCA的应用,参考:

https://blog.csdn.net/HLBoy_happy/article/details/77146012

原文地址:https://www.cnblogs.com/winslam/p/9971732.html