The Darkroom

Develop in progress
  1. I Histograms & contrast stretching
  2. II Otsu's threshold
  3. III Noise & the adaptive median filter
  4. IV Blobs & scale space
  5. V Counting cells with watershed
  6. VI Geometric transforms & gradients
  7. VII Hybrid images & sharpening
  8. VIII Color edges & the structure tensor
  9. IX Feature detection & matching
  10. X From filters to CNNs
I

Histograms & contrast stretching

Digital Image Processing
Original
Stretched

What I built

I wrote histogram builders from scratch (any bin count, optional masks) and compared a brute-force double loop against indexing by intensity ÷ bin width, which was dramatically faster. Then I tried to rescue a very dark night photo with contrast stretching.

The math

s=255⋅r−rmin⁡rmax⁡−rmin⁡s = 255\cdot\frac{r - r_{\min}}{r_{\max} - r_{\min}}rmin⁡=Pp(I),rmax⁡=P1−p(I)r_{\min} = P_{p}(I),\quad r_{\max} = P_{1-p}(I)

What I found

Plain min–max stretching did almost nothing: a handful of bright lights already sat near 255, so the extremes were pinned at both ends. Ignoring the darkest and brightest 1–5% of pixels before stretching is what finally pulled detail out of the shadows.

Read the Python on GitHub ↗
II

Otsu's threshold

Digital Image Processing
Input (grayscale)
Binarized at t

histogram between-class variance σ²B(t) your t Otsu's t*

What I built

I implemented Otsu’s method directly from the histogram, so the cost depends on the 256 intensity bins rather than the number of pixels, and used it to separate a lung cross-section, a central square, and a river from their backgrounds.

The math

σB2(t)=ω0(t) ω1(t) [μ0(t)−μ1(t)]2\sigma_B^2(t) = \omega_0(t)\,\omega_1(t)\,\big[\mu_0(t) - \mu_1(t)\big]^2t∗=arg⁡max⁡t  σB2(t)t^\ast = \arg\max_{t}\; \sigma_B^2(t)

What I found

Otsu works when the histogram is bimodal: the lung scan splits almost perfectly. The river photo is shadowy and low-contrast, with no clean valley between peaks, so any single global threshold struggles and it needs preprocessing first.

Read the Python on GitHub ↗
III

Noise & the adaptive median filter

Digital Image Processing
Noisy input
Filtered
–MSE, noisy
–MSE, filtered
–run time

What I built

I scored Gaussian and median filters against Gaussian and salt-and-pepper noise with mean squared error, then implemented the adaptive median filter by hand, including a count of how many pixels each window size recovered.

The math

A: zmin⁡<zmed<zmax⁡  ⇒  go to B\textbf{A: } z_{\min} < z_{\text{med}} < z_{\max} \;\Rightarrow\; \text{go to } \textbf{B}otherwise Sxy←Sxy+2    (Sxy≤Smax⁡)\text{otherwise } S_{xy} \leftarrow S_{xy} + 2 \;\; (S_{xy} \le S_{\max})B: f^(x,y)={zxyzmin⁡<zxy<zmax⁡zmedotherwise\textbf{B: } \hat f(x,y) = \begin{cases} z_{xy} & z_{\min} < z_{xy} < z_{\max} \\ z_{\text{med}} & \text{otherwise}\end{cases}MSE=1MN∑x,y(f(x,y)−f^(x,y))2\mathrm{MSE} = \tfrac{1}{MN}\textstyle\sum_{x,y}\big(f(x,y) - \hat f(x,y)\big)^2

What I found

On heavy Gaussian noise a small Gaussian (σ = 2) narrowly won, MSE 66 vs. 68 for a 7×7 median, while σ = 7 blurred away detail (MSE 281). On dense salt-and-pepper the adaptive median wins because it only grows its window where the median itself is an impulse, so edges survive. Try pushing the density to 70%.

Read the Python on GitHub ↗
IV

Blobs & scale space

Digital Image Processing
Detected nuclei
Scale-normalized response σ²∇²Gσ ∗ I (red channel)

What I built

I looked for cell nuclei in two histology slides with a scale-normalized Laplacian of Gaussian, run on the red channel because it gave the strongest contrast between nuclei and background, and compared it against k-means color segmentation.

The math

∇norm2L=σ2(∂2∂x2+∂2∂y2)(Gσ∗I)\nabla^2_{\text{norm}} L = \sigma^2 \left(\frac{\partial^2}{\partial x^2} + \frac{\partial^2}{\partial y^2}\right)\big(G_\sigma \ast I\big)blob of radius r peaks at σ=r/2\text{blob of radius } r \text{ peaks at } \sigma = r/\sqrt{2}

What I found

For the glomus tumor, σ = 3–4 isolated the nuclei best. Below that the filter only lit up nucleus edges instead of one consolidated peak; above it, small nuclei fragmented or vanished. Multiplying by σ² is what makes responses comparable across scales.

Read the Python on GitHub ↗
V

Counting cells with watershed

Digital Image Processing
K-means mask after opening
Distance transform + markers
Segmented cells

My full-resolution pipeline (with OpenCV's watershed) counted 252 cells on this slide.

What I built

To count touching cells I chained k-means color clustering, opening and closing, a distance transform, local-maxima markers, connected components and marker-controlled watershed, then measured the cell-area distribution.

The math

D(p)=min⁡q∉F ∥p−q∥D(p) = \min_{q \notin F}\, \lVert p - q \rVertmarkers={ p:D(p)=max⁡∥u−p∥∞≤dD(u) }\text{markers} = \big\{\,p : D(p) = \max_{\lVert u-p\rVert_\infty \le d} D(u)\,\big\}A∘B=(A⊖B)⊕BA \circ B = (A \ominus B) \oplus B

What I found

My pipeline counted 252 cells. The decisions that moved the count most were applying both opening and closing, the minimum spacing between distance-transform peaks, and labeling components both before and after watershed. The browser version uses a geodesic flood from each marker as a lightweight stand-in for watershed.

Read the Python on GitHub ↗
VI

Geometric transforms & gradients

Digital Image Processing
Input

What I built

I applied affine transforms (flips, rotation, scaling, translation, cropping) with inverse mapping, then computed x and y derivatives, gradient magnitude and orientation at two scales on a photo of a brick building.

The math

[x′y′]=[t11t12t21t22][xy]+[txty]\begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} t_{11} & t_{12} \\ t_{21} & t_{22}\end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix} + \begin{bmatrix} t_x \\ t_y \end{bmatrix}Iout(x′)=I(T−1x′)I_{\text{out}}(\mathbf{x}') = I\big(T^{-1}\mathbf{x}'\big)∥∇I∥=Ix2+Iy2,θ=atan2⁡(Iy, Ix)\lVert\nabla I\rVert = \sqrt{I_x^2 + I_y^2},\quad \theta = \operatorname{atan2}(I_y,\, I_x)

