python -m venv newts
Image Processing Basics
Newt affine alignment for newt affine aligners.
Purpose
This tutorial serves multiple functions.
- it introduces basic concepts of digital image processing
- it showcases some of the great functions of the Python package
scikit-image
- it derives a procedure to auto-extract regions of interest (newts) from more or less standardized field photos
You can read it in two ways:
- One can scan through the notebook and just look at headlines and outcome images.
- One can read the code in between, look up the functions in skimage docs, try to reproduce the outcomes, and tweak-optimize parameter choices.
Warning
As of March 2025, this tutorial is just a rough outline and requires further wording and refinement.
setup
python virtual environment
A venv
is the easiest way to get a reproducible environment on linux.
source newts/bin/activate.fish
pip install --upgrade pip
# pip install --upgrade numpy scipy pandas matplotlib scikit-image pygobject
# pip freeze > requirements.txt
pip install --upgrade -r requirements.txt
“conda”-like environment
On windows, you might be better off with a micromamba environment.
You might equally well use the “requirements.txt”.
start python
source newts/bin/activate.fish # or something like: `conda activate`
python
Here we go! These are the libraries we will use:
import os as os # operation system commands
import numpy as np # numerics, e.g. image array manipulation
import pandas as pd # data frames
import scipy.ndimage as ndi # more array manipulation
import scipy.signal as sig # signal processing (stay tuned!)
# some of the many useful modules of the skimage library
import skimage.io as skiio
import skimage.color as skicol
import skimage.filters as skifilt
import skimage.morphology as skimorph
import skimage.measure as skimeas
import skimage.segmentation as skiseg
import skimage.transform as skitrafo
import skimage.util as skiutil
import skimage.feature as skifeat
# visualization
import matplotlib as mpl
import matplotlib.pyplot as plt
# mpl.use("TkAgg")
"gtk4agg") # does not matter much; defaults / TkAgg has trouble with my screen mpl.use(
helper function
Below, we plot all the time; this makes it a one-liner.
def show(img, ax = None, **kwargs):
if ax is None:
= plt.subplots(1, 1)
fig, ax
if len(img.shape) == 3:
= "upper")
ax.imshow(img, origin else:
= "upper", cmap = "gray")
ax.imshow(img, origin
ax.set_axis_off()
= kwargs.get("title", None)
title if title is not None:
ax.set_title(title)
load image
raw
Here is the test image, included for download: