opencvSVD奇异值分解

资料:https://blog.csdn.net/u012198575/article/details/99548136  

实例一:压缩图像

#include<opencv2/opencv.hpp>
#include<iostream>



int main(int argc, char** argv) {

    cv::Mat src = cv::imread("D:/bb/tu/lm.jpg", 0);
    //src是m行n列

    cv::Mat result;
    cv::Mat tempt;
    cv::Mat U, S, V;
    src.convertTo(tempt, CV_32FC1);
    cv::SVD::compute(tempt, S, U, V);//SVD分解
    // S是n行1列(CV_32F),U是m行n列(CV_32F),V是n行n列(CV_32F)
    
    cv::Mat s = cv::Mat::zeros(cv::Size(S.rows, S.rows), CV_32FC1);
    double theratio = 0.1;//压缩比例--数值越小,压缩越厉害
    int len = theratio * S.rows;
    for (int i = 0; i < len; ++i) s.ptr<float>(i)[i] = S.ptr<float>(i)[0];

    result = U * s * V; //压缩后还原

    cv::Mat outputImg;  //压缩后的图像
    result.convertTo(outputImg, CV_8UC1);
        
    cv::imshow("原图", src);
    cv::imshow("压缩后", outputImg);

    cv::waitKey(0);
    return 0;
}

原文地址:https://www.cnblogs.com/liming19680104/p/15599337.html