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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With