1、OpenCV中LDA类的声明

//contrib.hpp

class CV_EXPORTS LDA
{
public:
// Initializes a LDA with num_components (default 0) and specifies how
// samples are aligned (default dataAsRow=true).
LDA(int num_components = 0) :
_num_components(num_components) {}; // Initializes and performs a Discriminant Analysis with Fisher's
// Optimization Criterion on given data in src and corresponding labels
// in labels. If 0 (or less) number of components are given, they are
// automatically determined for given data in computation.
LDA(const Mat& src, vector<int> labels,
int num_components = 0) :
_num_components(num_components)
{
this->compute(src, labels); //! compute eigenvectors and eigenvalues
} // Initializes and performs a Discriminant Analysis with Fisher's
// Optimization Criterion on given data in src and corresponding labels
// in labels. If 0 (or less) number of components are given, they are
// automatically determined for given data in computation.
LDA(InputArrayOfArrays src, InputArray labels,
int num_components = 0) :
_num_components(num_components)
{
this->compute(src, labels); //! compute eigenvectors and eigenvalues
} // Serializes this object to a given filename.
void save(const string& filename) const; // Deserializes this object from a given filename.
void load(const string& filename); // Serializes this object to a given cv::FileStorage.
void save(FileStorage& fs) const; // Deserializes this object from a given cv::FileStorage.
void load(const FileStorage& node); // Destructor.
~LDA() {} //! Compute the discriminants for data in src and labels.
void compute(InputArrayOfArrays src, InputArray labels); // Projects samples into the LDA subspace.
Mat project(InputArray src); // Reconstructs projections from the LDA subspace.
Mat reconstruct(InputArray src); // Returns the eigenvectors of this LDA.
Mat eigenvectors() const { return _eigenvectors; }; // Returns the eigenvalues of this LDA.
Mat eigenvalues() const { return _eigenvalues; } protected:
bool _dataAsRow;
int _num_components;
Mat _eigenvectors;
Mat _eigenvalues; void lda(InputArrayOfArrays src, InputArray labels);
};

2、演示样例

