A lightweight image processing program written in C that performs edge detection on grayscale images using the Discrete Laplacian Operator.
This project was developed as part of Calculus PBL Set 2 to demonstrate how concepts from multivariable calculus—particularly second-order partial derivatives—can be transformed into practical algorithms used in computer vision and spatial domain image processing.
An image can be modeled mathematically as a continuous intensity function
To detect edges, we seek regions where the intensity changes rapidly. The Laplacian Operator is a second-order differential operator defined as the sum of the unmixed second partial derivatives:
Unlike first derivatives, which measure the rate of change (gradient), the Laplacian measures the total curvature of the function. Large Laplacian magnitudes occur at locations where intensity undergoes abrupt transitions, making the operator highly effective for edge detection.
Digital images are not continuous surfaces; they are discrete grids of pixels where coordinates exist in
To approximate the continuous Laplacian numerically, we use finite difference methods.
For the second derivative along the
Similarly, along the
Adding both approximations yields the discrete Laplacian:
This equation forms the core mathematical foundation of the C implementation.
The discrete Laplacian can be represented as a convolution kernel. By sliding this
For every pixel, the kernel computes a weighted combination of neighboring intensities. Regions of nearly constant intensity produce responses close to zero, while strong intensity transitions produce large positive or negative responses.
Let
where the threshold
Consider a region of constant intensity where
The project requires only a standard C compiler and has no external dependencies.
gcc -Wall -Wextra -O2 -o edge_detector main.cThe program expects exactly two command-line arguments:
- The input image file (must be a binary P5 PGM image).
- The desired output file name.
Run the program as follows:
./edge_detector input.pgm output.pgmExample:
./edge_detector lena.pgm lena_edges.pgmUpon execution, the program loads the input image, computes the discrete Laplacian response at every pixel, applies thresholding, and writes the resulting edge map to the specified output file.
The implementation currently supports grayscale images stored in the Portable GrayMap (PGM) format using binary P5 encoding.
Example header:
P5
512 512
255
where:
P5specifies binary grayscale encoding.512 512denotes image width and height.255is the maximum pixel intensity value.
The following example demonstrates the effect of applying the Laplacian operator to a grayscale image.
Original Image (input.pgm) |
Edge Map (output.pgm) |
|---|---|
![]() |
![]() |
The output image highlights regions of rapid intensity variation. Pixels corresponding to detected edges are rendered white (255), while non-edge regions remain black (0).
For an image containing
-
Time Complexity:
$O(N)$ Each pixel is visited exactly once and requires a constant number of arithmetic operations. -
Space Complexity:
$O(N)$ An additional image buffer is allocated to store the resulting edge map.

