initUndistortRectifyMap assert error

asked 2015-08-13 19:16:12 -0600

Yolteotl gravatar image

updated 2015-08-13 19:27:51 -0600

Hi,

I'm trying to rewrite the stereo calibrate example in Java. I think I did things okayish but I got an error I don't understand how it can happen :

   OpenCV Error: Assertion failed (K.size() == Size(3, 3) && (D.empty() || D.total() == 4)) in initUndistortRectifyMap, file /home/yolteotl/opencv/opencv-3.0.0/modules/calib3d/src/fisheye.cpp, line 410
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: /home/yolteotl/opencv/opencv-3.0.0/modules/calib3d/src/fisheye.cpp:410: error: (-215) K.size() == Size(3, 3) && (D.empty() || D.total() == 4) in function initUndistortRectifyMap
]
    at org.opencv.calib3d.Calib3d.initUndistortRectifyMap_0(Native Method)
    at org.opencv.calib3d.Calib3d.initUndistortRectifyMap(Calib3d.java:834)
    at com.yolt.server.calibration.Calibration.calibrate(Calibration.java:267)
    at com.yolt.server.GUI.<init>(GUI.java:67)
    at com.yolt.server.ServerApplication.main(ServerApplication.java:30)

The correspond code :

    Mat cameraMatrix[] = new Mat[]{Mat.eye(3, 3, CvType.CV_64F), 
            Mat.eye(3, 3, CvType.CV_64F)};
    Mat distCoef[] = new Mat[]{new Mat(), new Mat()};

    Mat rMat = new Mat();
    Mat tMat = new Mat();
    Mat eMat = new Mat();
    Mat fMat = new Mat();
    double rms = Calib3d.stereoCalibrate(objectsPoint, cornersMatLeftList, cornersMatRightList, 
                cameraMatrix[0], distCoef[0], cameraMatrix[1], distCoef[1], 
                imgSize, rMat, tMat, eMat, fMat, CALIB_FLAGS, CALIB_CRITERIA);

        System.out.println("Calibration done with RMS error=" + rms);

    m1 = cameraMatrix[0];
    m2 = cameraMatrix[1];
    d1 = distCoef[0];
    d2 = distCoef[1];

    System.out.println("matrix saved");

    r1 = new Mat();
    r2 = new Mat();
    p1 = new Mat();
    p2 = new Mat();
    qMat = new Mat();

    Calib3d.stereoRectify(m1, d1, m2, d2, imgSize, rMat, tMat, r1, r2, p1, p2, qMat);

    mx1 = new Mat();
    mx2 = new Mat();
    my1 = new Mat();
    my2 = new Mat();

   Calib3d.initUndistortRectifyMap(m1, d1, r1, p1, imgSize, CvType.CV_16SC2, mx1, my1);        
   Calib3d.initUndistortRectifyMap(m2, d2, r2, p2, imgSize, CvType.CV_16SC2, mx2, my2);

The error is coming from the initUndistortRectifyMap line, and from the d1 / d2 which are not empty and total() = 5 instead of 4. How this can happen whereas I didn't modify those Mat and I'm just using the returned Mat from stereoRectify() ?

Any help would be welcome!

Edit : If this can help, I'm using those flags to calibrate / findChessboard :

private static final int CORNERS_FLAGS = Calib3d.CALIB_CB_FAST_CHECK 
    | Calib3d.CALIB_CB_NORMALIZE_IMAGE;
private static final int CALIB_FLAGS = Calib3d.CALIB_FIX_ASPECT_RATIO 
        | Calib3d.CALIB_ZERO_TANGENT_DIST  
    | Calib3d.CALIB_SAME_FOCAL_LENGTH;
private static final TermCriteria CORNERS_CRITERIA = new TermCriteria(TermCriteria.EPS
    + TermCriteria.MAX_ITER, 40, 0.001);
private static final TermCriteria CALIB_CRITERIA = new TermCriteria(TermCriteria.COUNT
    + TermCriteria.EPS, 100, 0.00001);
edit retag flag offensive close merge delete

Comments

why is cv::fisheye::initUndistortRectifyMap() being called ? (should be cv::initUndistortRectifyMap())

looks like a bug.

berak gravatar imageberak ( 2015-08-14 00:14:42 -0600 )edit

Ah, nice to know. I also found that there is a initUndistortRectifyMap() in the Imgproc wrapper. So by using Calib3d.stereoCalibrate(), then Calib3d.stereoRectify() and to finish Imgproc.initUndistortRectifyMap(), it seems ok.

Yolteotl gravatar imageYolteotl ( 2015-08-14 07:03:39 -0600 )edit