简体   繁体   中英

UndistortPoints in Android OpenCV (C#) and properly forming the input matricies

In an attempt to speed up some processing in a real time application of computer vision I'm developing for Android platforms, I'd like to undistort some key points, instead of entire frames. I've followed the documentation to the best of my abilities, but I'm receiving the following error:

OpenCV Error: Assertion failed (CV_IS_MAT(_cameraMatrix) && _cameraMatrix->rows == 3 && _cameraMatrix->cols == 3) in void cvUndistortPoints(const CvMat*, CvMat*, const CvMat*, const CvMat*, const CvMat*, const CvMat*), file /Volumes/Linux/builds/master_pack-android/opencv/modules/imgproc/src/undistort.cpp, line 301

The appropriate documentation is here: http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#undistortpoints

In my code I define and populate the matrix elements individually since they're small. I was unable to find a way to populate MatofPoint2f gracefully, but searching led to me to convert from a list as you'll see below.

Mat camMtx = new Mat(1, 5, CvType.Cv64fc1, new Scalar(0));
Mat distCoef = new Mat(3, 3, CvType.Cv64fc1, new Scalar(0));
MatOfPoint2f uncalibpoints = new MatOfPoint2f();
MatOfPoint2f calibpoints = new MatOfPoint2f();
List<Point> points = new List<Point>();
points.Add(center); //Some previously stored point
points.Add(apex1); //Some previously stored point
points.Add(apex2); //Some previously stored point
points.Add(apex3); //Some previously stored point
uncalibpoints.FromList(points); //Convert list of points to MatofPoint2f

Console.WriteLine(uncalibpoints.Channels());
Console.WriteLine(uncalibpoints.Size());
Console.WriteLine(uncalibpoints.GetType());

//Manually setting the matrix values
distCoef.Put(0, 0, 0.51165764);
distCoef.Put(0, 1, -1.96134156);
distCoef.Put(0, 2, 0.00600294);
distCoef.Put(0, 3, 0.00643735);
distCoef.Put(0, 4, 2.59503145);
camMtx.Put(0, 0, 1551.700);
camMtx.Put(0, 1, 0.0);
camMtx.Put(0, 2, 962.237163);
camMtx.Put(1, 0, 0.0);
camMtx.Put(1, 1, 1536.170);
camMtx.Put(1, 2, 589.418432);
camMtx.Put(2, 0, 0.0);
camMtx.Put(2, 1, 0.0);
camMtx.Put(2, 2, 1.0);

Imgproc.UndistortPoints(uncalibpoints, calibpoints, camMtx, distCoef);`

Two issues with the above code:

  1. Simple error in the allocation of the camMtx and distCoeff (reversed their allocation in a copy paste type error)

  2. The Undistort Points call should look like this:

    Imgproc.UndistortPoints(uncalibpoints, calibpoints, camMtx, distCoef, new Mat(), camMtx);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM