Коррекция перспективы opencv python

Русские Блоги

Python opencv коррекция изображения-преобразование перспективы

Коррекция изображения на основе перспективы

  1. Получите четыре вершины изображения
  2. Матрица преобразования формы
  3. Перспективное преобразование
from imutils.perspective import four_point_transform import imutils import cv2 def Get_Outline(input_dir): image = cv2.imread(input_dir) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(blurred, 75, 200) return image, gray, edged def Get_cnt (edged): # Определение контура Получите четыре угловые точки cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] docCnt = None if len(cnts) > 0: cnts = sorted(cnts, key=cv2.contourArea, reverse=True) for c in cnts: peri = cv2.arcLength (c, True) # Контуры сортируются по убыванию размера приблизительно = cv2.approxPolyDP (c, 0,02 * peri, True) # Получить приблизительный контур if len (приблизительно) == 4: # Приблизительный контур имеет четыре вершины docCnt = approx break return docCnt if __name__ == "__main__": input_dir = "21.png" image, gray, edged = Get_Outline(input_dir) docCnt = Get_cnt (edged) # Четыре угла result_img = four_point_transform (image, docCnt.reshape (4, 2)) # Выполнить четырехточечное преобразование перспективы на исходном изображении print(docCnt) # Обведите четыре угла for peak in docCnt: peak = peak[0] cv2.circle(image, tuple(peak), 10, (255, 0, 0)) cv2.imshow("original", image) cv2.imshow("gray", gray) cv2.imshow("edged", edged) cv2.imshow("result_img", result_img) cv2.waitKey(0) cv2.destroyAllWindows() 

Источник

Коррекция перспективы opencv python

OpenCV provides two transformation functions, cv.warpAffine and cv.warpPerspective, with which you can perform all kinds of transformations. cv.warpAffine takes a 2×3 transformation matrix while cv.warpPerspective takes a 3×3 transformation matrix as input.

Scaling

Scaling is just resizing of the image. OpenCV comes with a function cv.resize() for this purpose. The size of the image can be specified manually, or you can specify the scaling factor. Different interpolation methods are used. Preferable interpolation methods are cv.INTER_AREA for shrinking and cv.INTER_CUBIC (slow) & cv.INTER_LINEAR for zooming. By default, the interpolation method cv.INTER_LINEAR is used for all resizing purposes. You can resize an input image with either of following methods:

Translation

Translation is the shifting of an object’s location. If you know the shift in the (x,y) direction and let it be \((t_x,t_y)\), you can create the transformation matrix \(\textbf\) as follows:

\[M = \begin 1 & 0 & t_x \\ 0 & 1 & t_y \end\]

You can take make it into a Numpy array of type np.float32 and pass it into the cv.warpAffine() function. See the below example for a shift of (100,50):

Читайте также:  Крестики нолики на питон код

The third argument of the cv.warpAffine() function is the size of the output image, which should be in the form of **(width, height)**. Remember width = number of columns, and height = number of rows.

translation.jpg

Rotation

Rotation of an image for an angle \(\theta\) is achieved by the transformation matrix of the form

\[M = \begin cos\theta & -sin\theta \\ sin\theta & cos\theta \end\]

But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location you prefer. The modified transformation matrix is given by

\[\begin \alpha & \beta & (1- \alpha ) \cdot center.x — \beta \cdot center.y \\ — \beta & \alpha & \beta \cdot center.x + (1- \alpha ) \cdot center.y \end\]

\[\begin \alpha = scale \cdot \cos \theta , \\ \beta = scale \cdot \sin \theta \end\]

To find this transformation matrix, OpenCV provides a function, cv.getRotationMatrix2D. Check out the below example which rotates the image by 90 degree with respect to center without any scaling.

Источник

Perspective Transformation

In this blog, we will discuss what is perspective transformation and how to perform this transformation using OpenCV-Python. So, let’s get started.

What is Perspective Transformation?

As clear from the name, the perspective transformation is associated with the change in the viewpoint. This type of transformation does not preserve parallelism, length, and angle. But they do preserve collinearity and incidence. This means that the straight lines will remain straight even after the transformation.

In general, the perspective transformation can be expressed as

Here, (x’, y’) are the transformed points while (x, y) are the input points. The transformation matrix (M) can be seen as a combination of

For affine transformation, the projection vector is equal to 0. Thus, affine transformation can be considered as a particular case of perspective transformation.

Since the transformation matrix (M) is defined by 8 constants(degree of freedom), thus to find this matrix we first select 4 points in the input image and map these 4 points to the desired locations in the unknown output image according to the use-case (This way we will have 8 equations and 8 unknowns and that can be easily solved).

