Jay*_*son 17 opencv transform matrix homography perspective
我正在使用OpenCV计算的单应性.我目前使用此单应性来使用下面的函数转换点.这个函数执行我需要的任务但是我不知道它是如何工作的.
任何人都可以一行一行地解释最后3行代码背后的逻辑/理论,我知道这会改变点x,y但是我不清楚它为什么会起作用:
为什么Z
,px
并py
以这种方式计算,元素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);
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 30
cvFindHomography()
使用同质坐标返回矩阵:
同构坐标在计算机图形中无处不在,因为它们允许将平移,旋转,缩放和透视投影等常见操作实现为矩阵运算
发生了什么事中的代码:笛卡尔点p_origin_cartesian(x,y)
变换为均匀的坐标,然后3×3透视变换矩阵h
被应用并且将结果转换回直角坐标p_transformed_cartesian(px,py)
.
UPDATE
详细地:
转换p_origin_cartesian
为p_origin_homogenous
:
(x,y) => (x,y,1)
Run Code Online (Sandbox Code Playgroud)
做透视转换:
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)
Run Code Online (Sandbox Code Playgroud)
转换p_transformed_homogenous
为p_transformed_cartesian
:
(tx,ty,tz) => (tx/tz, ty/tz)
Run Code Online (Sandbox Code Playgroud)
您的代码已翻译:
px = tx/tz;
py = ty/tz;
Z = 1/tz;
Run Code Online (Sandbox Code Playgroud)
遵循@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]))
Run Code Online (Sandbox Code Playgroud)