Last active
November 6, 2024 12:42
-
-
Save omaraflak/aca9d0dc8d583ff5a5dc16ca5cdda86a to your computer and use it in GitHub Desktop.
Image convolution in C++ + Gaussian blur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <assert.h> | |
#include <cmath> | |
#include <png++/png.hpp> | |
using namespace std; | |
typedef vector<double> Array; | |
typedef vector<Array> Matrix; | |
typedef vector<Matrix> Image; | |
Matrix getGaussian(int height, int width, double sigma) | |
{ | |
Matrix kernel(height, Array(width)); | |
double sum=0.0; | |
int i,j; | |
for (i=0 ; i<height ; i++) { | |
for (j=0 ; j<width ; j++) { | |
kernel[i][j] = exp(-(i*i+j*j)/(2*sigma*sigma))/(2*M_PI*sigma*sigma); | |
sum += kernel[i][j]; | |
} | |
} | |
for (i=0 ; i<height ; i++) { | |
for (j=0 ; j<width ; j++) { | |
kernel[i][j] /= sum; | |
} | |
} | |
return kernel; | |
} | |
Image loadImage(const char *filename) | |
{ | |
png::image<png::rgb_pixel> image(filename); | |
Image imageMatrix(3, Matrix(image.get_height(), Array(image.get_width()))); | |
int h,w; | |
for (h=0 ; h<image.get_height() ; h++) { | |
for (w=0 ; w<image.get_width() ; w++) { | |
imageMatrix[0][h][w] = image[h][w].red; | |
imageMatrix[1][h][w] = image[h][w].green; | |
imageMatrix[2][h][w] = image[h][w].blue; | |
} | |
} | |
return imageMatrix; | |
} | |
void saveImage(Image &image, const char *filename) | |
{ | |
assert(image.size()==3); | |
int height = image[0].size(); | |
int width = image[0][0].size(); | |
int x,y; | |
png::image<png::rgb_pixel> imageFile(width, height); | |
for (y=0 ; y<height ; y++) { | |
for (x=0 ; x<width ; x++) { | |
imageFile[y][x].red = image[0][y][x]; | |
imageFile[y][x].green = image[1][y][x]; | |
imageFile[y][x].blue = image[2][y][x]; | |
} | |
} | |
imageFile.write(filename); | |
} | |
Image applyFilter(Image &image, Matrix &filter){ | |
assert(image.size()==3 && filter.size()!=0); | |
int height = image[0].size(); | |
int width = image[0][0].size(); | |
int filterHeight = filter.size(); | |
int filterWidth = filter[0].size(); | |
int newImageHeight = height-filterHeight+1; | |
int newImageWidth = width-filterWidth+1; | |
int d,i,j,h,w; | |
Image newImage(3, Matrix(newImageHeight, Array(newImageWidth))); | |
for (d=0 ; d<3 ; d++) { | |
for (i=0 ; i<newImageHeight ; i++) { | |
for (j=0 ; j<newImageWidth ; j++) { | |
for (h=i ; h<i+filterHeight ; h++) { | |
for (w=j ; w<j+filterWidth ; w++) { | |
newImage[d][i][j] += filter[h-i][w-j]*image[d][h][w]; | |
} | |
} | |
} | |
} | |
} | |
return newImage; | |
} | |
Image applyFilter(Image &image, Matrix &filter, int times) | |
{ | |
Image newImage = image; | |
for(int i=0 ; i<times ; i++) { | |
newImage = applyFilter(newImage, filter); | |
} | |
return newImage; | |
} | |
int main() | |
{ | |
Matrix filter = getGaussian(5, 5, 10.0); | |
cout << "Loading image..." << endl; | |
Image image = loadImage("image.png"); | |
cout << "Applying filter..." << endl; | |
Image newImage = applyFilter(image, filter); | |
cout << "Saving image..." << endl; | |
saveImage(newImage, "newImage.png"); | |
cout << "Done!" << endl; | |
} |
int newImageHeight = height-filterHeight+1; int newImageWidth = width-filterWidth+1;
Why output image differs in size from original?
Shouldn't it be the same?
output size will be the same as input if you do padding with the input.
int newImageHeight = (height-filterHeight+2*padding)/(stride) + 1
same formula for width.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nasirco2020 You have to include the png++ library. In Ubuntu try with
sudo apt-get install png++
Also, when you compile, don't forget to add the flag
-lpng