0

There are lots of runtime errors and overflowed integers with my code, and I have no idea why that is the case. Please could you check it out?

void edges(int height, int width, RGBTRIPLE image[height][width])
{
    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}};

    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            // create the new pixels
            int nrx = 0, nbx = 0, ngx = 0;
            int nry = 0, nby = 0, ngy = 0;
            // loop 3x3
            for (int k = 0; k < 3; k++)
            {
                for (int l = 0; l < 3; l++)
                {
                    // -1 is to make up for the offset
                    // conditions to make sure there's no IndexError
                    if (i + k - 1 >=0 && i + k - 1 < height)
                    {
                        if (j + l - 1 >= 0 && j + l - 1 < width)
                        {
                            // RED
                            nrx += copy[i + k - 1][j + l - 1].rgbtRed * Gx[k][j];
                            nry += copy[i + k - 1][j + l - 1].rgbtRed * Gy[k][j];

                            // GREEN
                            ngx += copy[i + k - 1][j + l - 1].rgbtGreen * Gx[k][j];
                            ngy += copy[i + k - 1][j + l - 1].rgbtGreen * Gy[k][j];

                            // BLUE
                            nbx += copy[i + k - 1][j + l - 1].rgbtBlue * Gx[k][j];
                            nby += copy[i + k - 1][j + l - 1].rgbtBlue * Gy[k][j];


                        }
                    }
                }
            }

            // compute the sqrt of the added squares
            int nr = round(sqrt(nrx * nrx + nry * nry));
            int ng = round(sqrt(ngx * ngx + ngy * ngy));
            int nb = round(sqrt(nbx * nbx + nby * nby));

            // check for below 255
            if (nr < 255)
            {   nr = 255;   }

            if (ng < 255)
            {   ng = 255;   }

            if (nb < 255)
            {   nb = 255;   }


            image[i][j].rgbtRed = nr;
            image[i][j].rgbtGreen = ng;
            image[i][j].rgbtBlue = nb;


        }
    }

    return;
}

Please do look into this, as I really would like to complete the assignment, but am quite confused :D.

1
  • Can you show us what your code outputs.
    – Ojou Nii
    May 9 '20 at 14:04
0

Did you notice the hint on the pset4-website? I don't know if it changes your result so much, but you can try to float the sqrt-calculation. Above you do that part only with ints. What does the automated test (check50) say? Are your values at least close to the expected ones? If it works, tell me! I am doing this one myself at the moment and I have got quite similar problems. The picture looks not that bad, but the automated test fails completely due to the output values.

Another one: when I read your code correctly, the way of taking the new-variables ist not correct. The program will start with i, j == 0, 0 but then your loop for the surrounding pixels starts also at m, n == 0, 0. So, you are not taking the values of the surrounding pixels but the values of nine pixels with its starting point bottom left.

0

You must log in to answer this question.

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