Tuesday 23 January 2024

OpenCV: flip the image

Image flipping is an image transformation operation that reverse the pixel values of an image along one or more axes to create a mirror image of the original image.

 

Horizontal flipping

Original Image       Flipped Image

1 2 3                3 2 1

4 5 6                6 5 4

7 8 9                9 8 7

 

Vertical flipping

Original Image       Flipped Image

1 2 3                7 8 9

4 5 6                4 5 6

7 8 9                1 2 3

 

Both horizontal and vertical flipping

Original Image       Flipped Image

1 2 3                9 8 7

4 5 6                6 5 4

7 8 9                3 2 1

 

Programmatically you can achieve the array flip using Numpy flip method.

 

flip_array.py

import numpy as np

# Create a sample 2D matrix (5x5)
matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])

# Perform horizontal flip (left to right)
horizontal_flipped_matrix = np.flip(matrix, axis=1)

# Perform vertical flip (top to bottom)
vertical_flipped_matrix = np.flip(matrix, axis=0)

# Perform vertical flip (top to bottom)
horizontal_and_vertical_flipped_matrix = np.flip(matrix, axis=0)
horizontal_and_vertical_flipped_matrix = np.flip(horizontal_and_vertical_flipped_matrix, axis=1)

# Print the original, horizontally flipped, and vertically flipped matrices
print("Original Matrix:\n")
print(matrix)

print("\nHorizontally Flipped Matrix:\n")
print(horizontal_flipped_matrix)

print("\nVertically Flipped Matrix:\n")
print(vertical_flipped_matrix)

print("\nHorizontal and Vertical Flipped Matrix:\n")
print(horizontal_and_vertical_flipped_matrix)

 

Output

Original Matrix:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Horizontally Flipped Matrix:

[[3 2 1]
 [6 5 4]
 [9 8 7]]

Vertically Flipped Matrix:

[[7 8 9]
 [4 5 6]
 [1 2 3]]

Horizontal and Vertical Flipped Matrix:

[[9 8 7]
 [6 5 4]
 [3 2 1]]

 

Image flipping

We can flip the image using opencv flip method.

 

Signature

cv2.flip(src, flipCode[, dst])

 

Following table summarizes the parameters of flip method.

 

Parameter

Description

src

Source image that we want to flip

flipCode

It is an integer,

0: Flip the array horizontally (around the vertical axis).

1: Flip the array vertically (around the horizontal axis).

-1: Flip the array both horizontally and vertically.

dst

It is an optional parameter, specifies the destination array where we want to store the result in a pre-allocated array

 

Find the below working application.

 

flip_image.py

 

import cv2 as cv

# Read the image as matrix of pixels
image = cv.imread('buildings.png')
horizontal_flip = cv.flip(image, 0)
vertical_flip = cv.flip(image, 1)
horizontal_and_vertical_flip = cv.flip(image, -1)

# Display the image in new window
cv.imshow('Buildings', image)
cv.imshow('horizontal_flip', horizontal_flip)
cv.imshow('vertical_flip', vertical_flip)
cv.imshow('horizontal_and_vertical_flip', horizontal_and_vertical_flip)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Original image 

 


Horizontal flip


 

Vertical flip


Both horizontal and vertical flip



 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment