Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to perform image transpose using OpenCV Python?
OpenCV represents images as NumPy ndarrays, allowing us to use array operations on images. Image transpose in OpenCV flips an image along its main diagonal rows become columns and columns become rows. We use cv2.transpose() to perform this operation.
Syntax
The syntax for transposing an image is ?
cv2.transpose(src)
Parameters
- src ? Input image (NumPy array)
Return Value
Returns the transposed image as a NumPy array.
Basic Image Transpose
Let's create a simple example to demonstrate image transposition ?
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Create a sample image for demonstration
img = np.zeros((200, 300, 3), dtype=np.uint8)
cv2.rectangle(img, (50, 30), (150, 100), (255, 0, 0), -1) # Blue rectangle
cv2.circle(img, (200, 150), 40, (0, 255, 0), -1) # Green circle
print(f"Original image shape: {img.shape}")
# Transpose the image
transposed_img = cv2.transpose(img)
print(f"Transposed image shape: {transposed_img.shape}")
# Display both images
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
ax1.set_title("Original Image")
ax1.axis('off')
ax2.imshow(cv2.cvtColor(transposed_img, cv2.COLOR_BGR2RGB))
ax2.set_title("Transposed Image")
ax2.axis('off')
plt.tight_layout()
plt.show()
Step-by-Step Process
Follow these steps to transpose an image ?
- Import required libraries (OpenCV, NumPy, Matplotlib)
- Read or create an input image
- Apply
cv2.transpose()to the image - Display or save the result
Complete Example with File Input
Here's a complete example that reads an image file and transposes it ?
import cv2
import matplotlib.pyplot as plt
# Read the input image
img = cv2.imread('/path/to/your/image.jpg')
if img is None:
print("Error: Could not load image")
else:
print(f"Original image dimensions: {img.shape}")
# Transpose the image
transposed_img = cv2.transpose(img)
print(f"Transposed image dimensions: {transposed_img.shape}")
# Display both images side by side
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(transposed_img, cv2.COLOR_BGR2RGB))
plt.title("Transposed Image")
plt.axis('off')
plt.tight_layout()
plt.show()
Key Points
- Image transpose swaps width and height dimensions
- A 300×200 image becomes 200×300 after transpose
- The operation flips the image along its main diagonal
- Color channels remain unchanged
- This is different from image rotation or flipping
Comparison with NumPy Transpose
You can also use NumPy's transpose method for the same result ?
import cv2
import numpy as np
# Sample image
img = cv2.imread('/path/to/image.jpg')
# Method 1: Using OpenCV
cv2_transposed = cv2.transpose(img)
# Method 2: Using NumPy (transpose axes 0 and 1)
numpy_transposed = np.transpose(img, (1, 0, 2))
# Both methods produce identical results
print("Results are identical:", np.array_equal(cv2_transposed, numpy_transposed))
Conclusion
Use cv2.transpose() to flip images along their main diagonal, effectively swapping rows and columns. This operation changes the image dimensions and is useful for specific image processing tasks that require axis manipulation.
