Monday, September 28, 2026

Geometrical Transforms in Python

 

In the pre-processing step of an image processing pipeline, it is often common to crop, resize, flip images. Here is how to do it in Python...


In this series of post, we'll use the package scikit-image.

1. Crop

To crop an image, we use the slicing and indexing syntax available for numpy arrays.

++++ Python Script ++++++++ End of Python Script ++++

The first lines of the script contain the imports:

1.1. Loading the image coins
  •  import numpy as np: Imports NumPy, the foundation for handling numerical data and arrays in Python. The alias np is standard practice.
  •  import matplotlib.pyplot as plt: Imports the plotting module from Matplotlib, used to render charts, plots, and images. The alias plt is also standard.
  •  import skimage as ski: Imports scikit-image, a dedicated image processing library built on top of NumPy.

Then, we load the coins image in skimage with  img = ski.data.coins() as a numpy array.




Fig. 1: Image coins from skimage
1.2. Cropping the Image
  • For cropping the image (line #7), we define a bounding box from its top left corner whose XY-coordinates (x0,y0) are (180,100) and the bottom right corner (x1,y1) are equal to (230,150). 
  • However, an image is defined by a 2D numpy array where the first axis corresponds to the Y and the second axis to the X. Thus, to define the bounding box with array indexing, we are first defining the Y range and then the X range. So, we need to write the box coordinates like this.... img[y0:y1, x0:x1].
1.3. Exercises
  1. Exercise: Try to crop the same coin giving the coin center (205,125) and the diameter 25.
  2. Exercise: Try to crop the coin giving the top left corner (180,100) and the box size (50,50) 

2. Flip

For flipping, we use the step in the indexing syntax (as a reminder, for indexing, the syntax is arr[start:stop:step]. By assigning -1 to the step, we reverse/flip the numpy array. For example..

 a = np.array([0,1,2,3])
 b = a[::-1] # [3,2,1,0]
 
Note: This syntax also works with List
 
Here is the code for flipping the image along Y and X, and both... 

++++ Python Script ++++++++ End of Python Script ++++

 ... and the corresponding montage.

Fig. 3: A) Original, B) Flip vertically, C) Flip horizontally, D) Flip along both axes

3. Padding

For padding, we use a dedicated function of numpy 'pad(..)'

++++ Python Script ++++++++ End of Python Script ++++

Various modes are available for the padding:

  1.     ‘constant’ (default): Pads with a constant value.
  2.     ‘edge’: Pads with the edge values of array.
  3.     ‘linear_ramp’:  Pads with the linear ramp between end_value and the array edge value.
  4.     ‘maximum’:  Pads with the maximum value of all or part of the vector along each axis.
  5.     ‘mean’:  Pads with the mean value of all or part of the vector along each axis.
  6.     ‘median’:  Pads with the median value of all or part of the vector along each axis.
  7.     ‘minimum’:  Pads with the minimum value of all or part of the vector along each axis.
  8.     ‘reflect’:  Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
  9.     ‘symmetric’: Pads with the reflection of the vector mirrored along the edge of the array.
  10.     ‘wrap’:  Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
  11.     ‘empty’:  Pads with undefined values.
  12.     <function> : Function for setting the extra pixels.

The padding is interesting when you extract objects of interest by crop and want to put all the cropped images in a box of identical size like in the following montage...

Fig. 4: Montage of all the coins cropped from img and then padded in a box of identical size. It is easier to manipulate image series of the same size. Here, the padding is done with the `edge` mode (the image border is extended).

 

4. Translation and Rotation

4.1. The Script

++++ Python Script ++++TODO++++ End of Python Script ++++

4.2. Description

5. Downsampling (binning)

 TODO

6. Resizing 

TODO

Thank you for reading.



3. Other crazybiocomputing posts

Further readings are available in ...
  • Image Enhancement Series  [Link]
  • Image Processing TOC [Link]
  • Wikipedia about Kernel [Link]







No comments:

Post a Comment