7491 # Bird's eye view perspective transformation from camera calibration opencv python

Question:

## I am trying to get the bird's eye view perspective transform from camera intrinsic, extrinsic matrices and distortion coefficients. I tried using the answer from <a href="https://stackoverflow.com/questions/23275877/opencv-get-perspective-matrix-from-translation-rotation" rel="nofollow">this</a> question.The image used is the sample image left02.jpg from the opencv official github repo<a href="https://i.stack.imgur.com/vUmcl.png" rel="nofollow"><img alt="The image to be prospectively un-distored left02.jpg image from opencv sample images i.e get the bird's eye view of the image" class="b-lazy" data-src="https://i.stack.imgur.com/vUmcl.png" data-original="https://i.stack.imgur.com/vUmcl.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>I calibrated the camera and found the intrinsic, extrinsic matrices and the distortion co-efficients. I undistored the image and found the pose. To check if the params are right. <a href="https://i.stack.imgur.com/mNLBy.png" rel="nofollow"><img alt="Image after un-distortion and visualising pose" class="b-lazy" data-src="https://i.stack.imgur.com/mNLBy.png" data-original="https://i.stack.imgur.com/mNLBy.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>The equations I used to find the perspective transformation matrix are (Refer the above link):`Hr = K * R.inv() * K.inv()` where R is rotational matrix (from cv2.Rodrigues()) and K is obtained from cv2.getoptimalnewcameramatrix()``` [ 1 0 | ] Ht = [ 0 1 | -K*C/Cz ] [ 0 0 | ] ```Where `C=-R.inv()*T` Where T is translational vector from `cv2.solvePnP()` and Cz is the 3rd component of the C vector The required transformation is: `H = Ht * Hr`The code I used to construct the above equation is:```K = newcameramtx # from cv2.getoptimalnewcameramatrix() ret,rvec,tvec = cv2.solvePnP(world_points,corners2,K,dist) R,_ = cv2.Rodrigues(rvec) _,R_inv = cv2.invert(R) _,K_inv = cv2.invert(K) Hr = np.matmul(K,np.matmul(R_inv,K_inv)) C = np.matmul(-R_inv,tvec) Cz = C temp_vector = np.matmul(-K,C/Cz) Ht = np.identity(3) for i,val in enumerate(temp_vector): Ht[i] = val homography = np.matmul(Ht,Hr) warped_img =cv2.warpPerspective(img,homography,(img.shape,img.shape)) # where img is the above undistored image with visualized pose ```The resulting warped image is not correct. <a href="https://i.stack.imgur.com/PnO3L.png" rel="nofollow"><img alt="With homographic matrix = Ht*Hr" class="b-lazy" data-src="https://i.stack.imgur.com/PnO3L.png" data-original="https://i.stack.imgur.com/PnO3L.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a> If I remove the translation from the homography by using the below code```homography = Hr.copy() warped_img =cv2.warpPerspective(img,homography,(img.shape,img.shape)) ```I am getting the following image <a href="https://i.stack.imgur.com/bLlYD.png" rel="nofollow"><img alt="With homographic matrix = Hr" class="b-lazy" data-src="https://i.stack.imgur.com/bLlYD.png" data-original="https://i.stack.imgur.com/bLlYD.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>I think the above image shows that my rotational part is correct but my translation is wrong. Since the translational matrix (Ht) is an augmented matrix am unsure whether my construction of the above matrix is correct.I specifically want to figure out the bird's eye perspective transformation from the camera calibration. So, How do I correct the above equations so that I am getting the perfect bird's eye view of the chessboard imageCould anyone also please explain the math on how the above equations for Ht and Hr are derived? I don't have much exposure to Linear algebra so these equations are not very obvious to me. <strong>UPDATE:</strong>```homography = np.matmul(Ht,Hr) warped_img =cv2.warpPerspective(img,homography,(img.shape,img.shape),flags=cv2.WARP_INVERSE_MAP) ```

cv2.WARP_INVERSE_MAP flag gave me a different result