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;
}
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.

| improve this answer | |
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

| improve this answer | |
  • OH MY GOD! It worked! I'm so happy! Thank you so much! – Riko Jun 9 at 15:51
  • Glad I could help. It stumped me for days. – Thomas Sullivan Jun 9 at 22:05

You must log in to answer this question.

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