OpenCvSharp PCA 异常:不支持的输入和输出数组格式组合
OpenCvSharp PCA Exception: Unsupported combination of input and output array formats
我正在使用 OpenCVSharp(OpenCvSharp3-AnyCPU 版本 3.0.0.20150823 运行 在 Visual Studio 2015 中并通过 NuGet 安装)从 C# 访问 OpenCV,但是当调用 Cv2.PCACompute
我得到一个通用的 OpenCVException 说明我有一个 不支持的输入和输出数组格式组合 .
我的目标是使用 PCA 找到像素块的主轴。这是我目前的(精简)代码:
using OpenCvSharp;
public struct point2D
{
public int X;
public int Y;
public point2D(int X, int Y)
{
this.X = X;
this.Y = Y;
}
}
public static void PCA2D()
{
int height = 5;
int width = 5;
int[] image = new int[]
{
0,0,0,0,1,
0,0,0,1,0,
0,0,1,0,0,
0,1,0,0,0,
1,0,0,0,0,
}
// extract the datapoints
List<point2D> dataPoints = new List<point2D>();
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
if (image[row * width + col] == 1)
{
dataPoints.Add(new point2D(col, row));
}
}
}
// create the input matrix
Mat input = new Mat(dataPoints.Length, 2, MatType.CV_32SC1);
for (int i = 0; i < dataPoints.Length; ++i)
{
input.Set(i, 0, dataPoints[i].X);
input.Set(i, 1, dataPoints[i].Y);
}
Mat mean = new Mat();
Mat eigenvectors = new Mat();
// OpenCVException occurs here: unsupported combination of input and output array formats
Cv2.PCACompute(input, mean, eigenvectors);
// Code to get orientation from the eigenvectors
}
我无法找到任何关于如何初始化均值和特征向量垫的文档,或者我调用 PCACompute 的方式是否正确。深入了解使用 PCACompute 的正确过程将非常有帮助。
原来dataPoints
不可能是MatType.CV_32SC1
。将代码更改为以下允许它工作:
// create the input matrix
Mat input = new Mat(dataPoints.Length, 2, MatType.CV_32FC1);
for (int i = 0; i < dataPoints.Length; ++i)
{
input.Set(i, 0, (float)dataPoints[i].X);
input.Set(i, 1, (float)dataPoints[i].Y);
}
我正在使用 OpenCVSharp(OpenCvSharp3-AnyCPU 版本 3.0.0.20150823 运行 在 Visual Studio 2015 中并通过 NuGet 安装)从 C# 访问 OpenCV,但是当调用 Cv2.PCACompute
我得到一个通用的 OpenCVException 说明我有一个 不支持的输入和输出数组格式组合 .
我的目标是使用 PCA 找到像素块的主轴。这是我目前的(精简)代码:
using OpenCvSharp;
public struct point2D
{
public int X;
public int Y;
public point2D(int X, int Y)
{
this.X = X;
this.Y = Y;
}
}
public static void PCA2D()
{
int height = 5;
int width = 5;
int[] image = new int[]
{
0,0,0,0,1,
0,0,0,1,0,
0,0,1,0,0,
0,1,0,0,0,
1,0,0,0,0,
}
// extract the datapoints
List<point2D> dataPoints = new List<point2D>();
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
if (image[row * width + col] == 1)
{
dataPoints.Add(new point2D(col, row));
}
}
}
// create the input matrix
Mat input = new Mat(dataPoints.Length, 2, MatType.CV_32SC1);
for (int i = 0; i < dataPoints.Length; ++i)
{
input.Set(i, 0, dataPoints[i].X);
input.Set(i, 1, dataPoints[i].Y);
}
Mat mean = new Mat();
Mat eigenvectors = new Mat();
// OpenCVException occurs here: unsupported combination of input and output array formats
Cv2.PCACompute(input, mean, eigenvectors);
// Code to get orientation from the eigenvectors
}
我无法找到任何关于如何初始化均值和特征向量垫的文档,或者我调用 PCACompute 的方式是否正确。深入了解使用 PCACompute 的正确过程将非常有帮助。
原来dataPoints
不可能是MatType.CV_32SC1
。将代码更改为以下允许它工作:
// create the input matrix
Mat input = new Mat(dataPoints.Length, 2, MatType.CV_32FC1);
for (int i = 0; i < dataPoints.Length; ++i)
{
input.Set(i, 0, (float)dataPoints[i].X);
input.Set(i, 1, (float)dataPoints[i].Y);
}