Skip to content

Beginners Opencv · Tutorials

Perspective transformation – OpenCV 3.4 with python 3 Tutorial 13

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

The Perspective Transformation is that operation that we use when we want to change the perspective of an object.

In simpler words, let’s say for example that we have a sheet of paper on the table and we’re capturing it with a camera.

Sheet paper

As you can clearly see on the picture above, the part of the paper closer to the camera is bigger than the one that is further.
That’s how our eyes see, objects closer look bigger than the one that are further.

How to do perspective transformation?

Let’s now quickly analyze the python code to do a perspective transformation.

First we need to load the image we want to transform. So let’s import the libraries and then we load the image.

import cv2
import numpy as np

img = cv2.imread("sheet_paper.JPEG")

We then need to select 4 points, in order: top-left, top-right, bottom-left, bottom-right.

From Line 6 to Line 9 I’m just drawing a circle to show the exact points we are taking.

On line 11 we create a list with this 4 points, and we’ll use this list later to apply the transformation.

cv2.circle(img, (470, 206), 5, (0, 0, 255), -1)
cv2.circle(img, (1479, 198), 5, (0, 0, 255), -1)
cv2.circle(img, (32, 1122), 5, (0, 0, 255), -1)
cv2.circle(img, (1980, 1125), 5, (0, 0, 255), -1)

pts1 = np.float32([[470, 206], [1479, 198], [32, 1122], [1980, 1125]])

On line 12 we create a new set of 4 points.
This 4 points are the size of the new window where we want to display the image transformed.

pts2 = np.float32([[0, 0], [500, 0], [0, 600], [500, 600]])

Then we apply the perspective transform to create the matrix and finally we can warp the image into using the original frame and the matrix just created.

matrix = cv2.getPerspectiveTransform(pts1, pts2)
result = cv2.warpPerspective(frame, matrix, (500, 600))

Then we can show it on the screen:

cv2.imshow("Image", img)
cv2.imshow("Perspective transformation", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is how the final result will look like:

12 comments

  1. Hello! Thank you a lot for this video, it was so usefull for me. But I have one problem. I can’t write this code for images. And I’d ask you, how can I write for cv2.imread(“”)?

    1. Doing it for images is almost the same, as even when we work with videos we work with images but they’re in a loop.

      Simply load the image with cv2.imread(“imagepath”) and use the same code without the While Loop.
      I suggest you to watch the first videos of Basic Opencv tutorial I created here https://www.youtube.com/watch?v=29vWJ1c9LX8&list=PL6Yc5OUgcoTmTGACTa__vnifNA744Cz-q so that you can understand the difference between videos and images.

  2. Hi, I have a question.
    What if we want to transform the whole image not only the page how can we do that??
    any possibility??
    because i am working on a problem where i need to transform whole image instead of part of image and i have only 4 points at the middle of the image??
    any help is appreciated.
    Thanks in advance..:)

  3. hey,
    I wanted to implement trackbar in order to determinethe points correctly.
    but it is showing only blue coloured window in result.Can anyone helP?
    import cv2
    import numpy as np
    cap=cv2.VideoCapture(0)
    def nothing(x):
    pass
    cv2.namedWindow(‘trackbar’)
    cv2.createTrackbar(‘U_L_X’,’trackbar’,0,640, nothing)
    cv2.createTrackbar(‘U_L_Y’,’trackbar’,0,480, nothing)
    cv2.createTrackbar(‘L_L_X’,’trackbar’,0,640, nothing)
    cv2.createTrackbar(‘L_L_Y’,’trackbar’,0,480, nothing)
    cv2.createTrackbar(‘L_R_X’,’trackbar’,0,640, nothing)
    cv2.createTrackbar(‘L_R_Y’,’trackbar’,0,480, nothing)
    cv2.createTrackbar(‘U_R_X’,’trackbar’,0,640, nothing)
    cv2.createTrackbar(‘U_R_Y’,’trackbar’,0,480, nothing)
    while(True):
    ret,frame=cap.read()
    #ret=cap.set(3,512)
    #ret=cap.set(4,512)
    U_L_X=cv2.getTrackbarPos(‘U_L_X’,’trackbar’)
    U_L_Y=cv2.getTrackbarPos(‘U_L_Y’,’trackbar’)
    L_L_X=cv2.getTrackbarPos(‘L_L_X’,’trackbar’)
    L_L_Y=cv2.getTrackbarPos(‘L_L_Y’,’trackbar’)
    L_R_X=cv2.getTrackbarPos(‘L_R_X’,’trackbar’)
    L_R_Y=cv2.getTrackbarPos(‘L_R_Y’,’trackbar’)
    U_R_X=cv2.getTrackbarPos(‘U_R_X’,’trackbar’)
    U_R_Y=cv2.getTrackbarPos(‘U_R_Y’,’trackbar’)

    cv2.circle(frame,(U_L_X,U_L_Y),5,(255,0,0),-1)
    cv2.circle(frame,(L_L_X,L_L_Y),5,(0,255,0),-1)
    cv2.circle(frame,(L_R_X,L_R_Y),5,(0,0,255),-1)
    cv2.circle(frame,(U_R_X,U_R_Y),5,(120,53,40),-1)
    pts1=np.float32([[U_L_X,U_L_Y],[U_R_X,U_R_Y],[L_L_X,L_L_Y],[U_R_X,U_R_Y]])
    pts2=np.float32([[0,0],[500,0],[0,600],[500,600]])
    M=cv2.getPerspectiveTransform(pts1,pts2)
    res=cv2.warpPerspective(frame,M,(500,600))
    cv2.imshow(‘original_Feed’,frame)
    cv2.imshow(‘Perspective Corrected’, res)
    k=cv2.waitKey(5)
    if(k==27):
    break
    cap.release()
    cv2.destroyAllWindows()

    This was my code,
    Thanks in advance

  4. Hey, thanks for such nice videos.
    In the above problem, I would like to use the mouse click event to determine the corner and then do the perspective change I wrote the code but the problem is I couldn’t figure out how to pass the 4 points (marked) as the parameter to the first step (matrix) can someone please help!

    here is my code
    import cv2
    import numpy as np
    circles=[]
    cap=cv2.VideoCapture(0)

    def mouse_drawing (event,x,y,param,list):

    if event == cv2.EVENT_FLAG_LBUTTON:
    circles.append((x,y))
    print(circles)

    cv2.namedWindow(‘FRAME’)
    cv2.setMouseCallback(‘FRAME’,mouse_drawing)

    while True:
    _,frame=cap.read()
    for center in circles:
    cv2.circle(frame, center, 5, (0, 255, 0),- 1)
    cv2.imshow(‘FRAME’, frame)
    #cv2.imshow(‘Result’,result)
    cv2.waitKey(1)
    k=cv2.waitKey(1)
    if k==97:
    break
    elif k==27:
    circles={}
    while True:
    _,frame1=cap.read()
    cv2.imshow(“FRAME1”,frame1)
    cap.release()
    cv2.destroyAllWindows()

  5. Hi,
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    here the matrix is obtained,is the any possibilities to get the angle of perspective from this matrix
    or
    can you please elaborate the elements in the matrix

  6. I want this perspective transformation for 1000 images. How can i do it. Do i have to do manually 1000 times by selecting four points at each time? Please help!!!

    1. Unless you’re able to identify somehow the four points automatically through some object detection algorithm, then yes, you would have to do that manually.

  7. how can I transform a video which I have downloaded in my storage, and not through the webcam? Essentially, I want to know if there is a way to use python as a video converter to convert my video’s perspective. This might be easier, but I am a newbie.

Join the discussion