NumPy (also known as “Numerical Python”) is one of the most crucial fundamental packages in Python for numerical computing. The core data structure of NumPy is the ndarray (N-dimensional array), which is a homogeneous collection of elements of the same data type. These arrays can be of any dimension, such as 1D, 2D, or even higher-dimensional arrays. NumPy provides a vast collection of mathematical functions to operate on these N-dimensional arrays efficiently.
Images in scikit-image are represented as NumPy ndarrays (multidimensional arrays). The scikit-image library is built on top of NumPy and it uses the NumPy arrays to represent images. Hence, the scikit-image library can perform various image-processing tasks effectively.
Representing images as NumPy arrays
Representing images as NumPy arrays, provide a convenient and efficient way to store and manipulate image data.
Here, the dimensions of the NumPy array correspond to the image dimensions, such as height, width, and color channels. For grayscale images, the array is typically two-dimensional (height x width). For color images, the array is three-dimensional (height x width x 3), where the last dimension represents the Red, Green, and Blue color channels.
Example 1
The following example demonstrates how a color image can be represented as a NumPy array in scikit-image.
from skimage import io # Read an image as a grayscale image img_array = io.imread(''Images/Dog.jpg'') # Display image properties from the image array print(''The following are the properties of the loaded image:'') print("Data type of the image object:", type(img_array)) print("Image shape:", img_array.shape) print("Image data type:", img_array.dtype)
Input Image
Output
The following are the properties of the loaded image: Data type of the image object: <class ''numpy.ndarray''> Image shape: (479, 500, 3) Image data type: uint8
Example 2
Let”s see the NumPy array representation of a grayscale image.
from skimage import io # Read an image as a grayscale image img_array = io.imread(''Images/dog.jpg'', as_gray=True) # Display image properties from the image array print(''The following are the properties of the loaded image:'') print("Data type of the image object:", type(img_array)) print("Image shape:", img_array.shape) print("Image data type:", img_array.dtype)
Output
The following are the properties of the loaded image: Data type of the image object: <class ''numpy.ndarray''> Image shape: (479, 500) Image data type: float64
Indexing and Slicing
NumPy”s indexing and slicing feature can be used to access and manipulate image data. Croping images, selecting specific color channels, or applying operations to specific regions within the image can be possible by using NumPy”s flexible indexing and slicing syntax.
Example
The following example demonstrates how the indexing and slicing syntax of NumPy can be used to modify an image in Scikit-image.
from skimage import io # Read an image as a grayscale image img_array = io.imread(''Images/Tajmahal.jpg'') # Get the value of the pixel at the 10th row and 20th column pixel_value = img_array[10, 20] print(''The pixel at the 10th row and 20th column of the image array'', pixel_value) # Set value 0 to the pixel at the 3rd row and 10th column img_array[3, 10] = 0 # Select a region in the image roi = img_array[100:200, 200:300] # Set the pixel values in the selected region to red (255, 0, 0) roi[:] = (255, 0, 0) # Display the modified image io.imshow(img_array) io.show()
Input Image
Output
Running the above code gives us the following result −
The pixel at the 10th row and 20th column of the image array [ 81 97 110]
In addition it generates the following image −