Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Detect edges
- void edges(int height, int width, RGBTRIPLE image[height][width])
- {
- // container for original image values
- RGBTRIPLE copy[height][width];
- // select row (height)
- for (int i = 0; i < height; i++)
- {
- // for each pixel in a row (width)
- for (int j = 0; j < width; j++)
- {
- // copy all image original values
- copy[i][j] = image[i][j];
- }
- }
- // select row (height)
- for (int i = 0; i < height; i++)
- {
- // for each pixel in a row (width)
- for (int j = 0; j < width; j++)
- {
- // intialize r g b Gx and Gy value holders and reset them at every j
- int GxR = 0, GxG = 0, GxB = 0, GyR = 0, GyG = 0, GyB = 0, counter = 0;
- //initialize Gx and Gy
- int Gx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
- int Gy[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
- // loop all pixels adjacent (max 3 x 3)!
- for (int m = i - 1; m < i + 2; m++)
- {
- for (int n = j - 1; n < j + 2; n++)
- {
- // set pixels past the border to 0
- if (m < 0 || n < 0 || m > height || n > width)
- {
- copy[m][n].rgbtRed = 0; copy[m][n].rgbtGreen = 0, copy[m][n].rgbtBlue = 0;
- }
- GxR += copy[m][n].rgbtRed * Gx[counter], GxG += copy[m][n].rgbtGreen * Gx[counter], GxB += copy[m][n].rgbtBlue * Gx[counter];
- GyR += copy[m][n].rgbtRed * Gy[counter], GyG += copy[m][n].rgbtGreen * Gy[counter], GyB += copy[m][n].rgbtBlue * Gy[counter];
- counter++;
- }
- }
- image[i][j].rgbtRed = sqrt((GxR * GxR) + (GyR * GyR));
- if (image[i][j].rgbtRed > 255)
- {
- image[i][j].rgbtRed = 255; // cap the result to 8 bit (max 255)
- }
- image[i][j].rgbtGreen = sqrt((GxG * GxG) + (GyG * GyG));
- if (image[i][j].rgbtGreen > 255)
- {
- image[i][j].rgbtGreen = 255; // cap the result to 8 bit (max 255)
- }
- image[i][j].rgbtBlue = sqrt((GxB * GxB) + (GyB * GyB));
- if (image[i][j].rgbtBlue > 255)
- {
- image[i][j].rgbtBlue = 255; // cap the result to 8 bit (max 255)
- }
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment