0

I'm stuck with the problem pset4 edges. The checks for the middle pixels work. But not the corner's pixels.
:( edges correctly filters pixel in corner expected "76 117 255\n", not "76 117 66\n"
It looks like I'm getting the right value for the red and green value, but not for the blue one. I checked the math again and again, and I tried to debug in a different program where I created a 3x3 array with the same values that check50 uses. It works properly in the latter, giving me the right values as the check50 ones. I cannot really see where the problem is, I know that I could have wrote the code much better, becasue it's clearly too long, but please help me to find out what's wrong with this one! The following just contains a code for middle pixels and corners pixels. Again the middle pixel works as expected.

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    float red[height][width], green[height][width], blue[height][width];
    float r, g, b, rr, bb, gg;
//copy the values to three different arrays, representing: red, green and blue.
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        red[i][j] = image[i][j].rgbtRed;
        green[i][j] = image[i][j].rgbtGreen;
        blue[i][j] = image[i][j].rgbtBlue;
    }
}
//moves through the pixels
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        //Middle pixels
        if (i > 0 && j < width -1  && i < height -1 && j > 0)
        {
            r = (-1 * red[i-1][j-1]) + red[i-1][j+1] - 2 * red[i][j-1] + 2 * red[i][j+1] - red[i+1] 
                [j-1] + red[i+1][j+1];
            g = (-1 * green[i-1][j-1]) + green[i-1][j+1] -2 * green[i][j-1] + (2 * green[i][j+1]) - 
                green[i+1][j-1] + green[i+1][j+1];
            b = (-1 * blue[i-1][j-1]) + blue[i-1][j+1] -2 * blue[i][j-1] + 2 * blue[i][j+1] - 
                blue[i+1][j-1] + blue[i+1][j+1];

            rr = (-1 * red[i-1][j-1]) -2 * red[i-1][j] - red[i-1][j+1] + red[i+1][j-1] + 2 * red[i+1] 
                  [j] + red[i+1][j+1];
            gg = (-1 * green[i-1][j-1]) -2 * green[i-1][j] - green[i-1][j+1] + green[i+1][j-1] + 2 * 
                  green[i+1][j] + green[i+1][j+1];
            bb = (-1 * blue[i-1][j-1]) -2 * blue[i-1][j] - blue[i-1][j+1] + blue[i+1][j-1] + 2 * 
                  blue[i+1][j] + blue[i+1][j+1];
        }
        //top-left corner
        if (i == 0 && j== 0)
        {
            r = 2 * red[i][j+1] + red[i+1][j+1];
            g = 2 * green[i][j+1] + green[i+1][j+1];
            b = 2 * blue[i][j+1] + blue[i+1][j+1];

            rr = 2 * red[i+1][j] + red[i+1][j+1];
            gg = 2 * green[i+1][j] + green[i+1][j+1];
            bb = 2 * blue[i+1][j] + blue[i+1][j+1];
        }
        //top-right corner
        if (i == 0 && j == width - 1)
        {
            r = -2 * red[i][j-1] - 1 * red[i+1][j-1];
            g = -2 * green[i][j-1] - 1 * green[i+1][j-1];
            b = -2 * blue[i][j-1] - 1 * blue[i+1][j-1];

            rr = red[i+1][j-1] + 2 * red[i+1][j];
            gg = green[i+1][j-1] + 2 * green[i+1][j];
            bb = blue[i+1][j-1] + 2 * blue[i+1][j];
        }
        //bottom-left corner
        if (i == height -1 && j == 0)
        {
            r = red[i-1][j+1] + 2 * red[i][j+1];
            g = green[i-1][j+1] + 2 * green[i][j+1];
            b = blue[i-1][j+1] + 2 * blue[i][j+1];

            rr = -2 * red[i-1][j] - red[i-1][j+1];
            gg = -2 * green[i-1][j] - green[i-1][j+1];
            bb = -2 * blue[i-1][j] - blue[i-1][j+1];
        }
        //bottom-right corner
        if (i == height - 1 && j == width -1)
        {
            r = -1 * red[i-1][j-1] - 2 * red[i][j-1];
            g = -1 * green[i-1][j-1] - 2 * green[i][j-1];
            b = -1 * blue[i-1][j-1] - 2 * blue[i][j-1];

            rr = -1 * red[i-1][j-1] - 2 * red[i -1][j];
            gg = -1 * green[i-1][j-1] - 2 * green[i -1][j];
            bb = -1 * blue[i-1][j-1] - 2 * blue[i -1][j];
        }
        //Compute each value throw the formula
        image[i][j].rgbtRed = round(sqrt((r * r) + (rr * rr)));
        image[i][j].rgbtGreen = round(sqrt((g * g) + (gg * gg)));
        image[i][j].rgbtBlue = round(sqrt((b * b) + (bb * bb)));

        //cap values
        if (image[i][j].rgbtRed > 255)
        {
            image[i][j].rgbtRed = 255;
        }
        if (image[i][j].rgbtGreen > 255)
        {
            image[i][j].rgbtGreen = 255;
        }
        if (image[i][j].rgbtBlue > 255)
        {
            image[i][j].rgbtBlue = 255;
        }
    }
}
return;
}
2
  • for top-left corner for getting r why did you multiply 2 * red[i][j+1] + red[i+1][j+1]; why times two for the first part and times positive one for the second part?
    – AlhasanY
    Oct 19, 2020 at 6:18
  • okay after looking at it for a long time. I think I know why. For Gx you imagine a 3x3 matrix for corner pixels and you take what is left from the Gx matrix. so for the top left corner what is left from Gx is the [1][2] and [2][2] pixels and that is why you multiply 2 and positive 1
    – AlhasanY
    Oct 19, 2020 at 6:46

2 Answers 2

0

I found another way to write my code. Less messy, but it still gives me 66 as blue value, I can't understand. The thing is that it works in a separate program.

0

I had the exact same problem with any result that should be 255. I used if statements at the end to test the values and that resulted in a value of 66 for top left corner blue.

Try creating a function to test for a ceiling of 255 instead of the if statements you have now.

That way you can test all 3 channels with one function. 1) Declare your function prototype inside of Edges (I placed mine right after my working array declaration) 2) Place the function after and outside of Edges 3) Make sure your function returns a value. 4) Incorporate the function (before round)into your image[i][j].rgbtRed = round(sqrt((r * r) + (rr * rr))); as well as the other 2 channels. 5) Good Luck

2
  • OH MY GOD! It worked! I'm so happy! Thank you so much!
    – Riko
    Jun 9, 2020 at 15:51
  • Glad I could help. It stumped me for days. Jun 9, 2020 at 22:05

You must log in to answer this question.

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