18

我正在使用由 OpenCV 计算的单应性。我目前使用这个单应性来使用下面的函数转换点。这个函数执行我需要的任务,但是我不知道它是如何工作的。

谁能准确地逐行解释最后3行代码背后的逻辑/理论,我知道这会改变点x,y,但我不清楚为什么会这样:

为什么是Zpxpy以这种方式计算,其中的元素h对应于什么?

非常感谢您的评论:)

double h[9];
homography = cvMat(3, 3, CV_64F, h);
CvMat ps1 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points1);
CvMat ps2 = cvMat(MAX_CALIB_POINTS/2,2,CV_32FC1, points2);

cvFindHomography(&ps1, &ps2, &homography, 0);

...

// This is the part I don't fully understand
double x = 10.0;
double y = 10.0;
double Z = 1./(h[6]*x + h[7]*y + h[8]);
px = (int)((h[0]*x + h[1]*y + h[2])*Z);
py = (int)((h[3]*x + h[4]*y + h[5])*Z);
4

2 回答 2

34

cvFindHomography()使用齐次坐标返回一个矩阵:

齐次坐标在计算机图形学中无处不在,因为它们允许将平移、旋转、缩放和透视投影等常见操作实现为矩阵操作

代码中发生了什么:笛卡尔点p_origin_cartesian(x,y)被转换为齐次坐标,然后应用 3x3 透视变换矩阵h并将结果转换回笛卡尔坐标p_transformed_cartesian(px,py)

更新

详细地:

转换p_origin_cartesianp_origin_homogenous

(x,y)  =>  (x,y,1)

做透视变换:

p_transformed_homogenous = h * p_origin_homogenous =

(h0,h1,h2)    (x)   (h0*x + h1*y + h2)   (tx)   
(h3,h4,h5)  * (y) = (h3*x + h4*y + h5) = (ty) 
(h6,h7,h8)    (1)   (h6*x + h7*y + h8)   (tz)

转换p_transformed_homogenousp_transformed_cartesian

(tx,ty,tz)  =>  (tx/tz, ty/tz) 

您的代码已翻译:

px = tx/tz;
py = ty/tz;
Z  = 1/tz;
于 2012-02-14T10:54:45.587 回答
5

@Ben 回答之后的 OpenCV Python 实现

p = np.array((x,y,1)).reshape((3,1))
temp_p = M.dot(p)
sum = np.sum(temp_p ,1)
px = int(round(sum[0]/sum[2]))
py = int(round(sum[1]/sum[2]))
于 2017-02-23T08:40:48.580 回答