This C++ program performs 2D convolution on dynamically-sized grayscale images using various kernels. It demonstrates image processing concepts such as zero padding, kernel application, and matrix-based convolution operations.
The program showcases fundamental image processing operations common in computer vision, edge detection, and digital filtering applications.
- Overview
- Features
- Code Structure
- How It Works
- Output Example
- Dependencies
- Build Instructions
- Limitations
- Possible Improvements
The program performs the following operations:
- Allows the user to specify the image size (3-12)
- Generates a random grayscale image (values 0-255)
- Offers multiple kernel options (edge detection, sharpening, blur)
- Applies zero-padding around the original image
- Performs convolution between the padded image and the chosen kernel
- Outputs the convolution result
- Configurable matrix sizes (3-12)
- Multiple kernel options:
- Vertical Edge Detection
- Horizontal Edge Detection
- Sharpen
- Blur
- Dynamic memory allocation for matrices
- Input validation for user selections
- Zero-padding implementation
- 2D convolution operation
- Formatted output display
- Well-organized modular code structure
The project is organized into multiple files for better code management:
main.cpp
: Contains the main control flow
matrix_ops.h
: Declarations for matrix-related functionsmatrix_ops.cpp
: Implementations of matrix functions:getValidatedMatrixSize()
: Validates and retrieves user input for matrix sizeallocateMatrix()
: Allocates memory for a matrixdeallocateMatrix()
: Releases allocated memorygenerateRandomImage()
: Creates a matrix with random pixel valuesdisplayMatrix()
: Outputs a matrix to the consoleinitializeWithZeros()
: Sets all matrix elements to zero
convolution.h
: Declarations for convolution-related functionsconvolution.cpp
: Implementations of convolution functions:selectKernelType()
: Gets user choice for kernel typeinitializeKernel()
: Sets up the kernel based on the selected typedisplayKernel()
: Outputs the kernel to the consoleapplyPadding()
: Creates a padded version of the original imageperformConvolution()
: Executes the convolution operation
- The program starts by seeding the random number generator
- It asks the user to specify the image size (within the range 3-12)
- It allocates memory for:
- The original image matrix
- The padded image matrix
- The result matrix
- It generates random values for the image
- It asks the user to select a kernel type
- It applies zero-padding to the original image
- It performs the convolution operation using the selected kernel
- It displays the result
- It cleans up allocated memory before exiting
=== Image Convolution with Padding ===
Enter matrix size (3-12): 6
Original Image Matrix:
156 248 9 87 22 81
234 46 103 30 135 144
213 93 112 32 132 111
22 219 144 215 128 251
89 63 63 27 30 187
163 157 206 82 47 73
Select kernel type:
1. Vertical Edge Detection
2. Horizontal Edge Detection
3. Sharpen
4. Blur
Enter your choice (1-4): 1
Selected Kernel:
-1 0 1
-1 0 1
-1 0 1
Convolution Result:
294 -278 -177 45 108 -157
387 -379 -238 65 187 -289
358 -110 -81 36 229 -395
375 -5 -101 -29 275 -290
439 139 -115 -208 187 -205
220 17 -111 -192 151 -77
- C++ compiler with STL support
- Standard libraries:
<iostream>
for I/O operations<cstdlib>
for random number generation<ctime>
for seeding the random generator<iomanip>
for formatted output
- Compile all source files together:
g++ main.cpp matrix_ops.cpp convolution.cpp -o image_convolution
- Run the executable:
./image_convolution
- Only supports square matrices
- Limited to grayscale images (single channel)
- Uses simple zero-padding only
- Random image generation (no file input)
- Fixed kernel size (3×3)
- No visualization beyond text output
- Add support for image file input/output
- Implement different padding strategies (reflect, replicate, etc.)
- Support non-square matrices and images
- Add more kernel options and custom kernels
- Implement normalization strategies for outputs
- Add graphical visualization of results
- Support multi-channel (color) image processing
- Implement more boundary handling options
- Add benchmarking capabilities
- Implement multi-threading for performance on larger matrices
This implementation demonstrates important concepts of digital image processing while providing a flexible, modular codebase for educational purposes.