0

I am trying to code CS50's Pset4 Filter (More comfortable)'s edges function, but I keep on getting a different value.

This is the link to the video explaining what I am trying to do in this function, and I hope this would be easier to understand than me trying to explain everything. https://www.youtube.com/watch?v=PnVQN4vzC4U&list=PLhQjrBD2T382OwvMbZuaMGtD9wZkhnhYj&index=5

I have no problem compiling this code, but somehow I keep on getting a different value than what is expected.

This is the kind of output I got for a 4x4 image when I used check50. (I could not find any pattern in the output I am getting.)

It would be great if anyone can help me find a problem with this code. (Sorry, I am a coding beginner so the code might be difficult to understand or inefficient.)

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    // Creating a copy of the image
    
    RGBTRIPLE cimage[height][width];
    
    // Copying every RGB value of the original image to the copy
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cimage[i][j] = image[i][j];
        }
    }
    
    // Creating an array for the algorithm that I am using
    
    int gx[3][3] = {{ -1, 0, -1 }, { -2, 0, 2 }, { -1, 0, 1 }};
    int gy[3][3] = {{ -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 }};
    
    // Creating a loop to modify the pixels one by one
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {   
            // Creating a loop to check each of the values of the surrounding pixels (a is the y value and b is the x value)
            
            for (int a = -1; a < 2; a++)
            {
                // If the pixel that is being checked does not exist, skips the iteration to ignore the current pixel and go to the next pixel
                
                if (i + a < 0 || i + a > height - 1)
                {
                    continue;
                }
                
                for (int b = -1; b < 2; b++)
                {
                    // If the pixel that is being checked does not exist, skips the iteration to ignore the current pixel and go to the next pixel
                    
                    if (j + b < 0 || j + b > width - 1)
                    {
                        continue;
                    }
                    
                    // Accumulating the Gx value for each color by multiplying the current pixel by the appropriate Gx value
                    
                    int Gx = gx[a + 1][b + 1];
                    sumRedx += Gx * cimage[i + a][j + b].rgbtRed;
                    sumGreenx += Gx * cimage[i + a][j + b].rgbtGreen;
                    sumBluex += Gx * cimage[i + a][j + b].rgbtBlue;
                    
                    // Accumulating the Gy value for each color by multiplying the current pixel by the appropriate Gy value
                    
                    int Gy = gy[a + 1][b + 1];
                    sumRedy += Gy * cimage[i + a][j + b].rgbtRed;
                    sumGreeny += Gy * cimage[i + a][j + b].rgbtGreen;
                    sumBluey += Gy * cimage[i + a][j + b].rgbtBlue;
                }
            }
        
        // Calculating the appropriate value for the pixel by taking the square root of (Gx^2 + Gy^2) and rounding that to the nearest integer
        
        value = round(fmin(255,sqrt(sumRedx * sumRedx + sumRedy * sumRedy)));
        image[i][j].rgbtRed = value;
        
        // Iterating the same steps for Green and Blue values
        
        value = round(fmin(255,sqrt(sumGreenx * sumGreenx + sumGreeny * sumGreeny)));
        image[i][j].rgbtGreen = value;
        value = round(fmin(255,sqrt(sumBluex * sumBluex + sumBluey * sumBluey)));
        image[i][j].rgbtBlue = value;
        
        // Resetting the values
        
        int sumRedx = 0;
        int sumGreenx = 0;
        int sumBluex = 0;
        int sumRedy = 0;
        int sumGreeny = 0;
        int sumBluey = 0;
        }
    }
    return;
}
1

Two problems with your code:

  • First: The group of int variables 'sumRedx, sumGreenx' etc need to be declared before using them. As they are now, the code won't compile.

  • Second: There is a misspell in your 'gx' array.

1
  • Thank you so much for your answer! I can't believe I missed that misspell... – RK-83 Jul 26 '20 at 3:27

You must log in to answer this question.

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