[FIXED] OpenCvSharp PCA Exception: Unsupported combination of input and output array formats

Issue

I'm using OpenCVSharp (OpenCvSharp3-AnyCPU Version 3.0.0.20150823 running in Visual Studio 2015 and installed via NuGet) to access OpenCV from C#, but when calling Cv2.PCACompute I get a generic OpenCVException stating that I have an Unsupported combination of input and output array formats.

My goal is to use PCA to find the primary axis of a pixel blob. This is my (stripped down) code at present:

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
}

I haven't been able to find any documentation on how to initialise the mean and eigenvector Mats, or if the way I'm calling PCACompute is even correct. Some insight into the correct procedure for using PCACompute would be immensely helpful.


Solution

So it turns out that dataPoints cannot be MatType.CV_32SC1. Changing the code to the following allowed it to work:

// 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);
}


Answered By - Ayb4btu
Answer Checked By - Gilberto Lyons (sql.easybugfix Admin)

Related Posts

Post a Comment