opencv perspective transformation, extract feature image

content

basic introduction

Introduction to cv2.getPerspectiveTransforms

Introduction to cv2.warpPerspective

Find feature images

Complete code and running effect 


basic introduction

        Note: The premise of this article is to have learned image affine transformation

        Using opencv's perspective transformation allows us to simply extract the desired information. We only need to know the 4 points of the original image. Through these 4 points and the coordinates of the image we want to generate, we can calculate the M matrix, and then pass cv. The warpPerspective method can extract the picture.

        Let's take a look at the effect first, and then use this technology to extract the book in the picture


Introduction to cv2.getPerspectiveTransforms

        Above we know that we need to use the coordinates in the 4 original images and the coordinates of the newly generated image to obtain the M matrix by operation. It is too troublesome to calculate by hand. We use the function provided by opencv to complete it. This function returns the M matrix

def getPerspectiveTransform(src, dst, solveMethod=None)
  • src: 4 point coordinates on the original image
  • dst: 4 point coordinates of the generated image
  • solveMethod: matrix decomposition method, passed to cv2.solve(DecompTypes) to solve linear equations or solve least squares problem, the default value is None, which means DECOMP_LU is used. For details, refer to the official website

        A schematic diagram is given below.


Introduction to cv2.warpPerspective

        This method is very similar to warpPerspective and is explained below

def warpPerspective(src, M, dsize, dst=None, flags=None, borderMode=None, borderValue=None)
  • src: input image
  • M: Operation matrix
  • dsize: The size of the matrix after the operation, that is, the size of the output image
  • dst: output image
  • flags: a combination of interpolation methods, the same as the interpolation in the resize function, you can view cv2.resize
  • borderMode: pixel extrapolation method, please refer to the official website for details
  • borderValue: borderValue value to use in case of constant border; by default it is 0 

Find feature images

        Output the image through matplotlib, and then roughly observe the 4 coordinates of the feature image

        Here I give 4 coordinates of upper left, upper right, lower left and lower right by observation, (190, 240), (505, 160), (360, 670), (768, 490)

src = np.float32([[190, 240], [505, 160], [360, 670], [768, 490]])

        We get the 4 coordinates of the feature image, and then give the 4 coordinates of the newly generated image. What I want to generate is an image with a height of 640 and a width of 480, so the newly generated upper left, upper right, lower left, lower right 4 coordinates are (0, 0), (480, 0), (0, 640), (480, 640)

dst = np.float32([[0, 0], [480, 0], [0, 640], [480, 640]])

Complete code and running effect 

import cv2
import numpy as np

img = cv2.imread('../images/book.jpg')
# 设置特征图像和生成图像的坐标
src = np.float32([[190, 240], [505, 160], [360, 670], [768, 490]])
dst = np.float32([[0, 0], [480, 0], [0, 640], [480, 640]])
# 通过运算得出M矩阵
M = cv2.getPerspectiveTransform(src, dst)
# 提取特征图片
book = cv2.warpPerspective(img, M, (480, 640))

cv2.imshow('img', cv2.resize(img, (480, 640)))
cv2.imshow('book', book)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/123961645