Learn to Flip Image in OpenCV Python Horizontally and Vertically using cv2.flip()

Introduction

In this tutorial, we are going to show how we can flip an image in OpenCV Python using cv2.flip() function. Flipping is a very common image transformation technique that produces mirror reversal of the image horizontally or vertically. We will show you examples of how to flip the image horizontally and vertically by using cv2.flip() function of OpenCV.

Flip Image in OpenCV Python : cv2.flip()

There is a function cv2.flip() in OpenCV Python that can be used to flip the image effortlessly. Let us first understand its syntax below –

Syntax

cv2.flip(src, dst, flipCode)

  • src – Array of Input Image
  • dst – Output Image Array of same size and type as src
  • flipCode – This determines the type of flip you want to apply to the image. A negative number like -1 produces a Vertical flip, a Positive number like 1 produces a Horizontal flip, and 0 produces both horizontal and vertical flip.

Examples of cv2.flip() in Python OpenCV

Import OpenCV Library

First of all, let us import the OpenCV library as shown below-

In [0]:

import cv2

Read Sample Image and Display

Next, we will read a sample image for our example and display it

In [1]:

window_name='Dog Image'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

OpenCV Python Flip Image Example

Example – 1: Flip the Image Horizontally with cv2.flip()

To flip the image horizontally, i.e. along the y-axis we need to pass a positive value of flipCode. In this example, we pass flipCode = 1 along with the image name.

In [2]:

horizontal_flip = cv2.flip(img, 1)

window_name='OpenCV Python Flip Image Horizontally'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,horizontal_flip)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

OpenCV Python cv2.flip() Example of Horizontal Flip

Example – 2: Flip the Image Vertically with cv2.flip()

To flip the image vertically, i.e. along the x-axis we need to provide a zero to flipCode. In this example, we pass flipCode = 0 along with the image name.

In [3]:

vertical_flip = cv2.flip(img, 0)

window_name='OpenCV Python Flip Image Vertically'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,vertical_flip)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

OpenCV Python cv2.flip() Example of Vertically Flip

Example – 3: Flip the Image Both Horizontally and Vertically with cv2.flip()

To flip the image both vertically and horizontally, i.e. along both the x and y axis, we need to provide a negative value to flipCode. In this example, we assign flipCode = -1 along with the image name.

In [4]:

vertical_horizontal_flip = cv2.flip(img, -1)

window_name='OpenCV Python Flip Image Both Vertically and Horizontally'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name,vertical_horizontal_flip)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

OpenCV Python cv2.flip() Example of Horizontal and Vertical Flip

Conclusion

Hope you found this short tutorial on how to flip images in OpenCV quite helpful. We first understood the syntax of the cv2 flip() function along with different types of examples of flipping an image vertically and horizontally.

Reference – OpenCV documentation

 

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *