Flipping images

From Lofaro Lab Wiki
Jump to: navigation, search

Flipping is more straightforward than any other method. We can flip an image horizontally, vertically or both horizontally and vertically. The method used to flip an image is cv2.flip which takes two arguments. The first argument is the image you want to flip and the second one is to define the way you want to flip the image. See the code below


import cv2
import numpy as np
image = cv2.imread('hubo.jpg',0)
cv2.imshow("Original", image)
# Flip the image horizontally
flipped = cv2.flip(image, 1)
cv2.imshow("Horizontally flipped", flipped)
# Flip the image vertically
flipped = cv2.flip(image, 0)
cv2.imshow(" Vertically Flipped", flipped)
# Flip the image along both axes
flipped = cv2.flip(image, -1)
cv2.imshow("Flipped Horizontally & Vertically", flipped)
cv2.waitKey(0)


The important thing to keep in mind here is about the flip code. The second argument of the flip method will take the flip code. Giving the flip code 0 will flip the image vertically around the x-axis. The flip code 1 flips the image horizontally around y axis. Any negative number will flip both horizontally and vertically at the same time. See the images to understand how the images look like after flipping.


Flipping.jpg