简体   繁体   English

透视变换后如何找到新的图像角点?

[英]How to find new image corner points after a perspective transform?

So I have rotated an image along the y axis using WarpPerspective in the code below. 所以我在下面的代码中使用WarpPerspective沿y轴旋转了图像。 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? 因此,例如,如果它们是[0,0],[100,0],[100,100],[0,100]之前,它们将是什么? 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());

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 要获得透视变换后的点,您可以将openCV中的perspectiveTransform函数与warpPerspective中使用的transform矩阵一起使用

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 现在world_corners包含扭曲的点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OpenCV calibrateCamera如何在不失真到提供的objectPoints之后匹配角点 - OpenCV calibrateCamera how to match corner points after undistort to objectPoints provided 找到4个特定的角点像素,并将其与warp透视图一起使用 - Find 4 specific corner pixels and use them with warp perspective 如何撤消opencv中单个点的透视变换 - How to undo a perspective transform for a single point in opencv 如何在不扭曲图像后找到要素的新位置? - How do I find the new position of a feature after undistorting the image? 如何找到检测到的图像的绘制轮廓点? - How to find points of drawcontour of a detected image? 查找在3D网格中具有随机点的多维数据集的角点 - Find corner points of a cube that has a random point in a 3D grid 将矩形转换为梯形以进行透视 - Transform Rectangle to trapezoid for perspective 如何从旋转角度计算 OpenCV 的透视变换? - How to calculate perspective transform for OpenCV from rotation angles? 如何使用OpenGL模拟OpenCV的warpPerspective功能(透视变换) - How to use OpenGL to emulate OpenCV's warpPerspective functionality (perspective transform) 使用OpenCV C ++从黑白图像中提取四角形的角(极端角点) - Extract corner (extreme corner points) of quadrangle from black/white image using OpenCV C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM