Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c - CS50(2019) problem set "Filter": "blur" somehow not working correctly

So, I have spent about 5 hours+ trying to figure out what is wrong with my code. I have tried debug50 with a 3x3 file I have manually created in Paint and everything seemed to work as intended; each pixel makes a 3x3 sweep around itself and disregards pixels that do not exist, like the ones in the corners or around edges. The final average values for each of the colors were also correct. Somehow, though, when I checked with check50, it gave out the following message:

With countless tweaking and head-scratching, I have decided that it was probably time for me to turn to the community for help. Here's my code:

{
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            int avgfordiv = 0;
            int neighvalgreen = 0;
            int neighvalblue = 0;
            int neighvalred = 0;

            for (int hh = -1; hh < 2; hh++)
            {
                for (int ww = -1; ww < 2; ww++)
                {
                    if ((h+hh) != height && (w+ww) != width && (h+hh) != -1 && (w+ww) != -1)
                    {
                        //sweep
                        avgfordiv++;//count up for division
                        neighvalgreen += image[h + hh][w + ww].rgbtGreen;
                        neighvalred += image[h + hh][w + ww].rgbtRed;
                        neighvalblue += image[h + hh][w + ww].rgbtBlue;
                    }
                }
            }
            //add values to pixels
             image[h][w].rgbtGreen = (int)(round((float)neighvalgreen / avgfordiv));
             image[h][w].rgbtBlue = (int)(round((float)neighvalblue / avgfordiv));
             image[h][w].rgbtRed = (int)(round((float)neighvalred / avgfordiv));

            //check cap
             if (image[h][w].rgbtGreen <= 255)
                {}
            else
                image[h][w].rgbtGreen %= 255;

            if (image[h][w].rgbtRed <= 255)
                {}
            else
                image[h][w].rgbtRed %= 255;

            if (image[h][w].rgbtBlue <= 255)
                {}
            else
                image[h][w].rgbtBlue %= 255;
        }
    }
    return;
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Making a copy of the image and using that copy to calculate the total amount of red green and blue seems to fix it.

RGBTRIPLE copy[height][width];
for (int h = 0; h < height; i++)
{
    for (int w = 0; w < width; j++)
    {
        copy[h][w] = image[h][w];
    }
}

And change it below:

neighvalgreen += copy[h + hh][w + ww].rgbtGreen;
neighvalred += copy[h + hh][w + ww].rgbtRed;
neighvalblue += copy[h + hh][w + ww].rgbtBlue;

Also you dont need to check if the values exceeded 255 because you are calculating the average value so it will never exceeded 255.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...