我正在使用EmguCV 3.0.0包装器来使用OpenCV 3.0。 EmguCV PCACompute
方法包装了OpenCV PCA::operator()
方法。
以下代码编译并运行。评论应该很好地解释意图。
// Populate the 5 row by 8 column input array (5 samples of 8 dimensions).
// The sample dimensions (columns) vary like this:
// - low variance: 0, 1, 4, 5, 6, 7
// - high variance: 2, 3
Matrix<double> input = new Matrix<double>(5, 8);
var r = new Random();
for (int row = 0; row < 5; row++) {
input.Data[row,0] = r.Next(0, 10); // low variance
input.Data[row,1] = r.Next(0, 20); // low variance
input.Data[row,2] = r.Next(80, 210); // high variance
input.Data[row,3] = r.Next(0, 240); // highest variance
input.Data[row,4] = r.Next(20, 21); // very low variance
input.Data[row,5] = r.Next(0, 10); // low variance
input.Data[row,6] = r.Next(0, 10); // low variance
input.Data[row,7] = r.Next(200, 210); // low variance
}
// create output array for PCACompute()
var eigenvectors = new Matrix<double>(8, 8);
// create *empty* mean array so that PCACompute() calculates its own means
var means = new Mat();
// HERE IS THE MAGIC.
CvInvoke.PCACompute(input, means, eigenvectors);
但魔法被打破了。毕竟eigenvectors
全是零。这个漂亮的打印代码:
// print each eigenvector on its own line
for (int vectorIdx = 0; vectorIdx < eigenvectors.Rows; vectorIdx++) {
string vectorStr = "";
for(int dimension = 0; dimension < eigenvectors.Cols; dimension++) {
vectorStr += eigenvectors.Data[vectorIdx, dimension].ToString() + ", ";
}
Console.WriteLine("{ " + vectorStr.Substring(0, vectorStr.Length - 2) + " }");
}
给出了这个输出:
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
事实上,如果我在之前将eigenvectors
的成员设置为PCACompute
:
eigenvectors.Data[1,1] = 42;
CvInvoke.PCACompute(input, means, eigenvectors);
漂亮的图片显示 eigenvectors
完全不受PCACompute
影响:
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 42, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
这是一个错误,还是我做错了?