Code:
//#include"standard_library.h"
//#include"SuperResolution.h"
#include<stdio.h>
#include<math.h>
void conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float res[][11]);//Function Prototype definition
void conv2D(int rowsKernel, int colsKernel, int rowsImage, int colsImage, float kernel[][5], int image[][15], float res[][11])
{
int kernelSize;//This variable represents the size of the Gaussian kernel
int i; //variable which controls the rows of an image
int j; //variable which controls the columns of an image
int ii; //variable which controls the rows of the kernel
int jj;//variable which controls the columns of the kernel
float sum;//variable that holds the result of convolution for a particular pixel of an image
//float imageConvolved;//Result of an image convolved by a Gaussian kernel
int imagePixel;
float kernelPixel;
//float imageConvolved[11][11];
kernelSize = colsKernel;/*Since we consider a square kernel, then rowsKernel=colsKernel, which implies that the size of the kernel (kernelSize) equals either of these two variables (that is, kernelSize=colsKernel=rowsKernel)*/
for (i = kernelSize/ 2; i < rowsImage - kernelSize / 2; i++) // perform iteration through the rows of an image
{
for (j = kernelSize / 2; j < colsImage - kernelSize / 2; j++) // perform iteration through the columns of an image
{
sum = 0; /*Initializing the accumulator. This variable will finally contain the convolution result for a particular pixel*/
for (ii = - kernelSize / 2; ii <= kernelSize / 2; ii++) // perform iteration through the rows of a kernel
{
for (jj = - kernelSize / 2; jj <= kernelSize / 2; jj++) //perform iteration through the columns of a kernel
{
imagePixel = image[i + ii][j +jj];
kernelPixel = kernel[ii + kernelSize / 2][jj + kernelSize / 2];
sum += imagePixel * kernelPixel;
}
}
res[i-kernelSize/2][j-kernelSize/2] = sum;
}
}
//res=imageConvolved;
//return(imageConvolved);// convolved image
}
int main()
{
float gauss[][5]={
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1}
};
int img[][15]={
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int rowsK = 5,colsK = 5,rowsI = 15,colsI = 15;
float res[11][11];
//float Iconv[11][11];
int i,j,m,n;
for(i=0;i<11;i++)
for(j=0;j<11;j++)
res[i][j]=0.0;
conv2D(rowsK, colsK, rowsI, colsI, gauss, img,res);
for(m=0;m<11;m++)
for(n=0;n<11;n++)
{
printf("%4.0f",res[m][n]);
if(n==10)
printf("\n");
}
return 0;
}
The result when compiled and run is: