0

I have an Image<Gray, Byte> trainImage with the size NxN, and i want to change it into 1D Matrix<float> trainMatrix with the size (N^2)x1.

What I am trying to do is to call CvInvoke.cvCalvCovarmatrix(). For the first parameter I'm using Matrix<Single>[] that i convert into IntPtr[].

Matrix<Single> avg = new Matrix<float>(7, 1);
Matrix<Single> cov = new Matrix<float>(7, 7);
Matrix<Single>[] input = new Matrix<float>[3];
for (int i = 0; i < 3; i++)
    input[i] = new Matrix<float>(7, 1);

IntPtr[] inObjs = Array.ConvertAll<Matrix<Single>, IntPtr>(input, delegate(Matrix<Single> mat) { return mat.Ptr; });

CvInvoke.cvCalcCovarMatrix(inObjs, 3, cov, avg, COVAR_METHOD.CV_COVAR_NORMAL);

But now i have input that is Image<Gray, Byte>[] with each image size (let's assume) 7x7. I think i will need to convert each image into Matrix<float> with size 49x1 first before changing it into IntPtr[]. How to do it?

maru1414152
  • 73
  • 2
  • 5
  • Do you mean 2D Matrix? Because a 1D matrix is a vector. – lorenz albert Sep 11 '12 at 08:56
  • Oh yes, a vector, but the type is Matrix, that's why the Matrix have N^2 rows and only 1 column. – maru1414152 Sep 11 '12 at 09:02
  • You can check [this question](http://stackoverflow.com/questions/7367770/how-to-flatten-or-index-3d-array-in-1d-array) it is about 3d arrays but it's very similar IMO. But in the question the solution for 2D is already given. – Dominik G Sep 11 '12 at 09:11
  • So you what you want to have is an image with 1 row or 1 column, right ? Could you provide some code. Because I don't know what Image exactly is (Simple GrayscaleMatrix?). – lorenz albert Sep 11 '12 at 09:16
  • I've edited the question. The Image come from emguCV, i don't know if it's a simple grayscalematrix or not. – maru1414152 Sep 11 '12 at 09:38

1 Answers1

0

You are on the right way. I put here some code that will serve as an example also to the others as there are no emgucv covariance examples out there.

      Matrix<float> mat1 = new Matrix<float>(1, 3);
      mat1[0, 0] = 38;
      mat1[0, 1] = 55;
      mat1[0, 2] = 49;

      Matrix<float> mat2 = new Matrix<float>(1, 3);
      mat2[0, 0] = 43;
      mat2[0, 1] = 54;
      mat2[0, 2] = 4;

      Matrix<float> cov = new Matrix<float>(3, 3);

      Matrix<float> avg = new Matrix<float>(2, 1);

      Matrix<float>[] input = new Matrix<float>[2];
      input[0] = mat1;
      input[1] = mat2;          
      IntPtr[] inputPtrs = Array.ConvertAll<Matrix<Single>, IntPtr>(input, delegate(Matrix<Single> mat) { return mat.Ptr; });
      CvInvoke.cvCalcCovarMatrix(inputPtrs, 2, cov, avg, COVAR_METHOD.CV_COVAR_NORMAL);

I checked cov matrix values against computed wolfram ones and they seems ok.

All my best, Luca

Luca Del Tongo
  • 2,702
  • 16
  • 18