Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new C++ style API in OpenCVSharp code?

I'm using OpenCVSharp but currently have half of my code in the C and the other half in the C++ API, I'm trying to port it all to the C++ version to avoid the deprecated API as well as to avoid loading images twice (share Mat instead of having one Mat and one CvMat per image)

Here's the code I had that works :

            CvMat distortion = new CvMat(8, 1, MatrixType.F64C1);
            distortion[0, 0] = camera.CameraConfig.k1;
            distortion[1, 0] = camera.CameraConfig.k2;
            distortion[2, 0] = camera.CameraConfig.p1;
            distortion[3, 0] = camera.CameraConfig.p2;
            distortion[4, 0] = camera.CameraConfig.k3;
            distortion[5, 0] = 0;
            distortion[6, 0] = 0;
            distortion[7, 0] = 0;

            CvMat intrinsic = new CvMat(3, 3, MatrixType.F32C1);
            intrinsic[0, 0] = camera.CameraConfig.fx;
            intrinsic[0, 1] = camera.CameraConfig.skew;
            intrinsic[0, 2] = camera.CameraConfig.cx;
            intrinsic[1, 0] = 0;
            intrinsic[1, 1] = camera.CameraConfig.fy;
            intrinsic[1, 2] = camera.CameraConfig.cy;
            intrinsic[2, 0] = 0;
            intrinsic[2, 1] = 0;
            intrinsic[2, 2] = 1;
            Cv.Undistort2(camera.SourceImage, newSourceImage,intrinsic,distortion);

And the code (that seemed like an obvious port as I typed it) that doesn't work (I end up with a single colored image of a color that is present in the scene) :

            Mat distortion = new Mat(8, 1, MatType.CV_64FC1);
            distortion.Set(0, 0, camera.CameraConfig.k1);
            distortion.Set(1, 0, camera.CameraConfig.k2);
            distortion.Set(2, 0, camera.CameraConfig.p1);
            distortion.Set(3, 0, camera.CameraConfig.p2);
            distortion.Set(4, 0, camera.CameraConfig.k3);
            distortion.Set(5, 0, 0);
            distortion.Set(6, 0, 0);
            distortion.Set(7, 0, 0);

            Mat intrinsic = new Mat(3, 3, MatType.CV_32FC1);
            intrinsic.Set(0, 0, camera.CameraConfig.fx);
            intrinsic.Set(0, 1, camera.CameraConfig.skew);
            intrinsic.Set(0, 2, camera.CameraConfig.cx);
            intrinsic.Set(1, 0, 0);
            intrinsic.Set(1, 1, camera.CameraConfig.fy);
            intrinsic.Set(1, 2, camera.CameraConfig.cy);
            intrinsic.Set(2, 0, 0);
            intrinsic.Set(2, 1, 0);
            intrinsic.Set(2, 2, 1);
            var newSourceImage = camera.SourceImage.Undistort(intrinsic, distortion);

Am I wrongly setting the values? Is the porting not that straightforward?

like image 991
Ronan Thibaudau Avatar asked Sep 30 '15 16:09

Ronan Thibaudau


1 Answers

It looks like the difference in your camera.SourceImage and newSourceImage. In your first snippet you assign newSourceImage out of the scope, so I assume you can do something like:

newSourceImage = new Mat(new Size(...), MatType.CV_8UC3)

But in your second snippet you create it as var newSourceImage = .... So, it looks like 'undistort' determines your Mat type by itself and does it wrong.

Can you show all initialization, please?

like image 199
Melnikov Sergei Avatar answered Nov 19 '22 03:11

Melnikov Sergei