To begin any image processing task in Python, the fundamental first step is to load the image data into your script. This typically involves using libraries like `scikit-image` (`skimage`), `OpenCV` (`cv2`), or `Pillow` (`PIL`) to read image files (e.g., JPEG, PNG, TIFF) into a format that Python can manipulate, often as NumPy arrays.
In this series of post, we'll use the package scikit-image.
1. Loading a sample image of scikit-image
The script always comprise the same code: (i) import the relevant libraries/modules. Here, we have numpy, skimage (scikit-image) and matplotlib.pyplot (for display). (ii) Then, we load a built-in image as a numpy array with the module data of scikit-image
++++ Python Script ++++>++++ End of Python Script ++++
Here is the detailed explanation:
1.1. Importing Libraries
- *
import numpy as np: Imports NumPy, the foundation for handling numerical data and arrays in Python. The aliasnpis standard practice. - *
import matplotlib.pyplot as plt: Imports the plotting module from Matplotlib, used to render charts, plots, and images. The aliaspltis also standard. - *
import skimage as ski: Imports scikit-image, a dedicated image processing library built on top of NumPy.
1.2. Loading the Image
-
img = ski.data.coins(): Loads a built-in grayscale image of several coins provided by scikit-image and saves it into the variableimg. Under the hood,imgis stored as a NumPy 2D array of numbers representing pixel brightness. - To confirm that img is a numpy array (<class 'numpy.ndarray'>), you can add the line
print(type(img))
1.3. Displaying the Image
-
plt.imshow(img, cmap='gray'): Preparesimgfor display. Thecmap='gray'argument tells Matplotlib to render the array using a grayscale color map (0 = black, 255 = white). -
plt.show(): Opens a window (or renders in your notebook) to display the prepared image (Fig.1).
Fig. 1: Image coins from skimage |
2. Loading an image from a URL
The code is rather the same except we use the function imread(..) from the io module of skimage. Here is the code...
++++ Python Script ++++++++ End of Python Script ++++
3. Saving an image
TODO
4. Reading Information and Pixels
4.1. The Script
++++ Python Script ++++++++ End of Python Script ++++
4.2. Getting image information: shape, dimensions, datatype
In the script above,
- First, we load the color image clown.png (lines #5 and #6)
- From lines #8 up to #13, we get some general information about the image shape, the dimension number, and the datatype. These information are crucial to understand the data structure.
- shape: an attribute indicating the data structure. For a color image, this typically returns a tuple (height, width, channels).
- ndim: the number of dimensions (it is equivalent to len(img.shape) )
- dtype: The datatype indicating the dynamic range of the image (8-bit,16-bit, float 32-bit, etc.). Here, uint8 corresponds to 8-bit unsigned integers.
Exercise: Load the images and try to understand their data structure (the meaning of each axis) and try to display them.
- Sample: https://imagej.net/ij/images/baboon.gif ★☆☆
- Sample: https://imagej.net/ij/images/apple.tif ★☆☆
- Sample: https://imagej.net/ij/images/fluoview-multi.tif ★★☆
- Sample: https://imagej.net/ij/images/mitosis.tif ★★★
4.3. Reading pixel values
- From lines #14 up to #22, for reading a pixel value, we use the List indexing syntax. The only difference is the simplified syntax for multiple indexes (
img[y][x]is replaced byimg[y,x]). Because, we are reading pixel values of a color RGB image, we get a triplet composed of the red, green, blue components. To read each component separately (from line #18), we need to use the third axisimg[y,x,channel].
Thank you for reading.


No comments:
Post a Comment