OpenCV Homography,转换点,这段代码在做什么?

时间:2012-02-14 10:50:51

标签: opencv matrix transform perspective homography

我正在使用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);

2 个答案:

答案 0 :(得分:28)

cvFindHomography()使用homogenous coordinates返回矩阵:

  

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

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

<强>更新

详细说明:

p_origin_cartesian转换为p_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_homogenous转换为p_transformed_cartesian

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

您的代码已翻译:

px = tx/tz;
py = ty/tz;
Z  = 1/tz;

答案 1 :(得分:0)

@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]))