2. OpenCV geometric transformation

2. OpenCV geometric transformation2.1. Image scaling2.2. Image cropping2.3. Image translation2.4.Image mirroring

Before running the sample program, you need to switch to the directory where the code is located. SSH connects the car, runs at the terminal,

2.1. Image scaling

After the car terminal switches to the directory where the code is located, run the program,

Terminal output the size of the original image and the reduced image (representing the width, height and number of channels of the image, respectively),

1685002333845

Open the image and you can see the difference in size,

1685002275425

2.2. Image cropping

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685003397012

2.3. Image translation

After the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685003830986

How to get the transformation matrix M? Here is an example,

Convert the original image src to the target image dst through the transformation matrix M:

dst(x, y) = src(M11x + M12y+M13, M21x+M22y+M23)

Move the original image src by 100 to the right and 50 pixels down, and the corresponding relationship is as follows:

dst(x, y) = src(x+100, y+50)

To complete the above expression, namely:

dst(x, y) = src(1·x + 0·y + 100, 0·x + 1·y + 50)

According to the above expression, it can be determined that the values of each element in the corresponding transformation matrix M are:

M11=1, M12=0, M13=100, M21=0, M22=1, M23=50.

Substituting the above values into the transformation matrix M, we get:

M = [[1,0,100],[0,1,50]

2.4.Image mirroring

There are two kinds of image image transformation: horizontal image and vertical image. The horizontal mirror takes the vertical center line of the image as the axis, and swaps the pixels of the image, that is, switches the left half of the image and the right half of the image. Vertical mirroring is based on the horizontal center line of the image as the axis, the upper part of the image and the lower part of the image.

Transformation principle:

Set the width of the image to width and the length to height. (x,y) is the coordinate after transformation, (x0,y0) is the coordinate of the original image,

Forward mapping: x=width-x0-1,y=y0

Backward mapping: x0=width-x-1,y0=y

Map up: x=x0,y=height-y0-1

Downward mapping: x0=x, y0=height-y-1

Summary: In the horizontal image transformation, the entire image is traversed, and then each pixel is processed according to the mapping relationship. In fact, the horizontal mirror transformation is to change the column of the image coordinate to the right, and the right column to the left, which can be transformed in column units. The same is true of the vertical mirror image transformation, which can be transformed in units of behavior.

The following takes vertical transformation as an example, after the car terminal switches to the directory where the code is located, run the program,

The resulting image is shown here,

1685004046699