This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging

If you have any ideas or techniques you’d like to share, feel free to leave them in the comments section.

This blog will learn about the method of image translation in OpenCV, the core method of learning is warpAffine.

Translation of OpenCV images

Every time I learn something new, my eraser is going to do a massive search and find the part that fits my understanding.

Then piece it together into a small system and try to get a preliminary understanding of the content.

Today this 1 hour, the core to learn is the image of translation, open a picture on the computer, the realization of movement is very simple, but in the code, there are some new concepts.

The first new concept encountered when retrieving information related to OpenCV image translation is affine transformation.

Every time to see such a mathematical name, must be cool in the heart, as a mathematical small white, and tremble.

Check it out and see what it says in the encyclopedia.

After reading some related introduction in the figure above, we do not have too deep understanding of this concept. In line with the idea of using it first and supplementing the theory of learning, we first realize the image translation.

In the process of learning, when you encounter some conceptual things, you can first apply them, and then in the subsequent in-depth learning process, gradually improve them.

Cv2.warpaffine () affine transformation

Based on this method to find the relevant data, get the basic concept of affine transformation, from two-dimensional coordinates to two-dimensional coordinates between the linear transformation, and to maintain the two-dimensional graph “straightness” and “parallelism”.

Affine transformation involves translation, scaling, rotation, rotation, and shearing.

The syntax structure of this method is as follows:

dst = cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
Copy the code

Parameter description:

  • SRC: Input an image
  • M: 2*3 Transformation matrix
  • Dsize: the size of the output image. The format is (cols,rows), width corresponds to COLs, height corresponds to Rows
  • Flags: Optional, combination of interpolation methods (int), default INTER_LINEAR
  • BorderMode: Optional, boundary pixel mode (type int), default value: BORDER_CONSTANT
  • BorderValue: Optional, boundary filling value; By default, the default value Scalar() is 0

Official information available in English is as follows:

Interpolation modes (flags) have the following types:

  • INTER_LINEAR interpolation
  • INTER_NEAREST nearest interpolation
  • INTER_AREA Area interpolation
  • INTER_CUBIC cubic spline interpolation
  • INTER_LANCZOS4 Lanczos interpolation

In general, the first three wrapAffine parameters, warpAffine(img,M,(rows,cols)), are used to achieve the basic affine transform effect, but in this case, black edges will appear.

The last parameter is the borderValue, which is the color the border is filled with. It defaults to black and can be set to any other color.

Specific code

Image translation is a shift in the position of the image. Before translation, it is necessary to transform matrix M,, where TX and ty represent the displacement in the direction of (x,y), as shown below:

The test code is as follows, with the important parts explained in the comments.

import numpy as np
import cv2 as cv

img = cv.imread('10.jpg'.1)
rows, cols, channels = img.shape
# [1,0,100] moves right 100, [0,1,50] moves down 50
M = np.float32([[1.0.100], [0.1.50]])
res = cv.warpAffine(img, M, (cols, rows))
cv.imshow("img", res)
cv.waitKey()
Copy the code

Note: The third parameter of the CV. WarpAffine method is the size of the output image, which should be of the form (width, height). Remember width = number of columns (Cols), height = number of rows (Rows).

The following is the result of the code run.

Description of the borderMode parameter value: This section deals with edge handling, and the instructions found in the manual process point to the BorderTypes. Read, is still some super class, can first record the relevant content, in the future review, reference address.

All values are listed as follows:

  • BORDER_CONSTANT = 0
  • BORDER_REPLICATE = 1
  • BORDER_REFLECT = 2
  • BORDER_WRAP = 3
  • BORDER_REFLECT_101 = 4
  • BORDER_TRANSPARENT = 5
  • BORDER_REFLECT101 = BORDER_REFLECT_101
  • BORDER_DEFAULT = BORDER_REFLECT_101
  • BORDER_ISOLATED = 16

BorderValue: The fill value when the image boundary is processed as BORDER_CONSTANT.

Test code is as follows:

import numpy as np
import cv2 as cv

img = cv.imread('10.jpg'.1)
rows, cols, channels = img.shape
M = np.float32([[1.0.100], [0.1.50]])
res = cv.warpAffine(img, M, (cols, rows),
                    borderMode=cv.BORDER_CONSTANT, borderValue=(200.66.66))
cv.imshow("img", res)
cv.waitKey()
Copy the code

OpenCV end

One hour has passed. Have you mastered the knowledge of OpenCV in Python?

In your spare time, you can subscribe to the eraser crawler hundred example course to learn crawler knowledge.