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

I am using OpenCVSharp, but currently I have half of my code in C and the other half in C ++ API, I am trying to port all this to a C ++ version in order to avoid an outdated API and also avoid loading the image (twice Mat instead of one Mat and one CvMat for each image)

Here the code I had is working:

  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 (which seemed like an explicit port when I typed it) that didn't work (I get a single color image of the color 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); 

Are you setting the values ​​wrong? Is the transfer not so simple?

+7
c # opencv opencvsharp
source share
2 answers

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

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

But in the second snippet, you create it as var newSourceImage = ... Thus, it seems that the "nonsense" defines your Mat type on its own and does it wrong.

Can you show all the initialization, please?

+1
source share

Have you decided this?

I have not used OpenCVSharp, but I use OpenCV in C ++ and call it from C # using the C ++ / CLI wrapper. I used EmguCV before, I think using OpenCV in C ++ is better than through the shell, sometimes the errors are clearer because you can debug and view the call stack, and also have a wider community to help you with the APIs OpenCV C ++ and Python.

Based on my experience, if you could get your own code and debug it, that would be better, so you can be sure that OpenCV undistort is actually getting the correct parameters. Try turning on your own code debugging, but I think you will need the OpenCVSharp source code to debug it.

If this does not work, perhaps you can look at the original non-repeating OpenCV method and recreate it using OpenCVSharp. Or perhaps unconscious channels separately and merger. It is hardly ideal, but you can’t fix the hidden behavior, just get around it.

0
source share

All Articles