【问题标题】:CS50 Blur (Floating point exception core dumped)CS50 Blur(浮点异常核心转储)
【发布时间】:2022-07-11 23:33:57
【问题描述】:

我在 CS50 中处理无滤镜问题,我几乎正确地完成了所有其他滤镜,但我在模糊方面有点挣扎。

我真的不明白 malloc 是如何工作的(我只是 ctrl+c ctrl+v 他们如何为“图像”分配空间以进行“复制”)以及如何计算周围像素的平均值,我尝试了一些我认为它可以工作的平均功能,但它告诉我“浮点异常(核心转储)”,我认为问题出在 for 循环中,但我不知道如何诚实地解决它。

通常我喜欢自己找到解决问题的方法,但在这里我真的不明白我在这个问题上已经快 2 周了,我不想看视频。

void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE (*copy)[width] = calloc(height, width * sizeof(RGBTRIPLE));
for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        copy[i][j] = image[i][j];
    }
}

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        image[i][j] = average(i, j, height, width, copy);
    }
}
free(copy);
return;
}

RGBTRIPLE average (int pxlheight, int pxlwidth, int height, int width, RGBTRIPLE 
copy[height][width])
{
RGBTRIPLE caca;
int x = 0;
int y = 0;

for (int i = pxlheight - 1; i <= pxlheight + 1 && i >= 0 && i <= height - 1; i++)
{
    y++;
    for (int j = pxlwidth - 1; j <= pxlwidth + 1 && j >= 0 && j <= width - 1; j++)
    {
        x++;
        caca.rgbtRed += copy[i][j].rgbtRed;
        caca.rgbtGreen += copy[i][j].rgbtGreen;
        caca.rgbtBlue += copy[i][j].rgbtBlue;
    }
}

caca.rgbtRed = round(caca.rgbtRed / (x * y));
caca.rgbtGreen = round(caca.rgbtGreen / (x * y));
caca.rgbtBlue = round(caca.rgbtBlue / (x * y));

return caca;

}

【问题讨论】:

  • 如果你使用c标签,也请说明问题。否则只需使用cs50 标签
  • i &lt;= pxlheight + 1j &lt;= pxlwidth + 1 可能有问题。检查以确保它们不允许ij 变得太大。
  • 没有任何浮点变量时的“浮点异常”通常表示被零除的问题。尝试在除以(x * y) 之前将if (x*y == 0) printf("Oops\n"); 放入average 函数中。
  • caca.rgbtRed += ... 您应该假设RGBTRIPLE 的每个字段只能保存一个像素的值。将多个像素的值相加可能会导致溢出。使用其他类型而不是 RGBTRIPLE

标签: c cs50 blur floating-point-exceptions


【解决方案1】:

感谢大家的有用提示和评论

你们指出了很多问题,我尝试解决所有问题

首先,i 和 j 从

for (int i = (pxlheight - 1 &lt; 0) ? 0 : pxlheight - 1; i &lt;= pxlheight + 1 &amp;&amp; i &lt;= height - 1; i++)

for (int j = (pxlwidth - 1 &lt; 0) ? 0 : pxlwidth - 1; j &lt;= pxlwidth + 1 &amp;&amp; j &lt;= width - 1; j++)

接下来,当我尝试运行我的程序时,它正在运行,但输出图像几乎是全黑的,我很确定这不是我应该得到的结果。

问题是我将值添加到不能超过 255 的 BYTE 类型。所以我只创建了 3 个浮点值,将它们定义为 0.0,然后将所有值添加到它们,然后将它们除以行 * 列并给出将结果四舍五入并将其转换为 int 后的结果到 RGBTRIPLE 字段

float proutR = 0.0; float proutG = 0.0; float proutB = 0.0;

proutR += copy[i][j].rgbtRed; proutG += copy[i][j].rgbtGreen; proutB += copy[i][j].rgbtBlue;

caca.rgbtRed = (int) round(proutR / (x * y)); caca.rgbtGreen = (int) round(proutG / (x * y)); caca.rgbtBlue = (int) round(proutB / (x * y));

再次感谢大家,你们的每一个cmet帮助我找出问题并解决它,而不是直接给我解决方案

所以最后它给出了这个并且效果很好:

RGBTRIPLE average (int pxlheight, int pxlwidth, int height, int width, 
RGBTRIPLE copy[height][width])
{
RGBTRIPLE caca;
int x = 0;
int y;
float proutR = 0.0;
float proutG = 0.0;
float proutB = 0.0;

for (int i = (pxlheight - 1 < 0) ? 0 : pxlheight - 1; i <= pxlheight + 1 && i <= height - 1; i++)
{
    x++;
    y = 0;
    for (int j = (pxlwidth - 1 < 0) ? 0 : pxlwidth - 1; j <= pxlwidth + 1 && j <= width - 1; j++)
    {
        y++;
        proutR += copy[i][j].rgbtRed;
        proutG += copy[i][j].rgbtGreen;
        proutB += copy[i][j].rgbtBlue;
    }
}

caca.rgbtRed = (int) round(proutR / (x * y));
caca.rgbtGreen = (int) round(proutG / (x * y));
caca.rgbtBlue = (int) round(proutB / (x * y));

return caca;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-17
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多