0

the corner check correct and the edges check correct and middle pixel check correct but the whole image wrong why ?????

    void edges(int height, int width, RGBTRIPLE image[height][width])
{
    int R,B,G;
    double redsum,bluesum,greensum;
    RGBTRIPLE original[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            original[i][j] = image[i][j];
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            double redsumX = 0, bluesumX = 0, greensumX = 0;
            double redsumY = 0, bluesumY = 0, greensumY = 0;
            double redsum = 0, bluesum = 0, greensum = 0;
            if (j == 0 && i == 0)
            {
                int r = 1, c = 1;
                for (int k = i; k <= i + 1; k++)
                {
                    for (int p = j; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 1;
                        }
                    }
                }
            }
            else if (j == width - 1 && i == 0)
            {
                int r = 1, c = 0;
                for (int k = i; k <= i + 1; k++)
                {
                    for (int p = j; p >= j - 1; p--)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 1)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            else if (i == height - 1 && j == 0)
            {
                int r = 0, c = 1;
                for (int k = i; k >= i - 1; k--)
                {
                    for (int p = j; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 1;
                        }
                    }
                }
            }
            else if (i == height - 1 && j == width - 1)
            {
                int r = 0, c = 0;
                for (int k = i; k >= i - 1; k--)
                {
                    for (int p = j; p >= j - 1; p--)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 1)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            else if (j == 0)
            {
                int r = 0, c = 1;
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int p = j; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 1;
                        }
                    }
                }
            }
            else if (j == width - 1)
            {
                int r = 0, c = 0;
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int p = j; p >= j - 1; p--)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 1)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            else if (i == 0)
            {
                int r = 1, c = 0;
                for (int k = i; k <= i + 1; k++)
                {
                    for (int p = j - 1; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            else if (i == height - 1)
            {
                int r = 0, c = 0;
                for (int k = i; k >= i - 1; k--)
                {
                    for (int p = j - 1; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX += original[k][p].rgbtRed * Gx[r][c];
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            else
            {
                int r = 0, c = 0;
                for (int k = i - 1; k <= i + 1; k++)
                {
                    for (int p = j - 1; p <= j + 1; p++)
                    {
                        //Gx
                        redsumX = original[k][p].rgbtRed * Gx[r][c] + redsumX;
                        bluesumX += original[k][p].rgbtBlue * Gx[r][c];
                        greensumX += original[k][p].rgbtGreen * Gx[r][c];
                        //Gy
                        redsumY += original[k][p].rgbtRed * Gy[r][c];
                        bluesumY += original[k][p].rgbtBlue * Gy[r][c];
                        greensumY += original[k][p].rgbtGreen * Gy[r][c];
                        c++;
                        if (c > 2)
                        {
                            r++;
                            c = 0;
                        }
                    }
                }
            }
            //Gx^2
            redsumX = pow(redsumX,2);
            bluesumX = pow(bluesumX,2);
            greensumX = pow(greensumX,2);
            //Gy^2
            redsumY = pow(redsumY,2);
            bluesumY = pow(bluesumY,2);
            greensumY = pow(greensumY,2);
            //Gx^2 + Gy^2
            redsum = redsumX + redsumY;
            bluesum = bluesumX + bluesumY;
            greensum = greensumX + greensumY;
            //sqrt()
            redsum = sqrt(redsum);
            bluesum = sqrt(bluesum);
            greensum = sqrt(greensum);
            R = round(redsum);
            B = round(bluesum);
            G = round(greensum);
            if (R > 255)
            {
                R = 255;
            }
            if (B > 255)
            {
                B = 255;
            }
            if (G > 255)
            {
                G = 255;
            }
            image[i][j].rgbtRed = R;
            image[i][j].rgbtBlue = B;
            image[i][j].rgbtGreen = G;
        }

    }
    return;
}

enter image description here enter image description here enter image description here

1

Your k-- and p-- don't match the c++ and r++. For example, you have

                int r = 0, c = 0;
                for (int k = i; k >= i - 1; k--)

which starts at the upper left in your matrix (r=0), but in the middle row for the 3x3 window.

I would assume that you could set your r to k - i + 1 and c to p - j + 1, this should remove that problem.

You must log in to answer this question.

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