zoukankan      html  css  js  c++  java
  • PCA降维demo

    PCA(Principal Components Analysis)主成分分析法是一种常用的减小数据维度的算法。

    能力有限在这里就不做过多的数学分析了,具体原理可参见http://ufldl.stanford.edu/tutorial/unsupervised/PCAWhitening/ 以及更具体的CS229

    这里结合网上代码做个简单的示例

    static Mat formatImagesForPCA(const vector<Mat> &data)
    {
        Mat dst(static_cast<int>(data.size()), data[0].rows*data[0].cols, CV_32F);
        for (unsigned int i = 0; i < data.size(); i++)
        {
            Mat image_row = data[i].clone().reshape(1, 1);
            Mat row_i = dst.row(i);
            image_row.convertTo(row_i, CV_32F);
        }
        return dst;
    }
    
    static Mat toGrayscale(InputArray _src) {
        Mat src = _src.getMat();
        // only allow one channel
        if (src.channels() != 1) {
            CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
        }
        // create and return normalized image
        Mat dst;
        cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
        return dst;
    }
    
    void main(int argc, char** argv)
    {
        Mat src1 = imread("F:\cha_classification\train\c0.jpg");
        Mat src2 = imread("F:\cha_classification\train\c1.jpg");
        Mat src3 = imread("F:\cha_classification\train\c2.jpg");
    
        Mat g1, g2, g3;
        vector<Mat> gray;
        
        cvtColor(src1, g1, CV_RGB2GRAY);
        gray.push_back(g1);
        cvtColor(src2, g2, CV_RGB2GRAY);
        gray.push_back(g2);
        cvtColor(src3, g3, CV_RGB2GRAY);
        gray.push_back(g3);
    
        imshow("g1", g1);
        //waitKey(0);
    
        Mat data = formatImagesForPCA(gray);//data矩阵大小为:3*(18*18)//  cout << pcaSet << endl;  
        PCA pca(data, cv::Mat(), CV_PCA_DATA_AS_ROW, 0.99);
        //PCA pca(data, Mat(), 200 );
        cout << pca.eigenvalues << endl;
        cout << endl;
        //cout << pca.eigenvectors << endl;
    
    
        // 将PCA降维的效果用第一张图片来显示出来。
        Mat point = pca.project(data.row(0)); // project into the eigenspace, thus the image becomes a "point"
        Mat reconstruction = pca.backProject(point); // re-create the image from the "point"
        reconstruction = reconstruction.reshape(gray[0].channels(), gray[0].rows); // reshape from a row vector into image shape
        reconstruction = toGrayscale(reconstruction); // re-scale for displaying purposes
        namedWindow("r", WINDOW_NORMAL);
        imshow("r", reconstruction);
    
        waitKey(0);
        //return 0;
    }

        

     

    简单的读取三张汉字的图片转换为灰度图像后将图片转为行的形式储存。利用opencv中pca进行分析后取

    显示第一张图只降了一个维度看起来跟原图一样

  • 相关阅读:
    Swift开发第六篇——操作运算符也可以重载& func 的参数修饰
    Swift开发第五篇——四个知识点(Struct Mutable方法&Tuple&autoclosure&Optional Chain)
    Swift开发第四篇——柯里化
    Swift开发第三篇——Playground
    Swift开发第一篇——异常处理及断言
    在Linux CentOS 6.5 (Final)上安装git-1.9.0
    如何有效地配置基于Spring的应用系统
    关于URL编码的问题
    如何优化pom依赖项?
    启动Tomcat的几种方式
  • 原文地址:https://www.cnblogs.com/klitech/p/6890323.html
Copyright © 2011-2022 走看看