preface

After the introduction, we have mastered the basic zooming function. This post will take you through another geometry transformation in OpenCV, namely, flipping.

flip

In OpenCV, it provides the cv2.flip() function to do the flip. This function can do the flip either horizontally or vertically, or at the same time. It is defined as follows:

def flip(src, flipCode, dst=None) : 
Copy the code

SRC: original image

DST = indicates a target image of the same size and type as the original image.

FlipCode: represents rotation type

There are three rotation types, as shown in the following table:

The parameter value instructions meaning
0 Can only be 0 The X axis flip
A positive number It could be any positive number Flip it around the Y-axis
A negative number It could be any negative number Flip it around the XY axis

tips

Now that we know what the function is and what each parameter does, let’s use an example to do all the flipping.

The specific code is as follows:

import cv2

img = cv2.imread("4.jpg")
img_x = cv2.flip(img, 0)
img_y = cv2.flip(img, 1)
img_xy = cv2.flip(img, -1)
cv2.imshow("img", img)
cv2.imshow("x", img_x)
cv2.imshow("y", img_y)
cv2.imshow("xy", img_xy)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the result should look like this:

It is important to note that everything mentioned in this article is a flip, which means a 90 degree rotation, not a random rotation, so don’t confuse the rotation with the flip.