Skip to content

Function Calib3d.fisheye_estimateNewCameraMatrixForUndistortRectify() result on Android is different from Python/C++ #17171

@KTOC

Description

@KTOC
System information (version)
  • OpenCV => 4.2 SDK for Android download from Github
  • Operating System / Platform => Android (compileSdkVersion 29 buildToolsVersion "29.0.3")
  • Compiler => 'com.android.tools.build:gradle:3.6.1' (gradle-5.6.4-all.zip)
Detailed description

I tried to port my application to Android app which using OpenCV function Calib3d.fisheye_estimateNewCameraMatrixForUndistortRectify but the result is different from the application on C++ and Python.
I tried on version 4.3 and 3.4.10 and 3.3.1 (both java and JNI methods) but the problem still remains.

Android result:

K: [600.44476, 0, 992.06427;
     0, 578.99298, 549.26825;
     0, 0, 1]

D: [-0.050901033;
     0.030944414;
     -0.021509226;
     0.0043378095]

K_new: [387.4809, 0, 1036.6697;
     0, 373.63757, 538.83734;
     0, 0, 1]

C++:

./fisheye_undistort_test 
K:
[600.44477382, 0, 992.06425788;
 0, 578.99298055, 549.26826242;
 0, 0, 1]
D:
[-0.05090103223466704;
 0.03094441364217331;
 -0.02150922549319891;
 0.004337809662829714]
Size:
[1920 x 1080]
K_new:
[394.774938075781, 0, 960.4617594329543;
 0, 380.671009239992, 528.6503559843645;
 0, 0, 1]

Python:

python3 fisheye_undistort_test.py 
K:
[[600.44477382   0.         992.06425788]
 [  0.         578.99298055 549.26826242]
 [  0.           0.           1.        ]]
D:
[[-0.05090103]
 [ 0.03094441]
 [-0.02150923]
 [ 0.00433781]]
Size:
(1920, 1080)
K_new:
[[394.77493808   0.         960.46175943]
 [  0.         380.67100924 528.65035598]
 [  0.           0.           1.        ]]

The OpenCV version used in C++ and Python are 4.2 also.

Steps to reproduce

Code in C++:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/calib3d.hpp>

int main()
{
    cv::Size size = { 1920, 1080 };

    cv::Mat K(3, 3, cv::DataType<double>::type);
    K.at<double>(0, 0) = 600.44477382;
    K.at<double>(0, 1) = 0.0;
    K.at<double>(0, 2) = 992.06425788;

    K.at<double>(1, 0) = 0.0;
    K.at<double>(1, 1) = 578.99298055;
    K.at<double>(1, 2) = 549.26826242;

    K.at<double>(2, 0) = 0.0;
    K.at<double>(2, 1) = 0.0;
    K.at<double>(2, 2) = 1.0;

    cv::Mat D(4, 1, cv::DataType<double>::type);
    D.at<double>(0, 0) = -0.05090103223466704;
    D.at<double>(1, 0) = 0.030944413642173308;
    D.at<double>(2, 0) = -0.021509225493198905;
    D.at<double>(3, 0) = 0.0043378096628297145;
    cv::Mat E = cv::Mat::eye(3, 3, cv::DataType<double>::type);

    cv::Mat map1;
    cv::Mat map2;

    std::cout <<"K:"<<std::endl;
    std::cout<< K << std::endl<<std::endl;
    std::cout <<"D:"<<std::endl;
    std::cout << D << std::endl<<std::endl;
    std::cout <<"Size:"<<std::endl;
    std::cout << size << std::endl<<std::endl;

    cv::Mat K_new(3, 3, cv::DataType<double>::type);

    cv::fisheye::estimateNewCameraMatrixForUndistortRectify(K,D,size,E,K_new,0.0,size);

    std::cout <<"K_new:"<<std::endl;
    std::cout << K_new << std::endl;
}

Compile with

g++ fisheye_test.cpp -o fisheye_test `pkg-config --libs --cflags opencv`

Code in Python:

import cv2
import numpy as np
np.set_printoptions(suppress=True)

DIM=(1920, 1080)
K=np.array([
    [600.44477382, 0.0, 992.06425788], 
    [0.0, 578.99298055, 549.26826242], 
    [0.0, 0.0, 1.0]
])
D=np.array([
    [-0.05090103223466704],
    [0.030944413642173308],
    [-0.021509225493198905],
    [0.0043378096628297145]
])

print("K:")
print(K)
print("D:")
print(D)
print("Size:")
print(DIM)

K_new = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, DIM, np.eye(3),0)

print("K_new:")
print(K_new)

Code in Java:

K = new Mat().eye(3, 3, CvType.CV_64FC1);
K.put(0,0,600.4447738238429f);
K.put(1,1,578.9929805505851f);
K.put(0,2,992.0642578801213f);
K.put(1,2,549.2682624212172f);

Log.d(TAG, "K: " + K.dump());

D = new Mat().zeros(4, 1, CvType.CV_64FC1);
D.put(0,0,-0.05090103223466704f);
D.put(1,0,0.030944413642173308f);
D.put(2,0,-0.021509225493198905f);
D.put(3,0,0.0043378096628297145f);

Log.d(TAG, "D: " + D.dump());

K_new = new Mat().eye(3, 3, CvType.CV_32FC1);
Calib3d.fisheye_estimateNewCameraMatrixForUndistortRectify(K,D,new Size(1920,1080),new Mat().eye(3, 3, CvType.CV_32F), K_new,0.0, new Size(1920,1080));

Log.d(TAG, "K_new: " + K_new.dump());
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions