Perspective Warping in OpenCV based on know camera orientation

后端 未结 3 1903
孤城傲影
孤城傲影 2021-01-05 14:11

I am working on a project which attempts to remove the perspective distortion from an image based on the known orientation of the camera. My thinking is that I can create a

相关标签:
3条回答
  • 2021-01-05 14:45

    You are doing several things wrong. First, you can't rotate on the x or y axis without a camera model. Imagine a camera with an incredibly wide field of view. You could hold it really close to an object and see the entire thing but if that object rotated its edges would seem to fly towards you very quickly with a strong perspective distortion. On the other hand a small field of view (think telescope) has very little perspective distortion. A nice place to start is setting your image plane at least as far from the camera as it is wide and putting your object right on the image plane. That is what I did in this example (c++ openCV)

    The steps are

    1. construct a rotation matrix
    2. center the image at the origin
    3. rotate the image
    4. move the image down the z axis
    5. multiply by the camera matrix
    6. warp the perspective

    //1
    float x =  -14 * (M_PI/180);
    float y =  20 * (M_PI/180);
    float z =  15 * (M_PI/180);
    
    cv::Matx31f rot_vec(x,y,z);
    cv::Matx33f rot_mat;
    cv::Rodrigues(rot_vec, rot_mat); //converts to a rotation matrix
    
    cv::Matx33f translation1(1,0,-image.cols/2,
                            0,1,-image.rows/2,
                            0,0,1);
    rot_mat(0,2) = 0;
    rot_mat(1,2) = 0;
    rot_mat(2,2) = 1;
    
    //2 and 3
    cv::Matx33f trans = rot_mat*translation1;
    //4
    trans(2,2) += image.rows;
    cv::Matx33f camera_mat(image.rows,0,image.rows/2,
                           0,image.rows,image.rows/2,
                           0,0,1);
    //5
    cv::Matx33f transform = camera_mat*trans;
    //6
    cv::Mat final;
    cv::warpPerspective(image, final, cv::Mat(transform),image.size());
    

    This code gave me this output

    enter image description here

    I did not see Franco's answer until I posted this. He is completely correct, using FindHomography would save you all these steps. Still I hope this is useful.

    0 讨论(0)
  • 2021-01-05 14:49

    Just knowing the rotation is not enough unless your images are taken either using a telecentric lens, or with a telephoto lens with very long focal (in which cases the images are nearly orthographic, and there is no perspective distortion).

    Besides, it's not necessary. True, you can undo the perspective foreshortening of one plane in the image by calibrating the camera (i.e. estimating the intrinsic and extrinsic parameters to form the full camera projection matrix).

    But you achieve the same result much more simply if you can identify in the image a quadrangle which is the image of a real-world square (or rectangle with known width/height ratio). If you can do that, you can trivially compute the homography matrix that maps the square (rectangle) to the quadrangle, then warp using its inverse.

    0 讨论(0)
  • 2021-01-05 14:53

    The Wikipedia page on rotation matrices shows how it is possible to combine the three basic rotation matrices into one.

    0 讨论(0)
提交回复
热议问题