0

maybe one of you can help me out. The following code compiles and runs without any complaint, but the results are not the expected ones. Somehow the image-edge will be detected, but the new values seem to be incorrect - according to the automated testing (check50).

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    int gxSumRed = 0;
    int gxSumGreen = 0;
    int gxSumBlue = 0;

    int gySumRed = 0;
    int gySumGreen = 0;
    int gySumBlue = 0;

    int sumRedX[height][width];
    int sumGreenX[height][width];
    int sumBlueX[height][width];

    int sumRedY[height][width];
    int sumGreenY[height][width];
    int sumBlueY[height][width];

    int i = 0;
    int j = 0;
    int m = 0;
    int n = 0;

    RGBTRIPLE canvas[height+2][width+2];


    for (i = 0; i <= height + 1; i++)
    {
        for (j = 0; j <= width + 1; j++)
        {
            if (i == 0 || j == 0 || i == height +1 || j == width + 1)
            {
                canvas[i][j].rgbtRed = 0;
                canvas[i][j].rgbtGreen = 0;
                canvas[i][j].rgbtBlue = 0;
            }
        }
    }

    for (i = 1; i <= height; i++)
    {
        for (j = 1; j <= width; j++)
        {
            canvas[i][j].rgbtRed = image[i-1][j-1].rgbtRed;
            canvas[i][j].rgbtGreen = image[i-1][j-1].rgbtGreen;
            canvas[i][j].rgbtBlue = image[i-1][j-1].rgbtBlue;
        }
    }


    // For each row of the image...
    for (i = 1; i <= height; i++)
    {
        //...take each pixel.
        for (j = 1; j <= width; j++)
        {
            //If (current height greater 0 AND current height less than height - 1) AND (current width greater 0 AND current width less than width - 1)...

    //        if ((i > 0 && i < height - 1) && (j > 0 && j < width - 1))
    //       {
                //..  take 3 rows of the picture..
                for (m = i - 1; m <= i + 1; m++)
                {
                    //...and in each row take 3 pixels.
                    for (n = j - 1; n <= j + 1; n++)
                    {

                        //Calculate new values for each color channel in x-direction..
                        if ((m == i - 1 || m == i + 1) && n == j - 1)
                        {
                            gxSumRed += canvas[m][n].rgbtRed * (-1);
                            gxSumGreen += canvas[m][n].rgbtGreen * (-1);
                            gxSumBlue += canvas[m][n].rgbtBlue * (-1);
                        }


                        if ((m == i - 1 || m == i + 1) && n == j + 1)
                        {
                            gxSumRed += canvas[m][n].rgbtRed * 1;
                            gxSumGreen += canvas[m][n].rgbtGreen * 1;
                            gxSumBlue += canvas[m][n].rgbtBlue * 1;
                        }

                        if (m == i && n == j - 1)
                        {
                            gxSumRed += canvas[m][n].rgbtRed * (-2);
                            gxSumGreen += canvas[m][n].rgbtGreen * (-2);
                            gxSumBlue += canvas[m][n].rgbtBlue * (-2);
                        }


                        if (m == i && n == j + 1)
                        {
                            gxSumRed += canvas[m][n].rgbtRed * 2;
                            gxSumGreen += canvas[m][n].rgbtGreen * 2;
                            gxSumBlue += canvas[m][n].rgbtBlue * 2;
                        }

                        if (n == j)
                        {
                            gxSumRed += 0;
                            gxSumGreen += 0;
                            gxSumBlue += 0;
                        }
                    }
                }

                sumRedX[i-1][j-1] = gxSumRed;
                sumGreenX[i-1][j-1] = gxSumGreen;
                sumBlueX[i-1][j-1] = gxSumBlue;

                gxSumRed = 0;
                gxSumGreen = 0;
                gxSumBlue = 0;

         //   }
        }
    }



    // For each column of the image...
    for (i = 1; i <= width; i++)
    {
        //...take each pixel.
        for (j = 1; j <= height; j++)
        {
            //If (current width greater 0 AND current width less than width - 1) AND (current height greater 0 AND current height less than height - 1)...
       //     if ((i > 0 && i < width - 1) && (j > 0 && j < height - 1))
       //     {
                //..  take 3 columns of the picture..
                for (m = - 1; m <= 1; m++)
                {
                    //...and in each column take 3 pixels.
                    for (n = - 1; n <= 1; n++)
                    {

                        //Calculate new values for each color channel in y-direction..
                        if ( n == j + 1 && (m == i + 1 || m == i - 1))
                        {
                            gySumRed += canvas[m][n].rgbtRed * (-1);
                            gySumGreen += canvas[m][n].rgbtGreen * (.1);
                            gySumBlue += canvas[m][n].rgbtBlue * (-1);
                        }


                        if ( n == j - 1 && (m == i + 1 || m == i - 1))
                        {
                            gySumRed += canvas[m][n].rgbtRed * 1;
                            gySumGreen += canvas[m][n].rgbtGreen * 1;
                            gySumBlue += canvas[m][n].rgbtBlue * 1;
                        }


                        if (n == j)
                        {
                            gySumRed += 0;
                            gySumGreen += 0;
                            gySumBlue += 0;
                        }

                        if ( n == j + 1 && m == 0)
                        {
                            gySumRed += canvas[m][n].rgbtRed * (-2);
                            gySumGreen += canvas[m][n].rgbtGreen * (-2);
                            gySumBlue += canvas[m][n].rgbtBlue * (-2);
                        }


                        if ( n == j - 1 && m == 0)
                        {
                            gySumRed += canvas[m][n].rgbtRed * 2;
                            gySumGreen += canvas[m][n].rgbtGreen * 2;
                            gySumBlue += canvas[m][n].rgbtBlue * 2;
                        }
                    }
                }

                sumRedY[i-1][j-1] = gySumRed;
                sumGreenY[i-1][j-1] = gySumGreen;
                sumBlueY[i-1][j-1] = gySumBlue;

                gySumRed = 0;
                gySumGreen = 0;
                gySumBlue = 0;
          //  }
        }
    }


    for (i = 0; i < height; i++)
    {

        for (j = 0; j < width; j++)
        {
            if (round((float)sqrt((sumRedX[i][j] * sumRedX[i][j]) + (sumRedY[i][j] * sumRedY[i][j]))) < 256)
            {
                image[i][j].rgbtRed = round((float)sqrt((sumRedX[i][j] * sumRedX[i][j]) + (sumRedY[i][j] * sumRedY[i][j])));
            }

            else
            {
                image[i][j].rgbtRed = 255;
            }


            if (round((float)sqrt((sumGreenX[i][j] * sumGreenX[i][j]) + (sumGreenY[i][j] * sumGreenY[i][j]))) < 256)
            {
                image[i][j].rgbtGreen = round((float)sqrt((sumGreenX[i][j] * sumGreenX[i][j]) + (sumGreenY[i][j] * sumGreenY[i][j])));
            }

            else
            {
                image[i][j].rgbtGreen = 255;
            }


            if (round((float)sqrt((sumBlueX[i][j] * sumBlueX[i][j]) + (sumBlueY[i][j] * sumBlueY[i][j]))) < 256)
            {
                image[i][j].rgbtBlue = round((float)sqrt((sumBlueX[i][j] * sumBlueX[i][j]) + (sumBlueY[i][j] * sumBlueY[i][j])));
            }

            else
            {
                image[i][j].rgbtRed = 255;
            }
        }
    }           
    return;
}
1

First of all, you should consider revise your code in order to group some of those for loops and reduce the number of lines. It will be more readable for you and for others.

You have two separate group of loops for getting the values in gxsum.... and gysum... when it would be easier to get them in one group. In fact the conditions of the 3x3 matrix around the current pixel should be the same for gx and gy, and they are not. The only diference between them, should be the value you multiply them with.

1
  • Thank you for the hint. I've shortened the code a lot and the separated loops are have been merged. Its still not working perfectly but I am on the right way. – ALL May 10 '20 at 18:02
1

So, now it is a lot shorter (thanks to the inspiration by Ayam Chan) and works fine. Am I allowed to publish the complete code? Or is that not appreciated?

Greetz

1
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review – Cliff B Aug 22 '20 at 3:31

You must log in to answer this question.

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