C · Local Linux terminal
Turn a color image into an edge map
Online version: https://fpga.labsland.com/altera/embedded-systems/lab-10/part-01/
Your computer
Can you keep the outline of a colored object while removing an isolated bright pixel? Implement a five-stage edge detector in C, inspect each image and measure the CPU time. This software activity runs in a Linux terminal with a C compiler and an image viewer. It assumes you can index a two-dimensional array and convolve a small kernel.

The same 63 × 47 target before processing (left) and after thinning and thresholding at 45 (right).
Run the starter
Extract the complete ZIP and work in its root directory. The supplied 63 × 47 color target contains a rectangle, a disk, a diagonal band and scattered bright pixels. Open resources/target.bmp and predict which features will survive smoothing.
cc -std=c11 -O2 -Wall -Wextra -Werror starter/edges.c -lm -o edges
./edges resources/target.bmp edges.bmp 45 --stages
Open 01-gray.bmp through 04-thin.bmp and edges.bmp. The starter reports 1,293 selected pixels. Several stages initially show the same image. Implement each stage before moving to the next.
Build the detector
- Grayscale. Inspect the supplied conversion. For RGB (45,140,225), predict the integer intensity and check a pixel inside the rectangle.
- Gaussian smoothing. Convolve with these five rows:
1 4 7 4 1,4 16 26 16 4,7 26 41 26 7,4 16 26 16 4,1 4 7 4 1. Divide the sum by 273. Treat pixels outside the image as zero. Check the center of the rectangle and a point near a corner. - Sobel gradients. Subtract the left column from the right using weights 1, 2, 1 for
gx. Subtract the bottom row from the top using the same weights forgy. Storemin(255, (abs(gx)+abs(gy))/2)and the gradient direction rounded to the nearest 45 degrees modulo 180. The first raster row is the top of the image. - Thin each edge. Compare a pixel with the two neighbors along its gradient. Keep it only when its intensity exceeds both. For 45 degrees compare northeast and southwest. Use a separate output buffer so an earlier update cannot change the next comparison.
- Threshold and check adjacency. Retain the intensity of a pixel only if it and at least one of its eight neighbors both exceed the threshold. Compare against the entire thinned input, including diagonal neighbors.
Check the result
At threshold 45 the supplied image should contain 525 edge pixels. At 255 it should contain none. Try 0, 45 and 120 and inspect the same corner and diagonal in each output. Look for an outer border introduced by zero padding. Test a second uncompressed 24-bit BMP of your own.
| Threshold | Edge pixels | Median CPU time over five runs | Visible change |
|---|---|---|---|
| 0 | |||
| 45 | |||
| 120 |
Record the image size, compiler options and machine alongside your timing. The program measures only the five processing stages. Explain which steps an FPGA accelerator would need to perform to produce the same output.
FPGAcademy · Embedded Systems · Lab 10 · I. Original handout · MIT
