table of Contents
Preface
After the previous introduction, we have mastered the basic zoom function. This blog post will lead everyone to learn another geometric transformation in OpenCV, that is, flip.
Flip
In OpenCV, it provides us with the cv2.flip() function to achieve flipping. This function can achieve horizontal flipping or vertical flipping. Of course, it can also flip both directions at the same time. Its definition is as follows:
def flip(src, flipCode, dst=None):
src: original image
dst=represents a target image with the same size and type as the original image.
flipCode: represents the type of rotation
There are 3 types of rotation, as shown in the following table:
Parameter value | Description | meaning |
---|---|---|
0 | Can only be 0 | X axis flip |
positive number | Can be any positive number | Flip around Y axis |
negative number | Can be any negative number | Flip around the XY axis |
Implement flip
Now that we have understood the specific definition of the function and the role of each parameter, let's use an example to achieve all the flipping effects.
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()
After running, the effect is as follows:
It should be noted that all the content mentioned in this article is flip, flip means 90 degrees, not random rotation, do not confuse the difference between rotation and flip.