Читайте также:  Настройка сохранения сессий php

Once the transformation matrix is calculated, then we apply the perspective transformation to the entire input image to get the final transformed image. Let’s see how to do this using OpenCV-Python.

OpenCV

OpenCV provides a function cv2.getPerspectiveTransform() that takes as input the 4 pairs of corresponding points and outputs the transformation matrix. The basic syntax is shown below.

Once the transformation matrix (M) is calculated, pass it to the cv2.warpPerspective() function that applies the perspective transformation to an image. The syntax of this function is given below.

dst = cv . warpPerspective ( src , M , dsize [ , dst [ , flags [ , borderMode [ , borderValue ] ] ] ] )

Now, let’s take a very classic example of perspective transform to obtain a top-down, “birds-eye view” of an image. Let’s understand step by step how to perform perspective transform using the below image.

Below is the image showing the basic idea which we will perform.

First, we will select the representative points (usually the corner points) for our region of interest (ROI). This can be done manually using matplotlib as shown below.

The above code opens up an interactive window. Use the mouse pointer to obtain the 4 corner coordinates which are shown below. Points are ordered anti-clockwise as shown in the above figure.

Then we will map these 4 points to the desired locations in the unknown output image according to the use-case. Here, since we used the corner points so we will derive the width and height of the output image from these 4 points as shown below (You can also manually specify the mapping). Below I have used the L2 norm. You can use L1 also.

width_AD = np . sqrt ( ( ( pt_A [ 0 ] — pt_D [ 0 ] ) * * 2 ) + ( ( pt_A [ 1 ] — pt_D [ 1 ] ) * * 2 ) )

width_BC = np . sqrt ( ( ( pt_B [ 0 ] — pt_C [ 0 ] ) * * 2 ) + ( ( pt_B [ 1 ] — pt_C [ 1 ] ) * * 2 ) )

height_AB = np . sqrt ( ( ( pt_A [ 0 ] — pt_B [ 0 ] ) * * 2 ) + ( ( pt_A [ 1 ] — pt_B [ 1 ] ) * * 2 ) )

height_CD = np . sqrt ( ( ( pt_C [ 0 ] — pt_D [ 0 ] ) * * 2 ) + ( ( pt_C [ 1 ] — pt_D [ 1 ] ) * * 2 ) )

Now specify the mapping as shown in the image below (same image as above).

Now, compute the transformation matrix (M) using the cv2.getPerspectiveTransform() function as shown below

After calculating the transformation matrix, apply the perspective transformation to the entire input image to get the final transformed image.

Читайте также:  Render problem android studio java lang reflect invocationtargetexception

Below is the transformed image.

That’s all for this blog. In the next blog, we will discuss how to automatically choose the 4 points using the contours. Hope you enjoy reading.

If you have any doubts/suggestions please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Geometric Transformation of images using OpenCV-Python

Before reading, please refer to this blog for better understanding.

In this blog, we will discuss how to perform a geometric transformation using OpenCV-Python. In geometric transformation, we move the pixels of an image based on some mathematical formulae. This involves translation, rotation, scaling, and distortion (or undistortion!) of images. This is frequently used as a pre-processing step in many applications where the input is distorted while capturing like document scanning, matching temporal images in remote sensing and many more.

There are two basic steps in geometric transformation

  • Spatial Transformation: Calculating the spatial position of pixels in the transformed image.
  • Intensity Interpolation: Finding the intensity values at the newly calculated positions.

OpenCV has built-in functions to apply the different geometric transformations to images like translation, rotation, affine transformation, etc. You can find all the functions here: Geometric Transformations of Images

In this blog, we will learn how to change the apparent perspective of an image. This will make the image look more clear and easy to read. Below image summarizes what we want to do. See how easily we can read the words in the corrected image.

For perspective transformation, we need 4 points on the input image and corresponding points on the output image. The points should be selected counterclockwise. From these points, we will calculate the transformation matrix which when applied to the input image yields the corrected image. Let’s see the steps using OpenCV-Python

Steps:

  • Load the image
  • Convert the image to RGB so as to display via matplotlib
  • Select 4 points in the input image (counterclockwise, starting from the top left) by using matplotlib interactive window.
  • Specify the corresponding output coordinates.
  • Compute the perspective transform M using cv2.getPerspectiveTransform()
  • Apply the perspective transformation to the input image using cv2.warpPerspective()to obtain the corrected image.

Источник

Оцените статью