CS50X模糊功能

发布于 2025-02-13 23:54:29 字数 2269 浏览 0 评论 0原文

我目前正在为PSET4中的For Filt-More处理模糊功能,并且我正在努力在代码中找到错误。模糊函数似乎正确地过滤了中间像素,我假设其他不在边缘上的像素,但是错误地滤波了拐角和边缘上的像素。任何朝着正确方向的推销都将不胜感激。


// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{

    // Copy to store filtered image into
    RGBTRIPLE temp[height][width];

    int red_total = 0, blue_total = 0, green_total = 0;

    int red_average = 0, blue_average = 0, green_average = 0;

    // Goes through each pixel [i][j]
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            // Loops through surrounding 3x3 grid and records the average of all of them
            for (int h = -1; h < 2; h++)
            {
                // Checks if the current row is within the image array
                if (i + h >= 0 && i + h < height)
                {
                    for (int k = -1; k < 2; k++)
                    {
                        // Checks if current column is within image array
                        if (j + k >= 0 && j + k < width)
                        {
                            red_total = red_total + image[i + h][j + k].rgbtRed;
                            blue_total = blue_total + image[i + h][j + k].rgbtBlue;
                            green_total = green_total + image[i + h][j + k].rgbtGreen;
                        }
                        else continue;
                    }
                }
                else continue;
            }

            // Averages and reassigns pixel value
            red_average = round(red_total / 9.0);
            blue_average = round(blue_total / 9.0);
            green_average = round(green_total / 9.0);

            temp[i][j].rgbtBlue = blue_average;
            temp[i][j].rgbtRed = red_average;
            temp[i][j].rgbtGreen = green_average;

            // Resets counter variables
            red_total = green_total = blue_total = 0;
            red_average = blue_average = green_average = 0;
        }
    }

    // Makes image (original) array equal to the filtered image
    for (int x = 0; x < height; x++)
    {
        for(int y = 0; y < width; y++)
        {
            image[x][y] = temp[x][y];
        }
    }

    return;
}

I am currently working on the blur function in PSET4 for filter-more, and I am struggling to find the bug in my code. The blur function seems to correctly filter the middle pixel and I would assume other pixels that are not on the edges, but is incorrectly filtering those on the corners and edges. Any nudges in the right direction would be greatly appreciated.


// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{

    // Copy to store filtered image into
    RGBTRIPLE temp[height][width];

    int red_total = 0, blue_total = 0, green_total = 0;

    int red_average = 0, blue_average = 0, green_average = 0;

    // Goes through each pixel [i][j]
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {

            // Loops through surrounding 3x3 grid and records the average of all of them
            for (int h = -1; h < 2; h++)
            {
                // Checks if the current row is within the image array
                if (i + h >= 0 && i + h < height)
                {
                    for (int k = -1; k < 2; k++)
                    {
                        // Checks if current column is within image array
                        if (j + k >= 0 && j + k < width)
                        {
                            red_total = red_total + image[i + h][j + k].rgbtRed;
                            blue_total = blue_total + image[i + h][j + k].rgbtBlue;
                            green_total = green_total + image[i + h][j + k].rgbtGreen;
                        }
                        else continue;
                    }
                }
                else continue;
            }

            // Averages and reassigns pixel value
            red_average = round(red_total / 9.0);
            blue_average = round(blue_total / 9.0);
            green_average = round(green_total / 9.0);

            temp[i][j].rgbtBlue = blue_average;
            temp[i][j].rgbtRed = red_average;
            temp[i][j].rgbtGreen = green_average;

            // Resets counter variables
            red_total = green_total = blue_total = 0;
            red_average = blue_average = green_average = 0;
        }
    }

    // Makes image (original) array equal to the filtered image
    for (int x = 0; x < height; x++)
    {
        for(int y = 0; y < width; y++)
        {
            image[x][y] = temp[x][y];
        }
    }

    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文