Fisheye InitUndistortRectifyMap and Remap issue
Summary of your issue
I am trying to calibrate a fish eye camera with camera and distortion matrices I obtained using the Python code in the following link: https://medium.com/@kennethjiang/calibrate-fisheye-lens-using-opencv-333b05afa0b0 I then proceeded to try to undistort the camera shots using the OpenCVSharp library using the aforementioned matrices. Unfortunately, the image does not get undistorted by the code below, which is technically identical to the code in the example below.
Environment
.NET Standard 3.0. Windows 11
What did you do when you faced the problem?
I tried various different inputs and input types, for example identity matrix for variable D, got the exact same picture back as I fed into the algorithm. Also I tried to have the map output in float, MatType.32FC1, resulting in the same black screen.
Example code:
private void UndistortImage(Mat image)
{
try
{
var k = new double[,]{ { 1570.7924595537536, 0.0, 286.031469726162},
{ 0.0, 1608.7235808775135, 229.44339886203983 },
{ 0.0, 0.0, 1.0 }};
var d = new double[] { -11.58754033191892, -11.58754033191892, 3953.3612313646645, -66435.10629047279 };
var kMatrix = new Mat(3, 3, MatType.CV_32FC1, k);
var identity = new double[,]{ { 1.0, 0.0, 0.0},
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 }};
var dMatrix = new Mat(4, 1, MatType.CV_32FC1, d);
var identityMatrix = new Mat(3, 3, MatType.CV_32FC1, identity);
UndistortedImage = Tracker.NewMat();
Mat map1 = Tracker.NewMat();
Mat map2 = Tracker.NewMat();
Cv2.FishEye.InitUndistortRectifyMap(kMatrix, dMatrix, identityMatrix, kMatrix, image.Size(), MatType.CV_16SC2, map1, map2);
Cv2.Remap(image, UndistortedImage, map1, map2, InterpolationFlags.Linear, BorderTypes.Constant);
var test1 = map1.Get<short>(240,320,0);
var test2 = map2.Get<short>(240, 320);
}
catch (Exception ex)
{
Message = ex.Message;
UndistortedImage = null;
}
}
Output:

What did you intend to be?
Checkerboard pattern should be straight and not distored as seen above.
Fixed it by cross checking with OpenCV in C++, input arrays need to be in float when defined as CV32F. ` private void UndistortImage(Mat image) {
try
{
var k = new float[,]{ { 1570.7924595537536, 0.0, 286.031469726162},
{ 0.0, 1608.7235808775135, 229.44339886203983 },
{ 0.0, 0.0, 1.0 }};
var d = new float[] { -11.58754033191892, -11.58754033191892, 3953.3612313646645, -66435.10629047279 };
var kMatrix = new Mat(3, 3, MatType.CV_32FC1, k);
var identity = new float[,]{ { 1.0, 0.0, 0.0},
{ 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 1.0 }};
var dMatrix = new Mat(4, 1, MatType.CV_32FC1, d);
var identityMatrix = new Mat(3, 3, MatType.CV_32FC1, identity);
UndistortedImage = Tracker.NewMat();
Mat map1 = Tracker.NewMat();
Mat map2 = Tracker.NewMat();
Cv2.FishEye.InitUndistortRectifyMap(kMatrix, dMatrix, identityMatrix, kMatrix, image.Size(), MatType.CV_16SC2, map1, map2);
Cv2.Remap(image, UndistortedImage, map1, map2, InterpolationFlags.Linear, BorderTypes.Constant);
var test1 = map1.Get<short>(240,320,0);
var test2 = map2.Get<short>(240, 320);
}
catch (Exception ex)
{
Message = ex.Message;
UndistortedImage = null;
}
}
`