Updated April 19, 2023
Introduction to OpenCV flip()
Open CV flip () is a function that is focused upon the flipping of the two-dimensional array that has been entered by the user. This library of functions has been created to aim at real-time-based computational analysis for enabling computer vision. The function Open CV flip () has the ability to flip the two-dimensional arrays in various directions, be it flipping the array vertically, or flipping the array horizontally or on either of the axis.
Syntax:
The following is the syntax for OpenCV flip() used for the application of the flip function in the python coding language:
cv2 *. * cv *. * flip * (src, * * flip * Code * [,* dst *] * ) *
Parameter:
The following are the parameters that are present in the Open CV flip function that has specific usage to enable the function to flip in various directions the image that has been provided:
Parameters | Description |
src
dst
flip code |
[It is the source image that has been provided by the user. This parameter is essential to be entered.
The output array that is obtained which an id of a similar or same size and has the same type as the src. The parameter represents the flag that has been specified to display the manner in which the flip would occur. |
Return Value: | It outputs an image which has been flipped after using the function on the original image. |
How flip function works in Open CV?
The open CV flip function is firstly called by importing the cv2 module in the code. As the primary input that is fed into the module, the function receives the original image which has been processed by the code which further needs to be flipped output image. As the next input receives an integer value which is representative of the action that needs to be executed the flipping function. The following values can be passed as a parameter to the flip function:
- The parameter 0 is used to flip the vertically (i.e., around the presumed x-axis of the image)
- A parameter greater than zero is provided as the second value to the function. It is responsible for presently flipping the original image (i.e., around the presumed y-axis of the image)
- Parameter less than zero is provided as the second value to the function when the original image needs to be flipped around both the x and y axes.
so the user you need to the complete process of flipping and image please start by passing the parameter as 0 for the second argument Open CV flip () function, in order to rotate it around the x-axis.
Vertical_flip = cv2.flip(OI, 0)
Further, flipping the image horizontally (i.e., is around the y axis) the parameter greater than is provided as the second argument for the open CV flip ().
Horizontal_flip = cv2.flip(OI, 1)
finally, when the user needs to which to be flipped from both the axis, a parameter less than zero ( generally – 1) is provided as the second argument of the open CV flip function.
Doubleflip= cv2.flip(OI, -1)
Example of use of the OpenCV flip() Function
The following example demonstrates the utilization of the open CV flip function:
Example #1
# A program is written in Python coding language aimed at explaining the cv2.flip() in built method
# importing the class library cv2 in order perform the usage of flip ()
import cv2
# defining the variable which read the image path for the image to be processed
path_1 = r'C:\Users\data\Desktop\edu cba logo2.png'
* * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
# Reading the provided path defined image file in the default mode
src_1 = cv2.imread(path_1)
# the name of the window in which image is to be displayed
window_name1 = 'Output Image'
# Using the Open CV flip() method in order to flip the file
# Using the Flip code 0 applied to flip the image vertically
image_1 = cv2.flip(src_1, 0)
# Displaying the output image which has been flipped
cv2.imshow(window_name1, image_1)
cv2.waitKey(0)
Output:
The final output of the above image where the image has been flipped
Example #2
# A program is written in Python coding language aimed at explaining the cv2.flip() in built method
# importing the class library cv2 in order perform the usage of flip ()
import cv2
org_Img = cv2.imread('C:\Users\data\Desktop\edu cba logo2.jpg')
# Displaying the output images which has been flipped
flip_V = cv2.flip(OI, 0)
flip_H = cv2.flip(OI, 1)
flip_Both = cv2.flip(OI, -1)
cv2.imshow('Displaying the Original image entered by the user', OI)
cv2.imshow(‘ Displaying the vertically flipped image', flip_V)
cv2.imshow(' Displaying the Horizontally flipped image ', flip_H)
cv2.imshow(' Displaying the double flipped image ', flip_Both)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Conclusion
Open CV flip () function is an important inbuilt function that enables to instantaneously flips the provided images by rotating the image in respect with the x and y-axis. The function Open CV flip () has the ability to flip the two-dimensional arrays in various directions, be it flipping the array vertically, or flipping the array horizontally or on either of the axis.
Recommended Articles
We hope that this EDUCBA information on “OpenCV flip()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.