0

I'm having a lot of trouble figuring out how my code for edges is failing so badly every time. Any help on this would be greatly appreciated! Here is what I'm getting back from check50:

:( edges correctly filters middle pixel

expected "210 150 60\n", not "91 222 164\n"

:( edges correctly filters pixel on edge

expected "213 228 255\n", not "161 182 255\n"

:( edges correctly filters pixel in corner

expected "76 117 255\n", not "76 102 255\n"

:( edges correctly filters 3x3 image

expected "76 117 255\n21...", not "76 102 255\n16..."

:( edges correctly filters 4x4 image

expected "76 117 255\n21...", not "76 102 255\n16..."

And here is my code:

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{  
    RGBTRIPLE copy[height][width];

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            copy[h][w] = image[h][w];
        }
    }

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            int gxy_red = gXY_grid(height, width, h, w, copy, 0); // use function gXY_grid to obtain new pixel values
            int gxy_green = gXY_grid(height, width, h, w, copy, 1);
            int gxy_blue = gXY_grid(height, width, h, w, copy, 2);

            copy[h][w].rgbtRed = image[h][w].rgbtRed; // replace copied pixels with original pixels so calculations are not effected 
            copy[h][w].rgbtGreen = image[h][w].rgbtGreen; 
            copy[h][w].rgbtGreen = image[h][w].rgbtBlue;  

            image[h][w].rgbtRed = gxy_red; // replace original pixels with new pixels 
            image[h][w].rgbtGreen = gxy_green;
            image[h][w].rgbtBlue = gxy_blue;
        }
    }
}

int gXY_grid(int height, int width, int y, int x, RGBTRIPLE image[height][width], int colour) // for colour, 0 = Red / 1 = Green / 2 = Blue 
{
    int gx[3][3] = {{-1,0,-1},{-2,0,2},{-1,0,-1}}; // 2D array for gx sobel operator 
    int gy[3][3] = {{-1,-2,-1},{0,0,0},{1,2,1}}; // 2D array for gy sobel operator 

    float red_x = 0.0; // variables to hold the sum(s) of RGB for 9-pixel grid multiplied by the gx sobel operator 
    float green_x = 0.0;
    float blue_x = 0.0;
    float red_y = 0.0; // variables to hold the sum(s) of RGB for 9-pixel grid multiplied by the gy sobel operator 
    float green_y = 0.0;
    float blue_y = 0.0;

    for (int column = y - 1, counter_1 = 0; column < y + 2; column++, counter_1++)
    {
        for (int row = x - 1, counter_2 = 0; row < x + 2; row++, counter_2++)  // iterate through 9 plot grid for gx
        {
            // border check 
            if (column < 0 || column > height -1 || row < 0 || row > width - 1)
            {
                continue; // if there is no pixel there (if column or row == position outside border of image), add 0 (ie. do nothing)
            }
            else // multiply the RGB value of each pixel by the applicable sobel operator value, then add to the appropriate variable
            {
                red_x += image[column][row].rgbtRed * gx[counter_1][counter_2]; 
                green_x += image[column][row].rgbtGreen * gx[counter_1][counter_2]; 
                blue_x += image[column][row].rgbtBlue * gx[counter_1][counter_2]; 

                red_y += image[column][row].rgbtRed * gy[counter_1][counter_2];
                green_y += image[column][row].rgbtGreen * gy[counter_1][counter_2];
                blue_y += image[column][row].rgbtBlue * gy[counter_1][counter_2];
            }
        }
    }
    // use function gX_gY to get the square root of colour_x^2 + colour_y^2
    int gxy_red = gX_gY(red_x, red_y); 
    int gxy_green = gX_gY(green_x, green_y);
    int gxy_blue = gX_gY(blue_x, blue_y);

    if (colour == 0)
        return gxy_red;

    else if (colour == 1)
        return gxy_green;

    else 
        return gxy_blue;
}

// function for obtaining the square root of gx^2 + gy^2
int gX_gY(int gx, int gy) 
{
    int sqr = round(sqrt((gx * gx) + (gy * gy)));

    if (sqr > 255)
    { 
        return 255;
    }

    return sqr;
}
1

There is a little misspell in your gx array in the 'gXY_grid' function. There would be some improvements to optimize your code, but by fixing the array, your program will do the job.

| improve this answer | |
  • Thank you so much. gx[0][2] and gx[2][2] should have been 1 instead of -1. In terms of optimizing my code, what sticks out to you for improvements to be made? I'm just starting out and would love any feedback. – JoshGrant5 Jun 12 at 18:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .