📅  最后修改于: 2023-12-03 14:42:05.710000             🧑  作者: Mango
In this tutorial, we will learn about how to display grayscale images using the imshow
function in Python. Grayscale images represent the intensity of each pixel in shades of gray.
To follow along with this tutorial, you should have a basic understanding of Python programming language and some familiarity with image processing concepts.
import matplotlib.pyplot as plt
import numpy as np
image = plt.imread('path_to_grayscale_image.png')
imshow
:plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()
imshow
function is used to display the image.cmap='gray'
sets the colormap to grayscale, ensuring that the image is displayed in shades of gray.axis('off')
removes the coordinate axis from the plot.show
displays the image on the screen.Let's consider an example to better understand how to use imshow
for grayscale images:
import matplotlib.pyplot as plt
# Load the grayscale image
image = plt.imread('example_image.png')
# Display the grayscale image
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()
In this tutorial, we have learned how to display grayscale images using the imshow
function in Python. By setting the colormap to grayscale, we can visualize the intensity of each pixel in shades of gray. This is a fundamental technique in image processing.