// LDA.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <contrib\contrib.hpp>
#include <cxcore.hpp>
using namespace cv;
using namespace std; int main(void)
{
//sampledata
double sampledata[6][2]={{0,1},{0,2},{2,4},{8,0},{8,2},{9,4}};
Mat mat=Mat(6,2,CV_64FC1,sampledata);
//labels
vector<int>labels;
for(int i=0;i<mat.rows;i++)
{
if(i<mat.rows/2)
{
labels.push_back(0);
}
else
{
labels.push_back(1);
}
} //do LDA
LDA lda=LDA(mat,labels);
//get the eigenvector
Mat eivector=lda.eigenvectors().clone(); cout<<"The eigenvector is:"<<endl;
for(int i=0;i<eivector.rows;i++)
{
for(int j=0;j<eivector.cols;j++)
{
cout<<eivector.ptr<double>(i)[j]<<" ";
}
cout<<endl;
} //针对两类分类问题,计算两个数据集的中心
int classNum=2;
vector<Mat> classmean(classNum);
vector<int> setNum(classNum); for(int i=0;i<classNum;i++)
{
classmean[i]=Mat::zeros(1,mat.cols,mat.type());
setNum[i]=0;
} Mat instance;
for(int i=0;i<mat.rows;i++)
{
instance=mat.row(i);
if(labels[i]==0)
{
add(classmean[0], instance, classmean[0]);
setNum[0]++;
}
else if(labels[i]==1)
{
add(classmean[1], instance, classmean[1]);
setNum[1]++;
}
else
{}
}
for(int i=0;i<classNum;i++)
{
classmean[i].convertTo(classmean[i],CV_64FC1,1.0/static_cast<double>(setNum[i]));
} vector<Mat> cluster(classNum);
for(int i=0;i<classNum;i++)
{
cluster[i]=Mat::zeros(1,1,mat.type());
multiply(eivector.t(),classmean[i],cluster[i]);
} cout<<"The project cluster center is:"<<endl;
for(int i=0;i<classNum;i++)
{
cout<<cluster[i].at<double>(0)<<endl;
} system("pause");
return 0;
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2pjMjExMzIy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

OpenCV LDA(Linnear Discriminant analysis)类的使用---OpenCV LDA演示样例的更多相关文章

  1. 多线程本地图片载入演示样例【OpenCV】【Pthread】

    Pthread barrier的简单使用演示样例: C++代码例如以下: // ThreadingLoadImages.cpp : 定义控制台应用程序的入口点. // #include "s ...

  2. Java线程演示样例 - 继承Thread类和实现Runnable接口

    进程(Process)和线程(Thread)是程序执行的两个基本单元. Java并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...

  3. Java 嵌套类和内部类演示样例&lt;二&gt;

    嵌套类(nested class)是一个在还有一个类或接口内部声明的类. 嵌套类分为两种:静态内部类(static inner class)和非静态嵌套类(non-static nested clas ...

  4. Java 嵌套类和内部类演示样例&lt;三&gt;

    <span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...

  5. Python类的继承演示样例

    class Pet: __name = "" def __init__(self, name): self.__name = name def bark(self): return ...

  6. DM8168 OpenCV尝试与评估(编译ARM版OpenCV)

     交叉编译opencv2.3.1,并在DM8168 cortex A8中执行图像处理. 开发环境: PC:ubuntu12.04LTS.Intel Core 2 Duo CPU  E7200@2. ...

  7. 大数据学习day16------第三阶段-----scala04--------1. 模式匹配和样例类 2 Akka通信框架

    1. 模式匹配和样例类 Scala有一个十分强大的模式匹配机制,可以应用到很多场合:如switch语句.类型检查等.并且Scala还提供了样例类,对模式匹配进行了优化,可以快速进行匹配 1.1 模式匹 ...

  8. 线性判别分析(Linear Discriminant Analysis,LDA)

    一.LDA的基本思想 线性判别式分析(Linear Discriminant Analysis, LDA),也叫做Fisher线性判别(Fisher Linear Discriminant ,FLD) ...

  9. 线性判别分析(Linear Discriminant Analysis, LDA)算法分析

    原文来自:http://blog.csdn.net/xiazhaoqiang/article/details/6585537 LDA算法入门 一. LDA算法概述:       线性判别式分析(Lin ...

随机推荐

  1. 【POJ3580】【块状链表】SuperMemo

    Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant i ...

  2. 封装Timer

    System.Timers.Timer,System.Timers.Timer在使用的过程中需要: 1.构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.T ...

  3. JS判断浏览器是否支持某一个CSS3属性的方法

    var div = document.createElement('div'); console.log(div.style.transition); //如果支持的话, 会输出 "&quo ...

  4. 如何:在 StackPanel 和 DockPanel 之间进行选择

    虽然可以使用 DockPanel 或 StackPanel 来堆叠子元素,但这两个控件并不总是会产生相同的结果. 例如,子元素的放置顺序可能会影响 DockPanel 中子元素的大小,但不会影响 St ...

  5. Bootstrap_网格系统

    首先添加CSS样式: [class *= col-]{ background-color: #eee; border: 1px solid #ccc; } [class *= col-] [class ...

  6. wdcp-apache配置错误导致进程淤积进而内存吃紧

    内存总是越来越少,虚拟内存使用越来越多 首先确定到底是什么占用了大量的内存 可以看到,大部分内存被闲置的httpd进程占用 且当我重启mysql服务后,内存没有出现明显变化,但是当我重启apache时 ...

  7. SAE flask及其扩展 bug指南

    1. ImportError: No moudle named ext.bootstrap 导入依赖包失败 SAE会提供一种机制通过导入virtualenv.bundle来解决 此处有坑:官网文档中说 ...

  8. VS自带的功能:性能和诊断

    先看一眼代码: using System; using System.Collections.Generic; using XCode; using XCode.Configuration; usin ...

  9. eclipse问题解决(maven插件link方式安装失败)

    一.link方式安装eclipse的一款插件:maven     (附:若不熟悉link方式,则进入此处:link方式安装eclipse插件) 其间,只弹出警告,大概意思是:部分内容,未经授权,谨慎使 ...

  10. 3.2. Grid Search: Searching for estimator parameters

    3.2. Grid Search: Searching for estimator parameters Parameters that are not directly learnt within ...