Part I · Foundations · Lesson 00

Setup and how the course code works

Install Python, OpenCV and friends in a virtual environment, download the code and data, and learn the two-flag convention every lesson script follows.

What you need#

  • Python 3.10 or newer. Check with python3 --version.
  • About 200 MB of disk space for the libraries and the data.
  • Any editor. Jupyter works too: every script is organised in small blocks you can paste into cells.

Get the code and the data#

Download cv-course-code.zip and unzip it. You get this layout:

cv-course/
├── cvutils.py              # tiny helpers: load data, draw/save figures
├── requirements.txt
├── 01_images_as_arrays.py  # one script per lesson
├── ...
├── 19_fingerprints.py
└── data/                   # every image and dataset used in the lessons
    ├── basics/  noise/  morphology/  color/  features/
    ├── calibration/        # chessboard pictures from my webcam
    ├── stereo/             # OpenCV's stereo calibration sample
    ├── modd/               # a stereo frame from the MODD maritime dataset
    ├── kitti/              # 15 frames of KITTI odometry, sequence 00
    ├── ct/                 # 21 CT slices (NIfTI)
    └── fingerprints/       # a fingerprint "database" and five crime-scene prints

Create an environment#

Always work in a virtual environment, so the course libraries never fight with your system packages.

cd cv-course
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

On Windows the activation line is .venv\Scripts\activate. The requirements are:

numpy>=1.26
opencv-python>=4.8
matplotlib>=3.8
scikit-image>=0.22
scikit-learn>=1.4
scipy>=1.11
nibabel>=5.2
pyproj>=3.6

opencv-python vs opencv-contrib-python

opencv-python already contains SIFT, ORB, stereo matching and calibration. Install opencv-contrib-python instead (never both) only if you want the extra modules (cv2.ximgproc, cv2.aruco extras…). On a server without a screen, use opencv-python-headless.

Check that everything imports:

python -c "import cv2, numpy, skimage; print(cv2.__version__, numpy.__version__)"

How every lesson script works#

Each lesson is a plain script that you run from the cv-course folder:

python 05_noise_filtering.py            # opens each figure in a window
python 05_noise_filtering.py --save out # writes the figures to out/ instead

Numbers (errors, counts, timings) are printed to the terminal, and the lesson pages show the same output. The helpers live in cvutils.py; the only one you need to know is U.show(name, images, titles), which draws a grid of images:

def show(name, images, titles=None, cols=None, size=3.4, cmap="gray"):
    """Draw a grid of images. Colour images are assumed to be BGR."""
    import matplotlib.pyplot as plt

    n = len(images)
    cols = cols or n
    rows = (n + cols - 1) // cols
    h, w = images[0].shape[:2]
    cell_h = size * min(max(h / w, 0.3), 1.6)       # follow the aspect ratio of the images
    fig, axes = plt.subplots(rows, cols, figsize=(size * cols, (cell_h + 0.35) * rows), squeeze=False)
    for i, ax in enumerate(axes.flat):
        if i < n:
            _draw(ax, images[i], titles[i] if titles else None, cmap)
        else:
            ax.axis("off")
    finish(name, fig)

Notice two details that will come back in lesson 01: colour images are converted from BGR (OpenCV's order) to RGB before drawing, and uint8 grayscale images are drawn with vmin=0, vmax=255, so Matplotlib never secretly stretches the contrast.

Run scripts from the course folder

Data paths are resolved relative to cvutils.py, so the scripts work from anywhere, but import cvutils needs the folder on Python's path. The simplest is to cd cv-course first.

Where the material comes from#

The exercises follow the lab sessions of Visão Robótica (Robotic Vision, UFRN) and TIM — Traitement d'Images (INSA Rouen Normandie), my solutions on GitHub, and the ORB project I did at INSA. The text, figures and code here were rewritten for this course. Datasets: KITTI odometry (CC BY-NC-SA 3.0), MODD (Kristan et al.), OpenCV's sample stereo images, the classic cameraman test image and the other test pictures used in the UFRN and INSA labs (including photos of Rouen and the low-contrast Hawke's Bay image from Wikimedia Commons).