OpenCV Homography,转换一个点,这段代码在做什么? [英] OpenCV Homography, Transform a point, what is this code doing?

查看:96
本文介绍了OpenCV Homography,转换一个点,这段代码在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenCV计算的单应性.我目前使用此单应性通过以下功能来变换点.该功能执行我需要的任务,但是我不知道它实际上是如何工作的.

I'm working with a homography calculated by OpenCV. I currently use this homography to transform points using the function below. This function performs the task I require however I have no clue how it actually works.

任何人都可以逐行准确地解释最后三行代码背后的逻辑/理论,我知道这可以转换点x,y,但是我不清楚为什么这样:

Can anyone explain, line by line exactly, the logic/theory behind the last 3 lines of code, I understand that this transforms the point x,y but I'm unclear as to why this works:

为什么以这种方式计算Zpxpyh中的元素对应什么?

Why are Z, px and py calculated in this way, what do the elements in h correspond to?

非常感谢您的评论:)

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);

推荐答案

cvFindHomography()使用同质返回矩阵坐标:

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

Homogeneous coordinates are ubiquitous in computer graphics because they allow common operations such as translation, rotation, scaling and perspective projection to be implemented as matrix operations

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

What's happening in the code: The cartesian point p_origin_cartesian(x,y) is transformed to homogenous coordinates, then the 3x3 perspective transformation matrix h is applied and the result is converted back to cartesian coordinates 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;

这篇关于OpenCV Homography,转换一个点,这段代码在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