What I found

Larger derivative scales give stronger responses on prominent edges but lose fine ones: the individual bricks are outlined at scale 3 and gone at 11. Because the scene is mostly bricks, windows and corners, the orientation histogram peaks at 0°, 90°, 180° and 270°, and smooths out between those peaks at the larger scale.

Read the Python on GitHub ↗
VII

Hybrid images & sharpening

Computer Vision

What I built

I split each photo into a Gaussian low-pass and its high-pass residual, then built hybrid images by adding the low frequencies of one photo to the high frequencies of another, and used the same residual for unsharp-mask sharpening.

The math

H=Gσ∗I1  +  (I2−Gσ∗I2)H = G_\sigma \ast I_1 \;+\; \big(I_2 - G_\sigma \ast I_2\big)

What I found

At small σ the high-pass image is just a faint outline over the low-pass one; as σ grows the balance tips and the close-up image takes over. Sharpening re-adds the high frequencies, making darks darker and lights lighter, which shows up as new histogram peaks at 0 and 255.

Read the Python on GitHub ↗
VIII

Color edges & the structure tensor

Computer Vision
Input: fluorescent cells (blue nuclei, red filaments)

What I built

I computed derivatives for each color channel of a fluorescence image and combined them into a color structure tensor, comparing it against gradients taken after converting to grayscale, at several integration scales.

The math

Jσ=Gσ∗∑c∈{R,G,B}[Ix,c2Ix,cIy,cIx,cIy,cIy,c2]J_\sigma = G_\sigma \ast \sum_{c\in\{R,G,B\}} \begin{bmatrix} I_{x,c}^2 & I_{x,c} I_{y,c} \\ I_{x,c} I_{y,c} & I_{y,c}^2 \end{bmatrix}R=det⁡Jσ−k (tr⁡Jσ)2R = \det J_\sigma - k\,\big(\operatorname{tr} J_\sigma\big)^2

What I found

Converting to grayscale first erases these edges: the dark blue nuclei, dark red filaments and black background all land on similar grays. Summing the tensor over channels keeps both structures, and a larger integration scale trades fine detail for a cleaner, more stable response.

Read the Python on GitHub ↗
IX

Feature detection & matching

Computer Vision

correct match (within 8 px of the true homography) wrong match

–matches kept
–correct
–precision

What I built

I built a full feature pipeline: Harris–Stephens corners with adaptive non-maximal suppression (200 points worked best), window descriptors plus an illumination-robust variant, and three matching rules: distance threshold, nearest neighbor, and nearest-neighbor distance ratio, scored against ground-truth homographies on the Oxford sets.

The math

accept (i,j1) if d(fi,fj1)d(fi,fj2)<τ  (τ=0.7)\text{accept } (i, j_1) \text{ if } \frac{d(f_i, f_{j_1})}{d(f_i, f_{j_2})} < \tau \; (\tau = 0.7)correct if ∥H x~i−xj1′∥<ϵ\text{correct if } \big\lVert H\,\tilde{\mathbf{x}}_i - \mathbf{x}'_{j_1} \big\rVert < \epsilon

What I found

The ratio test won, especially as the second image moved further from the first: comparing bikes 1 and 6, its true-positive rate (0.44) was more than double the next method (0.17) at a low false-positive rate (0.03). The plain window descriptor collapsed under lighting changes on Leuven, and the illumination-robust version raised its true-positive rate from about 0.2 to 0.8.

Read the Python on GitHub ↗
X

From filters to CNNs

Computer Vision
Input
3×3 kernel (edit me)
Feature map

What I built

A convolutional layer is the same 3×3 filtering as the rest of this room, except the network learns the kernel weights, follows each with a nonlinearity, and pools to grow its receptive field. I trained and compared CNNs on CIFAR-10 in PyTorch.

The math

yij=max⁡(0,  b+∑u,vWuv xi+u, j+v)y_{ij} = \max\Big(0,\; b + \sum_{u,v} W_{uv}\, x_{i+u,\, j+v}\Big)pij=max⁡(u,v)∈2×2y2i+u, 2j+vp_{ij} = \max_{(u,v)\in 2\times2} y_{2i+u,\,2j+v}

What I found

My redesigned network reached 68% mean per-class accuracy against 59% for the PyTorch tutorial baseline, with cat the hardest class. Restricting the task to four classes pushed accuracy to 71%. Edit the kernel to see what a single learned filter might respond to.

Read the Python on GitHub ↗

Test images were provided with CS 7650 (Fall 2023) and CS/ECE 8690 (Spring 2024) at the University of Missouri. All demos are re-implemented in TypeScript and run locally in your browser.