Skip to content

Instantly share code, notes, and snippets.

@eprana
Created November 19, 2015 10:19
Show Gist options
  • Select an option

  • Save eprana/8abf86ebd324545dc3fc to your computer and use it in GitHub Desktop.

Select an option

Save eprana/8abf86ebd324545dc3fc to your computer and use it in GitHub Desktop.
// Retrieve pinhole from 'pinholeProjectionMatrix' attribute
// Focal is the first element
MDoubleArray intrinsicsArray;
MVGMayaUtil::getDoubleArrayAttribute(camera.getDagPath().node(), "mvg_intrinsicParams", intrinsicsArray);
LOG_INFO("mvg_intrinsicParams : ")
LOG_INFO(intrinsicsArray)
// Initialize K
openMVG::Mat3 K;
K(0,1) = 0.0;
K(0,2) = 0.0;
K(1,0) = 0.0;
K(1,2) = 0.0;
K(2,0) = 0.0;
K(2,2) = 0.0;
K(0,0) = intrinsicsArray[0];
K(1,1) = intrinsicsArray[0];
K(2,2) = 1.0;
// First method : retrieve K and t
MMatrix inclusiveMatrix = camera.getDagPath().inclusiveMatrix();
MTransformationMatrix transformMatrix(inclusiveMatrix);
// Rotation matrix
openMVG::Mat3 R;
MMatrix rotationMatrix = transformMatrix.asRotateMatrix();
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
R(i, j) = rotationMatrix[i][j];
}
openMVG::Vec3 t;
MVector translation = transformMatrix.translation(MSpace::kWorld);
for(int i = 0; i < 3; ++i)
t(i) = translation[i];
openMVG::Mat34 P;
openMVG::P_From_KRt(K, R, t, &P);
@eprana
Copy link
Author

eprana commented Nov 19, 2015

J'ai aussi essayé de calculer t en fonction de R et C :

MFnCamera fnCamera(camera.getDagPath());
openMVG::Vec3 C = TO_VEC3(fnCamera.eyePoint(MSpace::kWorld));
openMVG::Vec3 t = -R * C;

@eprana
Copy link
Author

eprana commented Nov 19, 2015

Voilà la fonction qu'on utilisait avant, avec la pinhole Camera de OpenMVG :

