OpenCV Homography, Transform a point, what is this code doing?

前端 未结 2 606
梦谈多话
梦谈多话 2020-12-23 22:35

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

相关标签:
2条回答
  • 2020-12-23 23:11

    cvFindHomography() returns a matrix using homogenous coordinates:

    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

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

    UPDATE

    In detail:

    Convert p_origin_cartesian to p_origin_homogenous:

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

    Do perspective transformation:

    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)
    

    Convert p_transformed_homogenous to p_transformed_cartesian:

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

    Your code translated:

    px = tx/tz;
    py = ty/tz;
    Z  = 1/tz;
    
    0 讨论(0)
  • 2020-12-23 23:14

    OpenCV Python implementation following @Ben answer

    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]))
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题