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 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.
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
- To crop the image (line #7), we define a bounding box whose top left corner has XY-coordinates (x0,y0) = (180,100) and whose bottom right corner is (x1,y1) = (230,150).
- However, an image is represented as a 2D numpy array in which the first axis corresponds to the Y and the second to the X. Therefore, when defining the bounding box through array indexing, we specify the Y range first, followed by the X range. The box coordinates must therefore be written as follows:
img[y0:y1, x0:x1].
1.3. Exercises
- Exercise: Try to crop the same coin giving the coin center
(205,125)and the diameter25. - 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
++++ 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:
- ‘constant’ (default): Pads with a constant value.
- ‘edge’: Pads with the edge values of array.
- ‘linear_ramp’: Pads with the linear ramp between end_value and the array edge value.
- ‘maximum’: Pads with the maximum value of all or part of the vector along each axis.
- ‘mean’: Pads with the mean value of all or part of the vector along each axis.
- ‘median’: Pads with the median value of all or part of the vector along each axis.
- ‘minimum’: Pads with the minimum value of all or part of the vector along each axis.
- ‘reflect’: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
- ‘symmetric’: Pads with the reflection of the vector mirrored along the edge of the array.
- ‘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.
- ‘empty’: Pads with undefined values.
- <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...
4. Translation and Rotation
4.1. The theory
When you want to apply a rotation and/or translation to an image, you are actually defining the 3x3 matrix of Fig. 5 and by multiplying each pixel coordinates to the matrix, you get the new coordinates (and pixe value) of the transformed image.
![]() |
| Fig. 5: Transformation matrix used by skimage to rotate an image by alpha and translate it by a vector (tx,ty) |
4.2. Using ski.transform.rotate(..)
For rotation, there is a dedicated function in skimage called ski.transform.rotate(..) accepting the angle in degrees in a counter-clockwise rotation. However, nothing for translation, we need to use the warp(..) function combined with a transformation matrix (see next paragraph).
4.3. Using EuclideanTransform(..) with warp(..)
This is a two-step procedure: (i) First, you define the transformation matrix (Fig.5) by setting the rotation angle in radians and the translation vector, then (ii) you apply the inverse matrix to the image withe the function warp(..).
By using the EuclideanTransform, the rotation is around the image origin (top left corner Fig. 6A). If you want to rotate around the image center (Fig. 6B, the most common behavior), you have to...
- Shift the image center to (0,0) by a translation vector of (-width /2, -height /2).
- Rotate the image by alpha.
- Inverse the shifts to bring back the image at its original center.
++++ Python Script ++++++++ End of Python Script ++++
![]() |
| Fig. 6: Rotation using warp(..) and EuclideanTransform. |
5. Downsampling (binning)
TODO
6. Resizing
TODO
Thank you for reading.






No comments:
Post a Comment