1

I'm currently working through the filter problem, and I'm not sure why it's not working on the pixels on the edge (it also seems to only be incorrect on the B value in RGB). This is the message I'm getting:

:) edges correctly filters middle pixel

:( edges correctly filters pixel on edge
    expected "213 228 255\n", not "213 228 140\n"

:( edges correctly filters pixel in corner
    expected "76 117 255\n", not "76 117 66\n"

:( edges correctly filters 3x3 image
    expected "76 117 255\n21...", not "76 117 66\n213..."

:( edges correctly filters 4x4 image
    expected "76 117 255\n21...", not "76 117 66\n213..."

And here is my code:

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    //Create temporary copy array used to compute edge values
    RGBTRIPLE edge[height]][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            edge[i][j] = image[i][j];
        }
    }

    //Loop through rows
    for (int i = 0; i < height; i++)
    {
        //Loop through columns
        for (int j = 0; j < width; j++)
        {
            float redgx, redgy, greengx, greengy, bluegx, bluegy;

            redgx = redgy = greengx = greengy = bluegx = bluegy = 0;

            //Loop vertically in surrounding pixels
            for (int h = -1; h < 2; h++)
            {
                //Loop horizontally in surrounding pixels
                for (int w = -1; w < 2; w++)
                {
                    //Check if pixel is outside rows, set to black (0) if so
                    if (i + h < 0 || i + h >= height)
                    {
                        //Skip to end of loop for that iteration (because adding 0 means no change to any values)
                        continue;
                    }
                    //Check if pixel is outside columns, set to black (0) if so
                    if (j + w < 0 || j + w >= width)
                    {
                        //Skip to end of loop for that iteration (because adding 0 means no change to any values)
                        continue;
                    }

                    //In both Gx & Gy matrices, when the absolute values of w & h are equal, the Gx and Gy values are equivalent to the h & w values
                    //So multiply RGB values by the same values as w & h in all those instances
                    if (abs(h) == abs(w))
                    {
                        redgx += edge[i + h][j + w].rgbtRed * w;
                        redgy += edge[i + h][j + w].rgbtRed * h;
                        greengx += edge[i + h][j + w].rgbtGreen * w;
                        greengy += edge[i + h][j + w].rgbtGreen * h;
                        bluegx += edge[i + h][j + w].rgbtBlue * w;
                        bluegy += edge[i + h][j + w].rgbtBlue * h;
                    }

                    //If the absolute value of h is greater than absolute value of w, we can multiply the Gy value by twice the h value
                    //We don't need to add anything to the Gx values since those will be multiplied by 0
                    if (abs(h) > abs(w))
                    {
                        redgy += edge[i + h][j + w].rgbtRed * (2*h);
                        greengy += edge[i + h][j + w].rgbtGreen * (2*h);
                        bluegy += edge[i + h][j + w].rgbtBlue * (2*h);
                    }
                    //Do the opposite here for the opposite case
                    if (abs(h) < abs(w))
                    {
                        redgx += edge[i + h][j + w].rgbtRed * (2*w);
                        greengx += edge[i + h][j + w].rgbtGreen * (2*w);
                        bluegx += edge[i + h][j + w].rgbtBlue * (2*w);
                    }
                }

                //Calculate red value as square root of Gx^2 plus Gy^2
                image[i][j].rgbtRed = round(sqrtf((powf(redgx, 2) + powf(redgy, 2))));
                
                //Value cannot be greater than 255
                if (image[i][j].rgbtRed > 255)
                {
                    image[i][j].rgbtRed = 255; 
                }
                
                //Calculate red value as square root of Gx^2 plus Gy^2
                image[i][j].rgbtGreen = round(sqrtf((powf(greengx, 2) + powf(greengy, 2))));
                
                //Value cannot be greater than 255
                if (image[i][j].rgbtGreen > 255)
                {
                    image[i][j].rgbtGreen = 255; 
                }
                
                //Calculate red value as square root of Gx^2 plus Gy^2
                image[i][j].rgbtBlue = round(sqrtf((powf(bluegx, 2) + powf(bluegy, 2))));
                
                //Value cannot be greater than 255
                if (image[i][j].rgbtBlue > 255)
                {
                    image[i][j].rgbtBlue = 255; 
                }
            }
        }
    }
    return;
}

Any help is much appreciated :)

0

I am having exactly the same error and I have found the answer on this answer in another question.

PSET4 filter (more) - edge

typedef struct
{
    BYTE  rgbtBlue;
    BYTE  rgbtGreen;
    BYTE  rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

But i think problem is in Red values.

You must log in to answer this question.

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