cs50 pset4的模糊功能

时间:2020-05-09 10:25:40

标签: c cs50

我在做什么错?我也尝试了差异技巧。用3 for循环似乎对我来说效果不佳...任何人都可以帮助确保错误或正确的内容。代码似乎编译良好,但输出不正确

代码:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
   double sr;
    double sg;
    double sb;
    int total=1;

    double sur1=0;
    double sug1=0;
    double sub1=0;

    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            sr=image[i][j].rgbtRed;
            sg=image[i][j].rgbtGreen;
            sb=image[i][j].rgbtBlue;

            int k = -1;
            int l = -1;

                while(k < 2)
                {
                    int y = j - k;

                    if(y >= 0)
                    {
                        double sr1 = image[i][y].rgbtRed;
                        sur1 = sur1 + sr1;

                        double sg1 = image[i][y].rgbtGreen;
                        sug1 = sug1 + sg1;

                        double sb1 = image[i][y].rgbtBlue;
                        sub1 = sub1 + sb1;

                        total++;
                    }

                        k++;
                }

                while(l < 2)
                {
                    int x = i - l;

                    if(x >= 0)
                    {
                        double sr2 = image[x][j].rgbtRed;
                        sur1 = sur1 + sr2 ;

                        double sg2 = image[x][j].rgbtGreen;
                        sug1 = sug1 + sg2;

                        double sb2 = image[x][j].rgbtBlue;
                        sub1 = sub1 + sb2;

                        total++;
                    }
                    l++;
                }

                    sur1=sur1+sr;
                    sug1=sug1+sg;
                    sub1=sub1+sb;


                    int avgr=round(sur1/total);
                    int avgg=round(sug1/total);
                    int avgb=round(sub1/total);


                    image[i][j].rgbtRed=avgr;
                    image[i][j].rgbtGreen=avgg;
                    image[i][j].rgbtBlue=avgb;

                    total=1;
            }

        }

        return;
}

如果有人想要有关此的信息: https://cs50.harvard.edu/x/2020/psets/4/filter/less/

进行了一些更改 仍然没有想要的答案 代码:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int total=0;

    for (int i=0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {

            double sur1=0;
            double sug1=0;
            double sub1=0;
            int k = -1;
            int l = -1;

                while(l < 2)
                {
                    int x = i - l;

                    if(x >= 0)
                    {

                        while(k < 2)
                        {
                            int y = j - k;
                            if(y >= 0)
                            {
                                double sr1 = image[x][y].rgbtRed;
                                sur1 = sur1 + sr1;

                                double sg1 = image[x][y].rgbtGreen;
                                sug1 = sug1 + sg1;

                                double sb1 = image[x][y].rgbtBlue;
                                sub1 = sub1 + sb1;

                                total++;
                                k++;

                            }

                            else
                            {
                                k++;
                            }
                         }

                         l++;
                    }

                        else
                        {
                            l++;
                        }
                    }

                    image[i][j].rgbtRed=round(sur1/total);
                    image[i][j].rgbtGreen=round(sug1/total);
                    image[i][j].rgbtBlue=round(sub1/total);
                    total=0;

               }
        }
    return;
}

0 个答案:

没有答案