Neinhalt

filter-more

May 30th, 2022
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. // Detect edges
  2. void edges(int height, int width, RGBTRIPLE image[height][width])
  3. {
  4.     // container for original image values
  5.     RGBTRIPLE copy[height][width];
  6.  
  7.     // select row (height)
  8.     for (int i = 0; i < height; i++)
  9.     {
  10.         // for each pixel in a row (width)
  11.         for (int j = 0; j < width; j++)
  12.         {
  13.             // copy all image original values
  14.             copy[i][j] = image[i][j];
  15.         }
  16.     }
  17.  
  18.     // select row (height)
  19.     for (int i = 0; i < height; i++)
  20.     {
  21.         // for each pixel in a row (width)
  22.         for (int j = 0; j < width; j++)
  23.         {
  24.             // intialize r g b Gx and Gy value holders and reset them at every j
  25.             int GxR = 0, GxG = 0, GxB = 0, GyR = 0, GyG = 0, GyB = 0, counter = 0;
  26.  
  27.             //initialize Gx and Gy
  28.             int Gx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
  29.             int Gy[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
  30.  
  31.             // loop all pixels adjacent (max 3 x 3)!
  32.             for (int m = i - 1; m < i + 2; m++)
  33.             {
  34.                 for (int n = j - 1; n < j + 2; n++)
  35.                 {
  36.                     // set pixels past the border to 0
  37.                     if (m < 0 || n < 0 || m > height || n > width)
  38.                     {
  39.                         copy[m][n].rgbtRed = 0; copy[m][n].rgbtGreen = 0, copy[m][n].rgbtBlue = 0;
  40.                     }
  41.  
  42.                     GxR += copy[m][n].rgbtRed * Gx[counter], GxG += copy[m][n].rgbtGreen * Gx[counter], GxB += copy[m][n].rgbtBlue * Gx[counter];
  43.                     GyR += copy[m][n].rgbtRed * Gy[counter], GyG += copy[m][n].rgbtGreen * Gy[counter], GyB += copy[m][n].rgbtBlue * Gy[counter];
  44.                     counter++;
  45.                 }
  46.             }
  47.             image[i][j].rgbtRed = sqrt((GxR * GxR) + (GyR * GyR));
  48.             if (image[i][j].rgbtRed > 255)
  49.             {
  50.                 image[i][j].rgbtRed = 255; // cap the result to 8 bit (max 255)
  51.             }
  52.             image[i][j].rgbtGreen = sqrt((GxG * GxG) + (GyG * GyG));
  53.             if (image[i][j].rgbtGreen > 255)
  54.             {
  55.                 image[i][j].rgbtGreen = 255; // cap the result to 8 bit (max 255)
  56.             }
  57.             image[i][j].rgbtBlue = sqrt((GxB * GxB) + (GyB * GyB));
  58.              if (image[i][j].rgbtBlue > 255)
  59.             {
  60.                 image[i][j].rgbtBlue = 255; // cap the result to 8 bit (max 255)
  61.             }
  62.         }
  63.     }
  64.     return;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment