Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
The output image is majorly white, with some outlines of the edges

C
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temporary[height][width];

    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
            temporary[row][column] = image[row][column];
        }
    }
    for (int row = 0; row < height; row++)
    {
        for (int column = 0; column < width; column++)
        {
           image[row][column] = determineEdges(row, column, height, width, image, temporary);
        }
    }
    return;
}




//i and j are the current pixels
RGBTRIPLE determineEdges (int i, int j, int height, int width, RGBTRIPLE image[height][width], RGBTRIPLE temp[height][width])
{

    float sum_surround_redX = 0, sum_surround_greenX = 0, sum_surround_blueX = 0, sum_surround_redY = 0, sum_surround_greenY = 0, sum_surround_blueY = 0;
    float newRed = 0, newGreen = 0, newBlue = 0;
    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}
         };

    //ident throughout the circundant pixels
    for (int row_idx =  i - 1; row_idx <= i + 1; row_idx++)
    {
        for (int column_idx = j - 1; column_idx <= j + 1; column_idx++)
        {

            //checks for outta boundaries pixels, if so, skips. That avoid segmentation fault
            if (row_idx < 0 || column_idx < 0 || row_idx > height - 1 || column_idx > width - 1)
            {
                continue;
            }

            //sums the RGB surrounding pixels according to the leverage established in Gx and Gy
            sum_surround_redX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtRed;
            sum_surround_greenX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtGreen;
            sum_surround_blueX += Gx[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtBlue;

            sum_surround_redY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtRed;
            sum_surround_greenY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtGreen;
            sum_surround_blueY += Gy[row_idx - i + 1][column_idx - j + 1] * image[row_idx][column_idx].rgbtBlue;
        }
    }

        //assigns a new value for the pixels RGB according to square of Gx^2 + Gy^2
         newRed = round (sqrt (pow (sum_surround_redX, 2) + pow (sum_surround_redY, 2)));
         newGreen = round (sqrt (pow (sum_surround_greenX, 2) + pow (sum_surround_greenY, 2)));
         newBlue = round (sqrt (pow (sum_surround_blueX, 2) + pow (sum_surround_blueY, 2)));

                //garantees that no color has higher assigns than 255, and attach the new pixels values of RGB to a RGBTRIPLE struct
                if(newRed > 255)
                {
                    newRed = 255;
                }
                if(newGreen > 255)
                {
                    newGreen = 255;
                }
                if(newBlue > 255)
                {
                    newBlue = 255;
                }

                temp[i][j].rgbtRed = newRed;
                temp[i][j].rgbtGreen = newGreen;
                temp[i][j].rgbtBlue = newBlue;

    return temp[i][j];

}


What I have tried:

I've used debug50, but the variables seem to be correctly updated.
I've changed parameters, but no change succeed.
Posted
Updated 15-Jul-23 13:15pm
v5
Comments
Dave Kreskowiak 15-Jul-23 9:27am    
Next time, you're going to have to explain what "my code in cs50's code that applies a filter..." means. Who is "cs50"? Where did the code come from?

You have to provide context to what you're talking about.

Quote:
The function determineEdges was being passed to image before it was completely formed.

This sounds a lot like a concurrency. Simply copying may not be enough.

I would still suggest instead of copying everything in multiple loops one by one better to use memcpy. The creation of large arrays on the stack can also lead to problems.

This code:
C
RGBTRIPLE temporary[height][width];
for (int row = 0; row < height; row++) {
    for (int column = 0; column < width; column++) {
        temporary[row][column] = image[row][column];
    }
}

I would suggest to change to:
C
void copyImage(const RGBTRIPLE* source, RGBTRIPLE* destination, int height, int width)
{
    size_t imageMemSize = height * width * sizeof(RGBTRIPLE);
    memcpy(destination, source, imageMemSize);
}
    
RGBTRIPLE* temporary = malloc(height * width * sizeof(RGBTRIPLE));
copyImage(image, temporary, height, width);

Of course, the memory must also be released again.
 
Share this answer
 
chatgpt helpred me with this one ....
The function determineEdges was being passed to image before it was completely formed. So once that function use as a parameter image, image was getting changed and interfering in determineEdges.
A silly error of mine, but that it is
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900