Here the task mentioned explains
To display an image using OpenCV and Matplotlib in Python, first, read the image with cv2.imread(). Since OpenCV loads images in BGR format while Matplotlib expects RGB, convert the color format using cv2.cvtColor(). Finally, use plt.imshow() to show the image and plt.axis("off") to hide the axes for a cleaner display.
Matplotilb to Display an OpenCV image in python
import cv2
import matplotlib.pyplot as plt
# Load the image using OpenCV
image = cv2.imread("your_image.jpg") # Replace with your image path
# Convert BGR to RGB (since OpenCV loads images in BGR format)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image using Matplotlib
plt.imshow(image_rgb)
plt.axis("off") # Hide axis for a cleaner look
plt.show()
Explanation :
cv2.imread("your_image.jpg")– Loads the image.cv2.cvtColor(image, cv2.COLOR_BGR2RGB)– Converts BGR to RGB.plt.imshow(image_rgb)– Displays the image.plt.axis("off")– Removes the axis for a better visual appearance.plt.show()– Renders the image in a Matplotlib window.
Output:
- The image will be displayed in RGB format (correct colors).
- No axis labels or grid will be shown due to
plt.axis("off"). - The image will appear in a separate interactive window if running in a local environment (e.g., Jupyter Notebook, PyCharm, VS Code).
- If run in a non-GUI environment, ensure you have an image display backend set up.
More resources to learn are: