【发布时间】:2014-04-09 14:43:19
【问题描述】:
所以我在下面的代码中使用 WarpPerspective 沿 y 轴旋转了图像。我现在想在这个新的旋转图像中找到图像角的坐标?因此,例如,如果它们是 [0, 0], [100, 0] , [100, 100], [0, 100] 在它们之后会是什么?我想这样做我应该将这些第一个坐标与变换矩阵相乘,但这不起作用。
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());
【问题讨论】:
-
您必须将这些点(扩展)与您的变换相乘(或者可能相反,取决于经线的方向)。阅读有关齐次坐标和单应性的信息。
-
所以将每个输入图像角点与来自 getPerspectiveTransform 的变换矩阵相乘?或者至少是类似的东西?
-
您应该使用与用于变形的矩阵相同的矩阵:
homography*[x,y,1] = [x',y',z']现在您必须“去均质化”:warped_xy = [x'/z' , y'/z']。也许也有一个 openCV 函数。 -
@Fionn 查看答案here,可能会有所帮助。
标签: c++ opencv matrix computer-vision