How to Change Perspective Image With Python and OpenCV

Hello friends, in this post I will share a little about how to apply the "Bird's Eye" method by using the OpenCV library in the Python programming language using the PyCharm IDE. So, "Bird's Eye" is a term in looking at an image with a Top-Down perspective from top to bottom.

This method is quite popularly used in "machine learning" for example in the case of "Lane Detection", "Measuring Car Distance" and of course many more.

Okay, just first open the PyCharm IDE then create a new file with the name and include an image where the object is visible from the perspective from the bottom, more or less like in the image I use.


If the image is ready, then we enter the following code

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("book.jpg"), (215, 35), 5, (0, 0, 255), -1) # top left, (550, 25), 5, (0, 0, 255), -1) # top right, (130, 370), 5, (0, 0, 255), -1) # bottom left, (630, 370), 5, (0, 0, 255), -1) # bottom right

cv2.imshow("Image", img)

The code above serves to save the picture "book.jpg" into the variable "img", then we put a red dot to adjust the location of the coordinates of the book picture corner. Actually this is not necessary, but it helps us in determining the location of the coordinates.

Note : Customize your picture.

If the above program is run it will appear like this

If it is suitable, then you just delete the code below

cv2.imshow("Image", img)

Continue with the next code

pts1 = np.float32(
        [[215,35], # top left
         [550,25], # top right
         [130,370], # bottom left
         [630,370]] # bottom right

pts2 = np.float32(
        [[0,0], # top left
         [500,0], # top right
         [0,600], # bottom left
         [500,600]] # bottom right

matrix = cv2.getPerspectiveTransform(pts1,pts2)
result = cv2.warpPerspective(img, matrix, (500,600)) # set size image based on pts2 (w = 500 h = 600)

pts1 is the coordinate point of the corner of the book, adjusting to the previous red dot.

pts2 is the coordinate point that we made to determine the width and height of the image to be produced.
Next we call the "cv2.getPerspectiveTransform" function based on the pts1 and pts2 variables stored in the matrix variable. Then in the result variable we call the "cv2.warpPerspective" function with parameters (original image, matrix and image size)

The last stage, we display the original image and the resulting image using the library "matplotlib.pyplot"

plt.title('Original Image')


The following explanation above code :

  1. Define a plot consisting of (1 row, 2 columns, placement at position 1)
  2. Ready to display the original image "img" in its original color
  3. Set image title
  4. Define a plot consisting of (1 row, 2 columns, placement at position 2)
  5. Ready to display image "result"
  6. Set image title
  7. Show plot
  8. Hold the window to keep it appearing

    Powered by Blogger.