Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c - CS50 pset4 filter “edges”

When I run a image through my edges code for example :

Input image,

Output image

I've tried to debug my code, but I still cant figure it out:

here is log :

Expected Output:

76 117 255

213 228 255

192 190 255

114 102 255

210 150 60

103 108 255

114 117 255

200 197 255

210 190 255

Actual Output:

237 190 202

218 178 158

255 255 190

255 244 198

255 255 196

255 255 255

211 218 193

255 255 255

255 255 180

code :

typedef struct
{
    int red;
    int green;
    int blue;
}
kernel;    
void edges(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE pixel[height + 2][width + 2];
    int G_x[3][3] = {
        {-1, 0 , 1},
        {-2, 0 , 2},
        {-1, 0 , 1}
    };
    int G_y[3][3] = {
        {-1, -2, -1},
        {0, 0, 0},
        {1, 2, 1}
    };
    kernel x, y, net;  

    for (int p = 0; p < height + 2; p++)
    {
        for (int q = 0; q < width + 2; q++)
        {
            if (p == 0 || p == height + 1 || q == 0 || q == width + 1)
            {
                pixel[p][q] = (RGBTRIPLE){.rgbtRed = 0, .rgbtGreen = 0, .rgbtBlue = 0};
            }
            else
            {
                pixel[p][q] = image[p][q];
            }
        }
    }
    for (int i = 1; i <= height; i++)
    {
        for (int j = 1; j <= width; j++)
        {
            x = (kernel){.red = 0, .blue = 0, .green = 0};
            y = (kernel){.red = 0, .blue = 0, .green = 0};
            net = (kernel){.red = 0, .blue = 0, .green = 0};
            for (int m = -1; m < 2; m++)
            {
                for (int n = -1; n < 2; n++)
                {
                    x.red += pixel[i + m][j + n].rgbtRed * G_x[1 + m][1 + n];
                    x.blue += pixel[i + m][j + n].rgbtBlue * G_x[1 + m][1 + n];
                    x.green += pixel[i + m][j + n].rgbtGreen * G_x[1 + m][1 + n];
        
                    y.red += pixel[i + m][j + n].rgbtRed * G_y[1 + m][1 + n];
                    y.blue += pixel[i + m][j + n].rgbtBlue * G_y[1 + m][1 + n];
                    y.green += pixel[i + m][j + n].rgbtGreen * G_y[1 + m][1 + n];
                }
            }
            net = (kernel){
                .red = round(sqrt(abs((x.red * x.red) + (y.red * y.red)))),
                .green = round(sqrt(abs((x.green * x.green) + (y.green * y.green)))),
                .blue =round(sqrt(abs((x.blue * x.blue) + (y.blue * y.blue)))),
            };
            if (net.red > 255)
            {
                net.red = 255;
            }
            if (net.blue > 255)
            {
                net.blue = 255;
            }
            if (net.green > 255)
            {
                net.green = 255;
            }
            image[i - 1][j - 1].rgbtRed = net.red;
            image[i - 1][j - 1].rgbtGreen = net.green;
            image[i - 1][j - 1].rgbtBlue = net.blue;
        }
    }
    return;
}

i'm trying from last 5 hours but i can't figure out, what i'm doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...