1

I know this "looks" like a duplicate question, but -- it's not answered in any definitive way that I can see. In C++, OpenCv operation to init a Mat can be done like so:

    float[,] camera = new float[,] {
                    { 857.483f, 0.0f, 968.06f },
                    { 0.0f, 876.72f, 556.37f },
                    { 0.0f, 0.0f, 1.0f }
                };
    A = Mat(3,3, CV_32FC1, &camera);

Note that the array is 2D. Now, I want to accomplish the same thing in EMGU, //using managed C#; so Mat is constructed as:

    Mat _cameraMatrix = new Mat(3, 3, DepthType.Cv64F, 1);
// and a similar 1D Mat:
    Mat _distCoeffs = new Mat(8, 1, DepthType.Cv64F, 1);
// to be passed to the method:
   CvInvoke.Undistort(bkgimage, ndimage, _cameraMatrix, _distCoeffs);

Unlike cv::Mat, EMGU Mat's constructor doesn't seem to have an obvious way to get that 2D "camera" data into it. Anyone have any success getting this kind of code to work?

Jim
  • 33
  • 8

2 Answers2

1

Funnily enough, I came across this question while trying to solve exactly the same problem - camera calibration. There's an easier but much less elegant method to solve this:

Image<Gray, float> camImage = new Image<Gray, float>(new float[,,]
{
    {intrinsics.fx}, {0}, {intrinsics.ppx}},
    { {0}, {intrinsics.fy}, {intrinsics.ppy}},
    { {0}, {0}, {1} }
});
var camMatrix = camImage.Mat;

Image<Gray, float> distImage = new Image<Gray, float>(new float[,,] {{{0},{0},{0},{0}}});
var distCoeffs = distImage.Mat;

Not sure it's the best answer, but it's an answer.

mike1952
  • 493
  • 5
  • 12
  • It's been a while since I was into this, but your solution seems "reasonable"; not sure why it "needs" to be elegant ;^) But, for completeness... what is the "intrinsics" object that's referenced? – Jim Apr 26 '19 at 15:17
  • I saw that it was a bit old, but I figured someone else would need it too. It doesn't need to be elegant, but I feel guilty :) The intrinsics object is, in this case, a Brown-Conrady object from realsense2. The specific object is less relevant, but the key things are that fx and fy are the relevant focal lengths and ppx and ppy are the image centres. You can get these from a checkerboard calibration or - if your camera supplies them - from the camera itself. Looking at your matrix above, you've clearly gotten them from somewhere. – mike1952 May 09 '19 at 07:00
0

According to the EmguCV documentation, there is a constructor that takes an IntPr as 'data': see http://www.emgu.com/wiki/files/3.4.3/document/html/c8424736-2d44-c5d6-212f-31dedfe6fb95.htm

The problem is how to get that pointer. Here, maybe this post will help you:

How to get IntPtr from byte[] in C#

Hopefully, this will get you at least started.

Roland Deschain
  • 2,211
  • 19
  • 50