11# Image Convolution with Padding
22
3- This C++ program performs a 2D convolution on a randomly generated 6x6 grayscale image using a manually-defined 3x3 kernel . It simulates image processing concepts such as zero padding, kernel application, and matrix-based convolution.
3+ 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 .
44
5- The program demonstrates fundamental image processing operations that are common in computer vision, edge detection, and digital filtering.
5+ The program showcases fundamental image processing operations common in computer vision, edge detection, and digital filtering applications .
66
77## Table of Contents
88- [ Overview] ( #overview )
@@ -20,91 +20,112 @@ The program demonstrates fundamental image processing operations that are common
2020## Overview
2121
2222The program performs the following operations:
23- 1 . Generates a random 6×6 grayscale image (values 0-255)
24- 2 . Defines a 3×3 edge-detection kernel
25- 3 . Applies zero-padding to create an 8×8 matrix
26- 4 . Performs convolution between the padded image and kernel
27- 5 . Outputs the convolution result
23+ 1 . Allows the user to specify the image size (3-12)
24+ 2 . Generates a random grayscale image (values 0-255)
25+ 3 . Offers multiple kernel options (edge detection, sharpening, blur)
26+ 4 . Applies zero-padding around the original image
27+ 5 . Performs convolution between the padded image and the chosen kernel
28+ 6 . Outputs the convolution result
2829
2930---
3031
3132## Features
3233
33- - Random image generation for demonstration
34- - Predefined edge-detection kernel
34+ - Configurable matrix sizes (3-12)
35+ - Multiple kernel options:
36+ - Vertical Edge Detection
37+ - Horizontal Edge Detection
38+ - Sharpen
39+ - Blur
40+ - Dynamic memory allocation for matrices
41+ - Input validation for user selections
3542- Zero-padding implementation
3643- 2D convolution operation
3744- Formatted output display
45+ - Well-organized modular code structure
3846
3947---
4048
4149## Code Structure
4250
43- The program consists of the following functions:
44-
45- 1 . ` input(int image[6][6]) ` :
46- - Generates and displays a random 6×6 image matrix
47-
48- 2 . ` kernel(int kernell[3][3]) ` :
49- - Initializes a 3×3 edge-detection kernel
50- - Displays the kernel matrix
51-
52- 3 . ` padding(int padd[8][8]) ` :
53- - Creates an 8×8 zero-padded matrix
54-
55- 4 . ` padded(int padd[8][8], int paddedd[8][8], int image[6][6]) ` :
56- - Combines the zero-padded matrix with the original image
57-
58- 5 . ` convolution(int result[6][6], int paddedd[8][8], int kernell[3][3]) ` :
59- - Performs the convolution operation
60- - Stores results in a 6×6 matrix
51+ The project is organized into multiple files for better code management:
52+
53+ ### Main Program
54+ - ` main.cpp ` : Contains the main control flow
55+
56+ ### Matrix Operations Module
57+ - ` matrix_ops.h ` : Declarations for matrix-related functions
58+ - ` matrix_ops.cpp ` : Implementations of matrix functions:
59+ - ` getValidatedMatrixSize() ` : Validates and retrieves user input for matrix size
60+ - ` allocateMatrix() ` : Allocates memory for a matrix
61+ - ` deallocateMatrix() ` : Releases allocated memory
62+ - ` generateRandomImage() ` : Creates a matrix with random pixel values
63+ - ` displayMatrix() ` : Outputs a matrix to the console
64+ - ` initializeWithZeros() ` : Sets all matrix elements to zero
65+
66+ ### Convolution Operations Module
67+ - ` convolution.h ` : Declarations for convolution-related functions
68+ - ` convolution.cpp ` : Implementations of convolution functions:
69+ - ` selectKernelType() ` : Gets user choice for kernel type
70+ - ` initializeKernel() ` : Sets up the kernel based on the selected type
71+ - ` displayKernel() ` : Outputs the kernel to the console
72+ - ` applyPadding() ` : Creates a padded version of the original image
73+ - ` performConvolution() ` : Executes the convolution operation
6174
6275---
6376
6477## How It Works
6578
66791 . The program starts by seeding the random number generator
67- 2 . It creates:
68- - A 6×6 image with random pixel values (0-255)
69- - A 3×3 kernel for edge detection
70- - An 8×8 padded matrix (6×6 image + 1 pixel border)
71- 3 . The convolution operation:
72- - Slides the kernel over the padded image
73- - Computes element-wise multiplication and summation
74- - Produces a 6×6 output matrix
80+ 2 . It asks the user to specify the image size (within the range 3-12)
81+ 3 . It allocates memory for:
82+ - The original image matrix
83+ - The padded image matrix
84+ - The result matrix
85+ 4 . It generates random values for the image
86+ 5 . It asks the user to select a kernel type
87+ 6 . It applies zero-padding to the original image
88+ 7 . It performs the convolution operation using the selected kernel
89+ 8 . It displays the result
90+ 9 . It cleans up allocated memory before exiting
7591
7692---
7793
7894## Output Example
79- The original image matrix is:
95+
8096```
97+ === Image Convolution with Padding ===
98+ Enter matrix size (3-12): 6
99+ Original Image Matrix:
81100 156 248 9 87 22 81
82101 234 46 103 30 135 144
83102 213 93 112 32 132 111
84103 22 219 144 215 128 251
85104 89 63 63 27 30 187
86105 163 157 206 82 47 73
87- ```
88106
89- The kernel matrix is:
90- ```
91- -1 0 1
92- -1 0 1
93- -1 0 1
94- ```
107+ Select kernel type:
108+ 1. Vertical Edge Detection
109+ 2. Horizontal Edge Detection
110+ 3. Sharpen
111+ 4. Blur
112+ Enter your choice (1-4): 1
113+ Selected Kernel:
114+ -1 0 1
115+ -1 0 1
116+ -1 0 1
117+
95118Convolution Result:
96- ```
97- 294 -278 -177 45 108 -157
98- 387 -379 -238 65 187 -289
99- 358 -110 -81 36 229 -395
100- 375 -5 -101 -29 275 -290
101- 439 139 -115 -208 187 -205
102- 220 17 -111 -192 151 -77
119+ 294 -278 -177 45 108 -157
120+ 387 -379 -238 65 187 -289
121+ 358 -110 -81 36 229 -395
122+ 375 -5 -101 -29 275 -290
123+ 439 139 -115 -208 187 -205
124+ 220 17 -111 -192 151 -77
103125```
104126
105127---
106128
107-
108129## Dependencies
109130
110131- C++ compiler with STL support
@@ -118,42 +139,43 @@ Convolution Result:
118139
119140## Build Instructions
120141
121- 1 . Compile the code with any standard C++ compiler :
142+ 1 . Compile all source files together :
122143``` bash
123- g++ main.cpp -o convolution
144+ g++ main.cpp matrix_ops.cpp convolution.cpp -o image_convolution
124145```
125146
1261472 . Run the executable:
127148``` bash
128- ./convolution
149+ ./image_convolution
129150```
130151
131152---
132153
133154## Limitations
134155
135- - Fixed matrix sizes (6×6 image, 3×3 kernel)
136- - Uses simple zero-padding
137- - Random image generation may not be suitable for all use cases
138- - No input validation
139- - Basic edge detection kernel only
156+ - Only supports square matrices
157+ - Limited to grayscale images (single channel)
158+ - Uses simple zero-padding only
159+ - Random image generation (no file input)
160+ - Fixed kernel size (3×3)
161+ - No visualization beyond text output
140162
141163---
142164
143165## Possible Improvements
144166
145- 1 . Make matrix sizes configurable
146- 2 . Add support for image file input
147- 3 . Implement different padding strategies
148- 4 . Add more kernel options
149- 5 . Include normalization of output
150- 6 . Add visualization of results
151- 7 . Optimize convolution operation
152- 8 . Add multi-channel (color) image support
153- 9 . Implement boundary handling options
154- 10 . Add benchmarking capabilities
167+ 1 . Add support for image file input/output
168+ 2 . Implement different padding strategies (reflect, replicate, etc.)
169+ 3 . Support non-square matrices and images
170+ 4 . Add more kernel options and custom kernels
171+ 5 . Implement normalization strategies for outputs
172+ 6 . Add graphical visualization of results
173+ 7 . Support multi-channel (color) image processing
174+ 8 . Implement more boundary handling options
175+ 9 . Add benchmarking capabilities
176+ 10 . Implement multi-threading for performance on larger matrices
155177
156178---
157179
158- This implementation serves as an educational example of 2D convolution in digital image processing .
180+ This implementation demonstrates important concepts of digital image processing while providing a flexible, modular codebase for educational purposes .
159181
0 commit comments