Python - Perspective transform for OpenCV from a rotation angle

前端 未结 1 1211
不思量自难忘°
不思量自难忘° 2021-02-14 21:08

I\'m working on depth map with OpenCV. I can obtain it but it is reconstructed from the left camera origin and there is a little tilt of this latter and as you can

相关标签:
1条回答
  • 2021-02-14 21:37

    I have got a rough solution in place. You can modify it later.

    I used the mouse handling operations available in OpenCV to crop the region of interest in the given heatmap.

    (Did I just say I used a mouse to crop the region?) Yes, I did. To learn more about mouse functions in OpenCV SEE THIS. Besides, there are many other SO questions that can help you in this regard.:)

    Using those functions I was able to obtain the following:

    Now to your question of removing the tilt. I used the homography principal by taking the corner points of the image above and using it on a 'white' image of a definite size. I used the cv2.findHomography() function for this.

    Now using the cv2.warpPerspective() function in OpenCV, I was able to obtain the following:

    Now you can the required scale to this image as you wanted.

    CODE:

    I have also attached some snippets of code for your perusal:

    #First I created an image of white color of a definite size
    back = np.ones((435, 379, 3)) # size
    back[:] = (255, 255, 255)     # white color
    

    Next I obtained the corner points pts_src on the tilted image below :

    pts_src = np.array([[25.0, 2.0],[403.0,22.0],[375.0,436.0],[6.0,433.0]])
    

    I wanted the points above to be mapped to the points 'pts_dst' given below :

    pts_dst = np.array([[2.0, 2.0], [379.0, 2.0], [379.0, 435.0],[2.0, 435.0]])
    

    Now I used the principal of homography:

    h, status = cv2.findHomography(pts_src, pts_dst)
    

    Finally I mapped the original image to the white image using perspective transform.

    fin = cv2.warpPerspective(img, h, (back.shape[1],back.shape[0]))
    # img -> original tilted image.
    # back -> image of white color.
    

    Hope this helps! I also got to learn a great deal from this question.

    Note: The points fed to the 'cv2.findHomography()' must be in float. For more info on Homography , visit THIS PAGE

    0 讨论(0)
提交回复
热议问题