首页 > 代码库 > PCA降维demo

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进行分析后取技术分享

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

 

PCA降维demo