Pergunta

So I have rotated an image along the y axis using WarpPerspective in the code below. I now want to find the coordinates of the corners of the image in this new rotated image? So for example if they were [0, 0], [100, 0] , [100, 100], [0, 100] before what will they be after? I thought to do this I should just multiply these first coordinates against the transformation matrix but this does not work.

float rotx, roty, rotz; // set these first
int f = 2; // this is also configurable, f=2 should be about 50mm focal length

int h = img.rows;
int w = img.cols;

float cx = cosf(rotx), sx = sinf(rotx);
float cy = cosf(roty), sy = sinf(roty);
float cz = cosf(rotz), sz = sinf(rotz);

float roto[3][2] = { // last column not needed, our vector has z=0
    { cz * cy, cz * sy * sx - sz * cx },
    { sz * cy, sz * sy * sx + cz * cx },
    { -sy, cy * sx }
};

float pt[4][2] = {{ -w / 2, -h / 2 }, { w / 2, -h / 2 }, { w / 2, h / 2 }, { -w / 2, h / 2 }};
float ptt[4][2];
for (int i = 0; i < 4; i++) {
    float pz = pt[i][0] * roto[2][0] + pt[i][1] * roto[2][1];
    ptt[i][0] = w / 2 + (pt[i][0] * roto[0][0] + pt[i][1] * roto[0][1]) * f * h / (f * h + pz);
    ptt[i][1] = h / 2 + (pt[i][0] * roto[1][0] + pt[i][1] * roto[1][1]) * f * h / (f * h + pz);
}

cv::Mat in_pt = (cv::Mat_<float>(4, 2) << 0, 0, w, 0, w, h, 0, h);
cv::Mat out_pt = (cv::Mat_<float>(4, 2) << ptt[0][0], ptt[0][1],
    ptt[1][0], ptt[1][1], ptt[2][0], ptt[2][1], ptt[3][0], ptt[3][1]);

cv::Mat transform = cv::getPerspectiveTransform(in_pt, out_pt);

cv::Mat img_in = img.clone();
cv::warpPerspective(img_in, img, transform, img_in.size());
Foi útil?

Solução

To get the points after perspective transform, you can use the perspectiveTransform function from openCV with the same transform matrix you used in warpPerspective for example

std::vector<Point2f> camera_corners,world_corners;
camera_corners.push_back(Point2f(0, 0));
camera_corners.push_back(Point2f(100, 100));
....
perspectiveTransform(camera_corners, world_corners, transform);

Now world_corners contains the warped points

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top