Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OpenCVPCA异常:不支持输入和输出数组格式的组合_C#_Opencv_Pca_Opencvsharp - Fatal编程技术网

C# OpenCVPCA异常:不支持输入和输出数组格式的组合

C# OpenCVPCA异常:不支持输入和输出数组格式的组合,c#,opencv,pca,opencvsharp,C#,Opencv,Pca,Opencvsharp,我正在使用OpenCVSharp(OpenCvSharp3 AnyCPU版本3.0.0.20150823在Visual Studio 2015中运行,并通过NuGet安装)从C#访问OpenCV,但在调用Cv2.PCACompute时,我得到一个通用OpenCVException,说明我有一个不受支持的输入和输出数组格式组合 我的目标是使用PCA找到像素点的主轴。这是我目前的(精简)代码: using OpenCvSharp; public struct point2D { publ

我正在使用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
}
使用OpenCvSharp;
公共结构点2D
{
公共int X;
公共智力;
公共点2D(整数X,整数Y)
{
这个.X=X;
这个。Y=Y;
}
}
公共静态无效PCA2D()
{
整数高度=5;
整数宽度=5;
int[]图像=新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,
}
//提取数据点
列表数据点=新列表();
对于(int行=0;行<高度;++行)
{
用于(整数列=0;列<宽度;++列)
{
如果(图像[行*宽+列]==1)
{
添加(新的点2D(列,行));
}
}
}
//创建输入矩阵
Mat输入=新Mat(dataPoints.Length,2,MatType.CV_32SC1);
对于(int i=0;i

我还没有找到任何关于如何初始化均值和特征向量矩阵的文档,或者我调用PCACompute的方式是否正确。深入了解使用PCA计算机的正确过程将非常有帮助。

因此,
数据点
不能是
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);
}
//创建输入矩阵
材料输入=新材料(数据点.Length,2,材料类型.CV_32FC1);
对于(int i=0;i