知道为什么此功能会导致分段故障核心倾倒吗?新的CS50周4。

发布于 2025-02-07 04:20:55 字数 3400 浏览 1 评论 0原文

我了解这可能不是我新手的最有效的代码。我真的不知道为什么这会导致细分错误,如果有人可以向我解释我出错的地方,这将非常感谢。该函数应该通过将每个像素的颜色值更改为自身和8个周围像素的平均值来模糊.bmp图像。我尚未在将模糊的值传递给输出图像中的文章。谢谢。

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    float temp_red = 0, temp_green = 0, temp_blue = 0;
    RGBTRIPLE copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width ; j++)
        {
            int counter = 0;
            if (i >= 1 && j >= 1)
            {
                temp_red = temp_red + image[i-1][j-1].rgbtRed;
                temp_green = temp_green + image[i-1][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j-1].rgbtBlue;
                counter++;
            }

            if (i >= 1 && j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i - 1][j + 1].rgbtRed;
                temp_green = temp_green + image[i - 1][j + 1].rgbtGreen;
                temp_blue = temp_blue + image[i - 1][j + 1].rgbtBlue;
                counter++;
            }

            if (i >= 1)
            {
                temp_red = temp_red + image[i-1][j].rgbtRed;
                temp_green = temp_green + image[i-1][j].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j].rgbtBlue;
                counter++;
            }

            if (j >= 1)
            {
                temp_red = temp_red + image[i][j-1].rgbtRed;
                temp_green = temp_green + image[i][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i][j-1].rgbtBlue;
                counter++;
            }

            if (j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i][j+1].rgbtRed;
                temp_green = temp_green + image[i][j+1].rgbtGreen;
                temp_blue = temp_blue + image[i][j+1].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1 && j >= 1)
            {
                temp_red = temp_red + image[i+1][j-1].rgbtRed;
                temp_green = temp_green + image[i+1][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i+1][j-1].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1)
            {
                temp_red = temp_red + image[i-1][j].rgbtRed;
                temp_green = temp_green + image[i-1][j].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1 && j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i+1][j+1].rgbtRed;
                temp_green = temp_green + image[i+1][j+1].rgbtGreen;
                temp_blue = temp_blue + image[i+1][j+1].rgbtBlue;
                counter++;
            }

            temp_red = temp_red + image[i][j].rgbtRed;
            temp_green = temp_green + image[i][j].rgbtGreen;
            temp_blue = temp_blue + image[i][j].rgbtBlue;
            counter++;

            temp_red = round(temp_red / counter);
            temp_green = round(temp_green / counter);
            temp_blue = round(temp_blue / counter);
            counter = 0;
            copy[i][j].rgbtRed = temp_red;
            copy[i][j].rgbtGreen = temp_green;
            copy[i][j].rgbtBlue = temp_blue;
        }
    }

    return;
}

I understand this may not be the most efficient code as I am new to this. I really have no idea why this is causing a segmentation fault and would greatly appreciate if somebody could explain to me where I have gone wrong. the function is supposed to blur a .bmp image by changing the colour value of each pixel to an average of itself and the 8 surrounding pixels. I have not yet wrote in where I will pass the blurred values to the output image. Thanks.

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    float temp_red = 0, temp_green = 0, temp_blue = 0;
    RGBTRIPLE copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width ; j++)
        {
            int counter = 0;
            if (i >= 1 && j >= 1)
            {
                temp_red = temp_red + image[i-1][j-1].rgbtRed;
                temp_green = temp_green + image[i-1][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j-1].rgbtBlue;
                counter++;
            }

            if (i >= 1 && j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i - 1][j + 1].rgbtRed;
                temp_green = temp_green + image[i - 1][j + 1].rgbtGreen;
                temp_blue = temp_blue + image[i - 1][j + 1].rgbtBlue;
                counter++;
            }

            if (i >= 1)
            {
                temp_red = temp_red + image[i-1][j].rgbtRed;
                temp_green = temp_green + image[i-1][j].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j].rgbtBlue;
                counter++;
            }

            if (j >= 1)
            {
                temp_red = temp_red + image[i][j-1].rgbtRed;
                temp_green = temp_green + image[i][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i][j-1].rgbtBlue;
                counter++;
            }

            if (j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i][j+1].rgbtRed;
                temp_green = temp_green + image[i][j+1].rgbtGreen;
                temp_blue = temp_blue + image[i][j+1].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1 && j >= 1)
            {
                temp_red = temp_red + image[i+1][j-1].rgbtRed;
                temp_green = temp_green + image[i+1][j-1].rgbtGreen;
                temp_blue = temp_blue + image[i+1][j-1].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1)
            {
                temp_red = temp_red + image[i-1][j].rgbtRed;
                temp_green = temp_green + image[i-1][j].rgbtGreen;
                temp_blue = temp_blue + image[i-1][j].rgbtBlue;
                counter++;
            }

            if (i + 1 <= height - 1 && j + 1 <= width - 1)
            {
                temp_red = temp_red + image[i+1][j+1].rgbtRed;
                temp_green = temp_green + image[i+1][j+1].rgbtGreen;
                temp_blue = temp_blue + image[i+1][j+1].rgbtBlue;
                counter++;
            }

            temp_red = temp_red + image[i][j].rgbtRed;
            temp_green = temp_green + image[i][j].rgbtGreen;
            temp_blue = temp_blue + image[i][j].rgbtBlue;
            counter++;

            temp_red = round(temp_red / counter);
            temp_green = round(temp_green / counter);
            temp_blue = round(temp_blue / counter);
            counter = 0;
            copy[i][j].rgbtRed = temp_red;
            copy[i][j].rgbtGreen = temp_green;
            copy[i][j].rgbtBlue = temp_blue;
        }
    }

    return;
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-02-14 04:20:55
 if (i-1 <=0 && j+1 <= width - 1)

我怀疑i -1的条件是错误的,gurahtees the concess index -1

认为您的意思是

if (i-1 >=0 && j+1 <= width - 1)

其他索引也是错误的

        if (i + 1 <= height - 1 && j + 1 <= width - 1)
        {
            temp_red = temp_red + image[i+1][j+1].rgbtRed;

        if (i + 1 < height - 1 && j + 1 < width - 1)
        {
            temp_red = temp_red + image[i+1][j+1].rgbtRed;
 if (i-1 <=0 && j+1 <= width - 1)

i suspect that condition on i-1 is wrong, it gurahtees thatn you access index -1

i assume you mean

if (i-1 >=0 && j+1 <= width - 1)

other indexes are wrong too

        if (i + 1 <= height - 1 && j + 1 <= width - 1)
        {
            temp_red = temp_red + image[i+1][j+1].rgbtRed;

this will access image with index = height, you mean

        if (i + 1 < height - 1 && j + 1 < width - 1)
        {
            temp_red = temp_red + image[i+1][j+1].rgbtRed;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文