0

I've been stuck for hours trying to solve this, so I would appreciate someones help lol

So my edges filter is only passing the test for the middle pixel:

enter image description here

I can't figure out what is wrong with my code (and my logic), so any help is welcomed :)

int cap(int value)
{
    if (value < 255)
    {
        return value;
    } 
    else
    {
        return 255;
    }
}

RGBTRIPLE getPixel(int height, int width, RGBTRIPLE image[height][width], int row, int col)
{
    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}};
    int gxRed, gxGreen, gxBlue, gyRed, gyGreen, gyBlue;
    gxRed = gxGreen = gxBlue = gyRed = gyGreen = gyBlue = 0;
    
    for (int i = -1; i < 2; i++)
    {
        int newRow = row + i;
        if (newRow < 0 || newRow > height)
        {
            continue;
        }
        for (int j = -1; j < 2; j++)
        {
            int newCol = col + j;
            if (newCol < 0 || newCol > width)
            {
                continue;
            }
            
            int xWeight = gx[newRow][newCol];
            gxRed += xWeight * image[newRow][newCol].rgbtRed;
            gxGreen += xWeight * image[newRow][newCol].rgbtGreen;
            gxBlue += xWeight * image[newRow][newCol].rgbtBlue;
            
            int yWeight = gy[newRow][newCol];
            gyRed += yWeight * image[newRow][newCol].rgbtRed;
            gyGreen += yWeight * image[newRow][newCol].rgbtGreen;
            gyBlue += yWeight * image[newRow][newCol].rgbtBlue;
        }
    }
    
    RGBTRIPLE currentPixel;
    currentPixel.rgbtRed = cap(round(sqrt(gxRed * gxRed + gyRed * gyRed)));
    currentPixel.rgbtGreen = cap(round(sqrt(gxGreen * gxGreen + gyGreen * gyGreen)));
    currentPixel.rgbtBlue = cap(round(sqrt(gxBlue * gxBlue + gyBlue * gyBlue)));
    
    return currentPixel;
}

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];
    
    // Creates a copy of the image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = getPixel(height, width, image, i, j);
        }
    }
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = copy[i][j];
        }
    }
    return;
}

You must log in to answer this question.

Browse other questions tagged .