programdemos/image-pipeline/image-pipeline.xtl

An image pipeline as array programs: a picture built from masks, blurred, its edges found with the Sobel kernels, thresholded, and shrunk by max-pooling. Every 3 x 3 filter is the same two steps: the stack of the nine shifted copies of the image (its windows), then the kernel times the stack, summed. Run it with "xetal run demos/image-pipeline/image-pipeline.xtl"; it prints the windows' and the pooled map's shapes, then the picture, the edge strength, the edges and the pooled edges, each drawn with characters. The live page lets you edit the kernels and pick a picture, and click a pixel for its window times each kernel: https://softwarewrighter.github.io/X_eTaL-demos/image-pipeline/

source

rows : Int

value · line 14
rows ← 20
Used in: row, col, pool

cols : Int

value · line 15
cols ← 40
Used in: row, col, pool

row : Int

value · line 19

-- coordinates: each pixel's row and column. row, col: by broadcasting (t_able).

row ← (o̲ffsets rows) 'l̲eft t̲able o̲ffsets cols
Used in: disk, box, img

col : Int

value · line 20
col ← (o̲ffsets rows) 'r̲ight t̲able o̲ffsets cols
Used in: disk, box

disk : Bool

value · line 25
disk ← (((row − 9) × row − 9) + ((col − 10) × col − 10) d̲iv 2) < 36
Used in: img

box : Bool

value · line 26
box ← ((row ≥ 5) ∧ row < 15) ∧ (col ≥ 22) ∧ col < 33
Used in: img

img : Float

value · line 27
img ← 0.15 + (0.7 × f̲loat disk) + (0.45 × f̲loat box) + 0.001 × f̲loat r̲oll! (s̲hape row) r̲eshape 60

blur : Float

value · line 32

-- the kernels. blur: a Gaussian blur. kx, ky: Sobel edge kernels (ky is kx turned a quarter, o_\, the transpose). thresh: the edge strength that counts as an edge.

blur ← (3 c̲at 3) r̲eshape 0.0625 0.125 0.0625 0.125 0.25 0.125 0.0625 0.125 0.0625
Used in: smooth

kx : Float

value · line 33
kx ← (3 c̲at 3) r̲eshape -1.0 0.0 1.0 -2.0 0.0 2.0 -1.0 0.0 1.0
Used in: ky, gx

ky : Float

value · line 34
ky ← o̲\ kx
Used in: gy

thresh : Float

value · line 35
thresh ← 0.8

ᵘw̲indows : a -> a

function · line 42

-- the windows: nine shifted copies. u:w_indows x: rotating by a list of amounts gives one copy per amount, on a new first axis. Columns are shifted first; then the rows, which the new axis moved to axis 2. Item [a;b;r;c] is the pixel at row r + a, column c + b, for a and b in -1 0 1.

ᵘw̲indows ← { x → -1 0 1 o̲-₂ -1 0 1 o̲-₂ x }

A filter: kernel times windows, summed

ᵘf̲ilter : Num a => a -> a -> a

function · line 48

u:f_ilter k x: the kernel spread over the picture (3 x 3 x rows x cols) times the windows, summed over its two axes.

ᵘf̲ilter ← { k x → '+ r̲/₁₂ (ᵘw̲indows x) × k 'l̲eft t̲able x }
Used in: smooth, gx, gy

The pipeline

smooth : Float

value · line 54

smooth, gx, gy, mag, edges: the blur, the two Sobel gradients, the edge strength (their length), and where it passes the threshold.

smooth ← blur ᵘf̲ilter img
Used in: gx, gy

gx : Float

value · line 55
gx ← kx ᵘf̲ilter smooth
Used in: mag

gy : Float

value · line 56
gy ← ky ᵘf̲ilter smooth
Used in: mag

mag : Float

value · line 57
mag ← ((gx × gx) + gy × gy) ^ 0.5

edges : Float

value · line 58
edges ← f̲loat mag > thresh

pool : Float

value · line 62

-- max-pooling: 2 x 2 blocks by reshape, reduce. pool: reshaped so each 2 x 2 block has two axes of its own, then the largest over them.

pool ← 'm̲ax r̲/₂ 'm̲ax r̲/₄ ((rows d̲iv 2) c̲at 2 c̲at (cols d̲iv 2) c̲at 2) r̲eshape mag

ᵘd̲raw : Float -> Char

function · line 70

u:d_raw a: the picture, the smoothed edge strength, the edges and the pooled edges, drawn with characters (darkest first).

ᵘd̲raw ← { a → (1 + f̲loor 0 m̲ax 7 m̲in 8.0 × a) s̲elect " .:-=+*#" }