0

this is my attempt on cs50 more comfortable pset4 edges part

I cannot find my mistake.

I get a mostly white image with some repeating noise as output.

I have tried simplifying more complex expressions that coul cause problems, I even double checked my code with a youtube video

int limit(int value)
{
    return value < 255 ? value : 255;
}

RGBTRIPLE edged_pixel(int i, int j, int height, int width, RGBTRIPLE image[width][height])
{
    int g[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, -1}};
    int rx, ry, gx, gy, bx, by; rx = ry = gx = gy = bx = by = 0;
    for (int h = -1; h <= 1; h++)
    {
        for (int w = -1; w <= 1; w++)
        {
            if ((h + i) >= 0 && (h + i) < height && (w + j) >= 0 && (w + j) < width)
            {
                int weightx = g[h + 1][w + 1];
                rx += weightx * image[h + i][w + j].rgbtRed;
                gx += weightx * image[h + i][w + j].rgbtGreen;
                bx += weightx * image[h + i][w + j].rgbtBlue;

                int weighty = g[w + 1][h + 1];
                ry += weighty * image[h + i][w + j].rgbtRed;
                gy += weighty * image[h + i][w + j].rgbtGreen;
                by += weighty * image[h + i][w + j].rgbtBlue;
            }
        }
    }

    RGBTRIPLE newPixel;
    newPixel.rgbtRed = limit(round(sqrt((ry * ry) + (rx * rx))));
    newPixel.rgbtGreen = limit(round(sqrt((gy * gy) + (gx *gx))));
    newPixel.rgbtBlue = limit(round(sqrt((by * by) + (bx * bx))));
    free
    return newPixel;
}

// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE edged_image[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            edged_image[i][j] = edged_pixel(i, j, height, width, image);
        }
    }

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

Thanks in Advance

1

Your algorithm looks fine, just a couple typos.

RGBTRIPLE edged_pixel(int i, int j, int height, int width, RGBTRIPLE image[width][height])
should specify height before width, like the other instances.

int g[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, -1}}
should use have a positive 1 in the last triple.

You must log in to answer this question.

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