Advertisement
nirajs

convolution series naive

Oct 2nd, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4.  
  5. #define Image_Height 20000
  6. #define Image_Width  10000
  7. #define Kernel_Dim 5  // convolutin kernel will be of width [2(r)+1]*[2(r)+1]
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     // Intializing image size and  intially seeding it with some randome values 0-255 (gray scale)
  13.  
  14.     int **image, **out_image;
  15.  
  16.     image = new int *[Image_Height];
  17.     for(int i=0;i<Image_Height;i++)
  18.     image[i] = new int[Image_Width];
  19.  
  20.     out_image = new int *[Image_Height];
  21.     for(int i=0;i<Image_Height;i++)
  22.     out_image[i] = new int[Image_Width];
  23.  
  24.  
  25.  
  26.     for(int i=0;i<Image_Height;i++)
  27.     for(int j=0;j<Image_Width;j++)
  28.     image[i][j]=rand()%256;
  29.  
  30.  
  31.     // Intializing kernel let say guassian kernel of 5*5
  32.  
  33.     int kernel[Kernel_Dim][Kernel_Dim]={};
  34.     int a[5]={1,4,6,4,1}; // to get gussian kernel by multiplying a*a we get gussian kernel of size 5*5
  35.  
  36.     for(int k=0;k<5;k++)
  37.     for(int i=0;i<Kernel_Dim;i++)
  38.     for(int j=0;j<Kernel_Dim;j++)
  39.     kernel[i][j]=a[i]*a[j];
  40.  
  41.  
  42.   int i,j,k,l,sum,r=2,count=0;
  43.   // Main calulation done here multiplying image with kernel matrix
  44.      // boundaries values skipped
  45.   for(i=2;i<Image_Height-2;i++)
  46.    {
  47.        for(j=2;j<Image_Width-2;j++)
  48.     {
  49.           sum=0;
  50.           for(k=-r;k<r;k++)
  51.             {
  52.                 for(l=-r;l<r;l++)
  53.                 {
  54.                     sum=sum+image[i+k][j+l]*kernel[k][l];
  55.  
  56.                 }
  57.             }
  58.             out_image[i][j]=sum;
  59.     }
  60.  
  61.    }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement