Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. void convolve(unsigned char out[][SIZE][RGB], unsigned char in[][SIZE][RGB],
  2. int N, double kernel[][11])
  3. {
  4.  
  5. int padded[SIZE+10][SIZE+10][RGB]; // Use for input image with appropriate
  6. // padding
  7. int temp[SIZE][SIZE][RGB]; // Use for the unclamped output pixel
  8. // values then copy from temp to out,
  9. // applying clamping
  10.  
  11. //first set all of padded to 0 (black)
  12. for (int x = 0; x < SIZE+10; x++)
  13. {
  14. for (int y = 0; y < SIZE+10; y++)
  15. {
  16. if (x < 5 || x > SIZE + 5)
  17. {
  18. for (int k = 0; k < 3; k++)
  19. {
  20. padded[x][y][k] = 0;
  21. }
  22. }
  23.  
  24. if( y < 5 || y > SIZE + 5)
  25. {
  26. for (int k = 0; k < 3; k++)
  27. {
  28. padded[x][y][k] = 0;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. //now copy input into padding to appropriate locations
  35.  
  36. for (int i = 5; i < SIZE + 5; i ++)
  37. {
  38. for (int j = 5; j < SIZE + 5; j++)
  39. {
  40. for (int k = 0; k < 3; k++)
  41. {
  42. padded[i][j][k] = in[i-5][j-5][k];
  43. }
  44. }
  45. }
  46.  
  47.  
  48. //initialize temp pixels to 0 (black)
  49.  
  50. for (int f = 0; f < SIZE; f++)
  51. {
  52. for (int u = 0; u < SIZE; u++)
  53. {
  54. for (int k = 0; k < 3; k++)
  55. {
  56. temp[f][u][k] = 0;
  57. }
  58.  
  59. }
  60. }
  61.  
  62.  
  63. //now perform convolve (using convolution equation on each pixel of the
  64. // actual image) placing the results in temp (i.e. unclamped result)
  65. //Here we give you the structure of the convolve for-loops, you need
  66. //to figure out the loop limits
  67. for(int y = 0; y < SIZE; y++)
  68. for(int x = 0; x < SIZE; x++)
  69. for(int k=0;k<RGB;k++)
  70. for(int i= 0; i<= 3 ; i++)
  71. for(int j= 0 ; j<= 3 ; j++)
  72. temp[y][x][k] += padded[y][x][k]*kernel[i][j];
  73.  
  74.  
  75. //now clamp and copy to output
  76. // You may need to cast to avoid warnings from the compiler:
  77. // (i.e. out[i][j][k] = (unsigned char) temp[i][j][k];)
  78.  
  79. for (int y = 0; y < SIZE; y++)
  80. {
  81. for (int x = 0; x < SIZE; x++)
  82. {
  83. for (int k = 0; k < RGB; k++)
  84. {
  85. if (temp[y][x][k] < 0)
  86. {
  87. temp[y][x][k] = 0;
  88. }
  89. else if (temp[y][x][k] > 255)
  90. {
  91. temp[y][x][k] = 255;
  92. }
  93.  
  94. }
  95. }
  96. }
  97.  
  98. for (int y = 0; y < SIZE; y++)
  99. {
  100. for (int x = 0; x < SIZE; x++)
  101. {
  102. for (int k = 0; k < RGB; k++)
  103. {
  104. out[y][x][k] = (unsigned char) temp[y][x][k];
  105.  
  106. }
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement