原始 OpenCV 的 lda.cpp 封裝較多,不便取得 LDA 過程中的中間值。改寫後可獨立編譯,並輸出感興趣的矩陣或向量。
#include <cxcore.hpp>
#include <iostream>
#include "lda.h"
using namespace std;
using namespace cv;
int main(void)
{
double data[6][2] = {
{0, 1}, {0, 2}, {1, 4},
{8, 0}, {8, 2}, {9, 4}
};
Mat dmat = Mat(6, 2, CV_64FC1, data);
int labels[6] = {0, 0, 0, 1, 1, 1};
Mat lmat = Mat(1, 6, CV_32SC1, labels);
MyLDA(dmat, lmat);
return 0;
}#ifndef _MY_LDA_H
#define _MY_LDA_H
#include <cxcore.hpp>
extern void MyLDA(InputArrayOfArrays _src, InputArray _lbls);
#endif_src 是樣本矩陣,每行代表一個樣本;_lbls 是類別標籤。MyLDA 可進一步改寫成回傳投影方向、類間矩陣或類內矩陣,方便後續視覺化與分析。