Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I suspect you are constructing your rvec and tvec wrong. Remember that R|t transforms a point from world coordinates (X, Y, Z) to camera coordinates.

Try a couple of simple test cases to make sure you're understanding it correctly. IE: start at no rotation, no translation, and make sure that the point projects to where you think it should. Then start adding translation to your tvec and making sure you see the relationship between tvec and which points project to the center of the camera. Then add rotation and make sure you understand that as well.

Here's the first test case to get started:

    Mat rvec, tvec, cmat;
    rvec.create(1, 3, CV_32F);
    rvec.at<float>(0) = 0;
    rvec.at<float>(1) = 0;
    rvec.at<float>(2) = 0;

    tvec.create(3, 1, CV_32F);
    tvec.at<float>(0) = 0;
    tvec.at<float>(1) = 0;
    tvec.at<float>(2) = 0;

    cmat.create(3, 3, CV_32F);
    setIdentity(cmat);
    cmat.at<float>(0, 0) = 30.0*(640.0 / 22.0);
    cmat.at<float>(1, 1) = 30.0*(480.0 / 16.0);
    cmat.at<float>(0, 2) = 640 / 2;
    cmat.at<float>(1, 2) = 480 / 2;

    vector<Point3f> world;
    world.push_back(Point3f(0, 0, 40));
    vector<Point2f> image;

    projectPoints(world, rvec, tvec, cmat, noArray(), image);
    std::cout << image[0] << "\n";