Skip to content

Medium-Image-Convolution-Simulation-in-CPP

Latest

Choose a tag to compare

@mhmmdslmnfslnust mhmmdslmnfslnust released this 17 May 13:40

🧮 Advanced Image Convolution with Padding in C++

This project implements a flexible image convolution system using C++ that demonstrates key concepts in digital image processing. It allows users to apply various convolution kernels to dynamically-sized grayscale images with zero-padding.

📌 Overview

  • User-configurable grayscale image generation (size 3×3 to 12×12)
  • Multiple kernel options including edge detection, sharpening, and blur effects
  • Zero-padding applied around the original image for proper boundary handling
  • Modular implementation with separate components for matrix and convolution operations
  • Clean, organized codebase with memory management and input validation

🛠️ Features

  • 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

📷 Sample Output

The original image matrix is:

  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

The kernel matrix is (vertical edge detection):

  -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

💡 Results will vary since the input image is generated with random values.


🧩 Code Structure

The project is divided into modules for better organization and maintainability:

Module Files Purpose
Main Program main.cpp Contains the main control flow
Matrix Operations matrix_ops.h, matrix_ops.cpp Handles matrix creation, display, memory management
Convolution Operations convolution.h, convolution.cpp Manages kernel selection, padding, and convolution

Key Functions

Function Purpose
getValidatedMatrixSize() Gets and validates user input for matrix dimensions
allocateMatrix() Dynamically allocates memory for matrices
generateRandomImage() Creates a matrix with random pixel values (0-255)
selectKernelType() Gets user choice for kernel type
initializeKernel() Sets up the kernel based on user selection
applyPadding() Creates a padded version of the original image
performConvolution() Executes the convolution operation
displayMatrix() Outputs a matrix to the console with formatting
deallocateMatrix() Releases allocated memory

🗂️ File Structure

project/
├── main.cpp              # Contains the main control flow
├── matrix_ops.h          # Declarations for matrix-related functions
├── matrix_ops.cpp        # Implementations of matrix functions
├── convolution.h         # Declarations for convolution-related functions
├── convolution.cpp       # Implementations of convolution functions
├── README.md
└── build/

🚀 How to Run

Compile and execute using a C++ compiler:

g++ main.cpp matrix_ops.cpp convolution.cpp -o image_convolution
./image_convolution

⚠️ Ensure that your compiler supports C++11 or higher.


📊 Improvements from v1.0

  • Added support for user-defined image sizes (previously fixed at 6×6)
  • Implemented multiple kernel options (previously only edge detection)
  • Created a modular code structure with separate files
  • Added input validation for user selections
  • Enhanced memory management with proper allocation/deallocation
  • Improved documentation and code organization

📈 Future Enhancements

  1. Add support for image file input/output
  2. Implement different padding strategies (reflect, replicate)
  3. Support non-square matrices and images
  4. Add custom kernel definition capabilities
  5. Implement normalization strategies for outputs
  6. Add graphical visualization of results
  7. Support multi-channel (color) image processing
  8. Implement more boundary handling options
  9. Add benchmarking capabilities
  10. Implement multi-threading for performance on larger matrices

This release represents a significant evolution of the image convolution system, providing greater flexibility, modularity, and educational value for understanding digital image processing concepts.