Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void convolve(unsigned char out[][SIZE][RGB], unsigned char in[][SIZE][RGB],
- int N, double kernel[][11])
- {
- int padded[SIZE+10][SIZE+10][RGB]; // Use for input image with appropriate
- // padding
- int temp[SIZE][SIZE][RGB]; // Use for the unclamped output pixel
- // values then copy from temp to out,
- // applying clamping
- //first set all of padded to 0 (black)
- for (int x = 0; x < SIZE+10; x++)
- {
- for (int y = 0; y < SIZE+10; y++)
- {
- if (x < 5 || x > SIZE + 5)
- {
- for (int k = 0; k < 3; k++)
- {
- padded[x][y][k] = 0;
- }
- }
- if( y < 5 || y > SIZE + 5)
- {
- for (int k = 0; k < 3; k++)
- {
- padded[x][y][k] = 0;
- }
- }
- }
- }
- //now copy input into padding to appropriate locations
- for (int i = 5; i < SIZE + 5; i ++)
- {
- for (int j = 5; j < SIZE + 5; j++)
- {
- for (int k = 0; k < 3; k++)
- {
- padded[i][j][k] = in[i-5][j-5][k];
- }
- }
- }
- //initialize temp pixels to 0 (black)
- for (int f = 0; f < SIZE; f++)
- {
- for (int u = 0; u < SIZE; u++)
- {
- for (int k = 0; k < 3; k++)
- {
- temp[f][u][k] = 0;
- }
- }
- }
- //now perform convolve (using convolution equation on each pixel of the
- // actual image) placing the results in temp (i.e. unclamped result)
- //Here we give you the structure of the convolve for-loops, you need
- //to figure out the loop limits
- for(int y = 0; y < SIZE; y++)
- for(int x = 0; x < SIZE; x++)
- for(int k=0;k<RGB;k++)
- for(int i= 0; i<= 3 ; i++)
- for(int j= 0 ; j<= 3 ; j++)
- temp[y][x][k] += padded[y][x][k]*kernel[i][j];
- //now clamp and copy to output
- // You may need to cast to avoid warnings from the compiler:
- // (i.e. out[i][j][k] = (unsigned char) temp[i][j][k];)
- for (int y = 0; y < SIZE; y++)
- {
- for (int x = 0; x < SIZE; x++)
- {
- for (int k = 0; k < RGB; k++)
- {
- if (temp[y][x][k] < 0)
- {
- temp[y][x][k] = 0;
- }
- else if (temp[y][x][k] > 255)
- {
- temp[y][x][k] = 255;
- }
- }
- }
- }
- for (int y = 0; y < SIZE; y++)
- {
- for (int x = 0; x < SIZE; x++)
- {
- for (int k = 0; k < RGB; k++)
- {
- out[y][x][k] = (unsigned char) temp[y][x][k];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement