Lab / Edge filter demo
Edge filter demo
Run the edge filter I wrote in Verilog for a Zybo Z7-20 FPGA board on a picture, here in this browser tab. No FPGA is involved: the page runs a JavaScript port of the repository’s bit-accurate software model of the filter, the model its RTL simulation was checked against, so you see what the Verilog computes, bugs included, beside the textbook Sobel and Prewitt filters it was meant to be.
How it works
The repository’s verify/model.py models the filter module, vid_proc.v, and its square-root helper, sqrt.v, cycle by cycle and register for register. Run against the Verilog in Icarus Verilog, after two edits a simulator needs that should not change the synthesised logic, it gave the same output in 62 of 62 cases, 10,291,200 output words without a mismatch (verify/results/compare.txt). The JavaScript here is a port of that model at commit e86e208; tests in this site’s repository check it against model.py’s output word for word in the same 62 cases, pixel for pixel on small frames streamed as this page streams them, and against the impulse responses and other figures recorded from the Verilog run.
Each picture goes through the model one pixel per clock, with the 1080p60 line timing of the board’s output: 2,200 clocks a line, active video starting 192 clocks after the start of horizontal sync, and the same vertical blanking. The run starts from the all-zero power-up state and the page shows the second of two frames, as the repository’s comparison does. The sample pictures are at most 480 pixels wide, so they lie in the part of each line the filter processes: in a full 1080p frame the rightmost 310 columns are always white in the gradient modes, and at 720p every second line is.
What the original design computes, as the repository’s RESULTS.md describes it. The line buffers’ write pointer advances every pixel instead of every line, so the filter does not see a 3×3 neighbourhood: its impulse response has six taps over four lines. Gradients are cut to 8 bits, so a negative edge wraps round instead of showing its absolute value, and −253 is drawn as 3, almost black. The greyscale is a shift-and-add luma, about 0.297 R + 0.594 G + 0.125 B, so white becomes 253. And the picture is drawn where the hardware puts it against the syncs: two lines down and three pixels right in the X and Y modes, two pixels right in the greyscale bypass, two lines down and five pixels right in the magnitude mode.
The magnitude mode is the least certain. Its square root is a latch inside a combinational loop, which has no defined cycle behaviour on the FPGA; in simulation it takes one Newton step each time its input changes, and that is what the model reproduces. Its inputs are the wrapped 8-bit gradients, and their sum of squares, up to 17 bits, goes into a 16-bit signed port, so only its low 16 bits count: a sum from 32,768 to 65,535, or 98,304 and over, reads as negative and gives 0, while a sum from 65,536 to 98,303 wraps round to a number below 32,768 and is used as that number. That mode describes the simulator, not the board.
The limits. This is a simulation only: nothing in the repository was measured on the board. The design does not meet timing: the committed Vivado reports give a pixel-clock worst negative slack of −92.2 ns (Prewitt) and −90.3 ns (Sobel) against a 6.734 ns period, through the square root and through the line-buffer reads that every gradient mode uses. So whether the board’s output matches the simulation is not known.
The textbook reference is the repository’s reference_filter: the standard 3×3 Sobel or Prewitt on the same greyscale picture, giving |X|, |Y| or the integer square root of X² + Y², limited to 255, with the border pixels set to 0. It is what the design was meant to compute.
Sobel and Prewitt are separate builds, two Vivado projects, so on the board the filter is chosen when the design is built and the mode while it runs. The design applies one mode at a time: two buttons step to the next or previous mode and a third returns to mode 0; mode 0 is the Y gradient, mode 1 the X gradient and mode 2 the magnitude (modes 3 to 15 give a white screen, left out here); and switch SW3 selects the greyscale bypass, whatever the mode. Running up to 6 stages in order is this page’s own addition, computed in software. In the original-design model each stage’s grey output goes into the next as the 24-bit pixels the hardware puts out, so the next stage turns it into grey again; in the textbook reference the picture is turned into grey once.
Everything runs in this browser tab, in a background worker where the browser allows one. The sample pictures, six drawn by a script in this site’s repository and seven photographs, are files on this site, each loaded only when you choose it. The photographs are JPEG files, which browsers may decode very slightly differently, so their results can differ by a few pixels from one browser to another. Nothing is uploaded, no camera is used, and the pictures, the pipeline and the results stay in this tab.
Photo credits
The photographs come from Lorem Picsum, which serves images from Unsplash; Unsplash offers its photographs under the Unsplash License. Each was downloaded once, at 480 by 360 pixels, and is part of this site, so the page contacts neither service.
- Stepped facade: photograph by Julien Moreau, on Unsplash
- Towers from below: photograph by Anthony DELANOIX, on Unsplash
- Waterfall and rocks: photograph by Paul Jarvis, on Unsplash
- Lioness: photograph by Samuel Scrimshaw, on Unsplash
- Jellyfish: photograph by Marat Gilyadzinov, on Unsplash
- Strawberries: photograph by veeterzy, on Unsplash
- Stormy sea: photograph by Patrick Fore, on Unsplash