void triangulatePoint(const std::map<int, MPoint>& point2dPerCamera_CS,
MPoint& outTriangulatedPoint_WS)
{
size_t cameraCount = point2dPerCamera_CS.size();
assert(cameraCount > 1);
// prepare n-view triangulation data
openMVG::Mat2X imagePoints(2, cameraCount);
std::vectoropenMVG::Mat34 projectiveCameras;

// Retrieve transformation matrix of the Maya full DAG.
MVGPointCloud cloud(MVGProject::_CLOUD);
MMatrix mayaInclusiveMatrix = MMatrix::identity;
if(cloud.isValid() && cloud.getDagPath().isValid())
    mayaInclusiveMatrix = cloud.getDagPath().inclusiveMatrix();
MTransformationMatrix transformMatrix(mayaInclusiveMatrix);
MMatrix rotationMatrix = transformMatrix.asRotateMatrix();
openMVG::Mat3 rotation;
for(int i = 0; i < 3; ++i)
{
    for(int j = 0; j < 3; ++j)
        rotation(i, j) = rotationMatrix[i][j];
}

{
    std::map<int, MPoint>::const_iterator it = point2dPerCamera_CS.begin();
    for(size_t i = 0; it != point2dPerCamera_CS.end(); ++i, ++it)
    {
        MVGCamera camera(it->first);
        const MPoint& point2d_CS = it->second;

        // Retrieve the intrinsic matrix.
        //
        // K Matrix:
        // f*k_u     0      c_u
        //   0     f*k_v    c_v
        //   0       0       1
        // c_u, c_v : the principal point, which would be ideally in the centre of the image.
        //
        // In maya we keep the camera with an ideal optical center at the center
        // of the camera sensor: c_u = width/2 and c_v = height/2
        // We compensate this offset on the image plane.
        // So here, for the triangulation, we should use the ideal K.
        openMVG::Mat3 K = camera.getPinholeCamera()._K;
        std::pair<double, double> imageSize = camera.getImageSize();
        K(0, 2) = imageSize.first * 0.5;
        K(1, 2) = imageSize.second * 0.5;

        // Recompute the full Projection Matric P with the maya parent
        // transformations applied.
        MPoint cameraCenter = TO_MPOINT(camera.getPinholeCamera()._C);
        cameraCenter *= mayaInclusiveMatrix;
        const openMVG::Mat3 R = camera.getPinholeCamera()._R;
        openMVG::Vec3 C = TO_VEC3(cameraCenter);
        const openMVG::Vec3 t = -R * rotation * C;
        openMVG::Mat34 P;
        openMVG::P_From_KRt(K, R * rotation, t, &P);
        projectiveCameras.push_back(P);

        // clicked point matrix (image space)
        MPoint clickedISPosition;
        MVGGeometryUtil::cameraToImageSpace(camera, point2d_CS, clickedISPosition);
        imagePoints.col(i) = openMVG::Vec2(clickedISPosition.x, clickedISPosition.y);
    }
}

// call n-view triangulation function
openMVG::Vec4 result;
openMVG::TriangulateNViewAlgebraic(imagePoints, projectiveCameras, &result);
outTriangulatedPoint_WS.x = result(0);
outTriangulatedPoint_WS.y = result(1);
outTriangulatedPoint_WS.z = result(2);
if(result(3) == 0.0)
{
    LOG_ERROR("Triangulated point w = 0")
    return;
}
outTriangulatedPoint_WS = outTriangulatedPoint_WS / result(3);

}

@eprana
Copy link
Author

eprana commented Nov 19, 2015

Mise à jour du code :

        // Retrieve pinhole from 'pinholeProjectionMatrix' attribute
        MDoubleArray intrinsicsArray;
        MVGMayaUtil::getDoubleArrayAttribute(camera.getDagPath().node(), "mvg_intrinsicParams", intrinsicsArray);
        LOG_INFO("mvg_intrinsicParams : ")   
        LOG_INFO(intrinsicsArray)

        openMVG::Mat3 K;
        K(0,0) = intrinsicsArray[0];
        K(0,1) = 0.0;
        K(0,2) = intrinsicsArray[1];
        K(1,0) = 0.0;
        K(1,1) = intrinsicsArray[0];
        K(1,2) = intrinsicsArray[2];
        K(2,0) = 0.0;    
        K(2,2) = 1.0;

        LOG_INFO("### K ###")
        LOG_INFO(K)

        MMatrix inclusiveMatrix = camera.getDagPath().inclusiveMatrix();
        LOG_INFO("### inclusiveMatrix ###")
        LOG_INFO(inclusiveMatrix)
        MTransformationMatrix transformMatrix(inclusiveMatrix);

        openMVG::Mat3 R;        
        MMatrix rotationMatrix = transformMatrix.asRotateMatrix();
        for(int m = 0; m < 3; ++m)
        {
            for(int j = 0; j < 3; ++j)
                R(m, j) = rotationMatrix[m][j];
        }
        LOG_INFO("### R ###")
        LOG_INFO(R)

        MFnCamera fnCamera(camera.getDagPath());
        openMVG::Vec3 C = TO_VEC3(fnCamera.eyePoint(MSpace::kWorld));
        LOG_INFO("### C ###")
        LOG_INFO(C)
        openMVG::Vec3 t = -R * C;
        LOG_INFO("### t ###")
        LOG_INFO(t)

        openMVG::Mat34 P; 
        openMVG::P_From_KRt(K, R, t, &P);
        LOG_INFO("### P ###")
        LOG_INFO(P)

        projectiveCameras.push_back(P);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment