Define an external camera with opencv for opengl with a world space object

I use opencv and openframework (i.e. opengl) to compute a camera (world and projection transformation matrix) from an image (and then multiple images for triangulation).

For opencv purposes, the "floor plan" becomes an object (ie a chessboard) with a 0.0.0 center of the world. The world / gender values ​​are known, so I need to get information about the forecast (distortion factors, fov, etc.) and the external coordinates of the camera.

2D input coordinates

I compared the presentation positions of these plan points in my two-dimensional image in the normalized viewing space ([0,0] left up. [1,1] bottom right).

( / ) xz, -y up, xy ( , z-up ...) opencv,

ofMatrix4x4 gWorldToCalibration(
    1, 0, 0, 0,
    0, 0, 1, 0,
    0, 1, 0, 0,
    0, 0, 0, 1
    );

1,1 ImageSize . CV_CALIB_FIX_ASPECT_RATIO|V_CALIB_FIX_K4|CV_CALIB_FIX_K5 calibrateCamera , ( 0.003).

calibrationMatrixValues FOV, 50 , , .

, ... , solvePnP, ( )

//  rot and trans output...
cv::Mat& RotationVector = ObjectRotations[0];
cv::Mat& TranslationVector = ObjectTranslations[0];

//  convert rotation to matrix
cv::Mat expandedRotationVector;
cv::Rodrigues(RotationVector, expandedRotationVector);

//  merge translation and rotation into a model-view matrix
cv::Mat Rt = cv::Mat::zeros(4, 4, CV_64FC1);
for (int y = 0; y < 3; y++)
   for (int x = 0; x < 3; x++) 
        Rt.at<double>(y, x) = expandedRotationVector.at<double>(y, x);
Rt.at<double>(0, 3) = TranslationVector.at<double>(0, 0);
Rt.at<double>(1, 3) = TranslationVector.at<double>(1, 0);
Rt.at<double>(2, 3) = TranslationVector.at<double>(2, 0);
Rt.at<double>(3, 3) = 1.0;

, ( , , , )

//  convert to openframeworks matrix AND transpose at the same time
ofMatrix4x4 ModelView;
for ( int r=0;  r<4;    r++ )
    for ( int c=0;  c<4;    c++ )
        ModelView(r,c) = Rt.at<double>( c, r ); 

(y ), .

//  swap y & z planes so y is up
ofMatrix4x4 gCalibrationToWorld = gWorldToCalibration.getInverse();
ModelView *= gCalibrationToWorld;

, ... , ...

//  invert y and z planes for -/+ differences between opencv and opengl
ofMatrix4x4 InvertHandednessMatrix(
    1,  0,  0, 0,
    0,  -1, 0, 0,
    0,  0,  -1, 0,
    0,  0,  0,  1
    );
ModelView *= InvertHandednessMatrix;

, , - , (0,0,0)

ModelView = ModelView.getInverse();

output 3D view

, . , - Y, , , ... . , , .

SO, , , , , -, , , - ? - ?

1 - ... XY (Z up), openCV. (gWorldToCalibration ). - , , , (, , ) 3D view on XY plane

Update2 - , ; , 1,1, , imageSize , , ... , ( - , z = 0 ) , ( 1,1 640 480. 640,480) enter image description here , , ...

+5
3

, , Edit 2 (ImageSize - , 1,1), , , , .

, .

0

, , - , , . , . ( , OpenCV.) , , , . , xz, . , gWorldToCalibration matrix. , , . ( - , ).

0

I think you should not take the inverse of gWorldToCalibration

ofMatrix4x4 gCalibrationToWorld = gWorldToCalibration.getInverse();

Here I posted code that does more or less what you need OpenCV- for OpenGL COS . It is in C, but should be similar to C ++.

0
source

All Articles