Warp perspective and Transform OpenCV python

warp perspective and transform opencv python

In this tutorial, I will show you image warping in OpenCV. Perspective view warping is to convert or perspective correction of images from angle to birds eye view transform. To convert image to birds eye view I will be using warp perspective OpenCV function.

Find points for perspective correction opencv

For perspective transformation in OpenCV we need to know each point (pixel values of each point). For example red, green, blue and black point like above image. So we need to know each point to convert image to birds eye view.

To know point value (pixel value) of each desired point, you can open any image in paint in windows and move your cursor to check pixel value of that point at the bottom left.

image warping opencv

Now let’s say you want to warp perspective of 8 of spade from above image. For that you have to note pixel values of four ending points of 8 of spades from paint. As shown in image I am noting each point as red, green, blue and black.

  • red: (147,183) left top point of that particular card
  • green: (181,77) right top point of that particular card
  • blue: (461,100) left bottom point of that particular card
  • black: (456,215) right bottom point of that particular card

Create matrix

Now lets create a matrix of above points for perspective view warping in OpenCV.

# Pixel values in original image
red_point = [147,150]
green_point = [256,182]
black_point = [119,453]
blue_point = [231,460]

# Create point matrix
point_matrix = np.float32([red_point,green_point,black_point, blue_point])

Draw Bounding points for image warping OpenCV

Now let’s draw circle for each bounding points, for which we are going to do image warping OpenCV.

# Draw circle for each point
cv2.circle(img,(red_point[0],red_point[1]),10,(0,0,255),cv2.FILLED)
cv2.circle(img,(green_point[0],green_point[1]),10,(0,255,0),cv2.FILLED)
cv2.circle(img,(blue_point[0],blue_point[1]),10,(255,0,0),cv2.FILLED)
cv2.circle(img,(black_point[0],black_point[1]),10,(0,0,0),cv2.FILLED)

If you are have any confusion understanding above lines of code to draw circle, I will suggest you to read my previous articles.

Related article:

Convert image to birds eye view python

perspective transform opencv

Above image is showing what we are going to to in this article. This is called birds eye view transform.

We need to tell which point needs to be converted to which point. You can see in above picture I want to convert red point to red point, green points to green and so on for other points.

For example if we want an (250,350) output image, then value of red point should be: [0,0], similarly for other points:

  • Converted red point: [0,0]
  • Converted green point: [250,0]
  • Converted black point: [0,350]
  • Converted blue point: [250,350]
# Output image size
width, height = 250,350

# Desired points value in output images
converted_red_pixel_value = [0,0]
converted_green_pixel_value = [width,0]
converted_black_pixel_value = [0,height]
converted_blue_pixel_value = [width,height]

# Convert points
converted_points = np.float32([converted_red_pixel_value,converted_green_pixel_value,
                               converted_black_pixel_value,converted_blue_pixel_value])

Perspective correction OpenCV python

As we have defined all points now let’s do perspective correction or birds eye view transform.

# perspective transform opencv
perspective_transform = cv2.getPerspectiveTransform(point_matrix,converted_points)
# perspective view warping opencv
img_Output = cv2.warpPerspective(img,perspective_transform,(width,height))

Full code Warp perspective and transform

Now putting all together:

import cv2
import numpy as np

img = cv2.imread('data/cards_test.jpg')

# Pixel values in original image
red_point = [147,150]
green_point = [256,182]
black_point = [119,453]
blue_point = [231,460]

# Create point matrix
point_matrix = np.float32([red_point,green_point,black_point, blue_point])

# Draw circle for each point
cv2.circle(img,(red_point[0],red_point[1]),10,(0,0,255),cv2.FILLED)
cv2.circle(img,(green_point[0],green_point[1]),10,(0,255,0),cv2.FILLED)
cv2.circle(img,(blue_point[0],blue_point[1]),10,(255,0,0),cv2.FILLED)
cv2.circle(img,(black_point[0],black_point[1]),10,(0,0,0),cv2.FILLED)

# Output image size
width, height = 250,350

# Desired points value in output images
converted_red_pixel_value = [0,0]
converted_green_pixel_value = [width,0]
converted_black_pixel_value = [0,height]
converted_blue_pixel_value = [width,height]

# Convert points
converted_points = np.float32([converted_red_pixel_value,converted_green_pixel_value,
                               converted_black_pixel_value,converted_blue_pixel_value])

# perspective transform
perspective_transform = cv2.getPerspectiveTransform(point_matrix,converted_points)
img_Output = cv2.warpPerspective(img,perspective_transform,(width,height))

cv2.imshow("Original Image", img)
cv2.imshow("Output Image", img_Output)
cv2.waitKey(0)

If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.

Leave a Comment

Your email address will not be published. Required fields are marked *