CS50模糊(浮点异常储备)

发布于 2025-02-13 19:12:59 字数 1419 浏览 0 评论 0原文

我在CS50中执行了无滤波器问题,我做了几乎所有其他过滤器,但是我在模糊中有点挣扎。

我真的没有得到malloc的工作原理(我只是ctrl+c ctrl+v他们如何为“映像”进行“复制”的空间以及如何完成周围像素的平均值,我尝试了一些具有平均功能的东西我认为它会起作用,但它告诉我“浮点异常(core倾倒)”,我认为问题在于for循环,但idk如何诚实地修复它。

通常,我喜欢自己找到解决我的问题的解决方案,但是在这里我真的不理解我在这个问题上,近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;

}

Im doing the filter-less problem in CS50, i did pretty much all the other filter correctly but im a little struggling with the blur.

I don't really get how malloc works (i just ctrl+c ctrl+v how they allocated space for 'image' to do 'copy') and how to do the average of the surrounding pixels, i tried something with the average function i thought it would work but it tells me 'floating point exception (core dumped)' and i think the problem is in the for loop but idk how to fix it honestly.

Usually i like to found solutions to my problems by myself but here i really don't understand im on this one for almost 2 weeks and i don't wanna see videos.

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;

}

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

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

发布评论

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

评论(1

醉生梦死 2025-02-20 19:12:59

感谢大家的有用技巧和评论,

你们指的是很多问题,我尝试

首先修复所有问题,我和J开始了AT&lt; 0因此,不可能通过for循环,将x和y保持在0,并通过除以0,从而导致浮点例外。
因此,我刚刚定义了I和J更好地定义了I和J的条件:

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

(int j =(pxlwidth -1&lt; 0) :pxlwidth -1; j&lt; = pxlwidth+

1我很确定这不是我得到的结果。

问题在于我将值添加到不能超过255的字节类型中。因此,我刚刚创建了3个浮点值,将它们定义为0.0,然后将所有值添加到它们中,然后将其划分为行 *列 * rgbtriple字段舍入结果并将其转换为int

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; < / code>

caca.rgbtred =(int)round(proutr /(x * y)); caca.rgbtgreen =(int)round(proutg /(x * y)); caca.rgbtblue =(int)round(proutb /(x * y)); < / code>

再次感谢您的每个人,您的每个评论都帮助我找出了问题并解决了问题,

而 无需直接给我解决方案它给出了这个,并且效果很好:

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;

}

Thanks to everyone for your useful tips and remarks

There was a lot of problems you guys pointed to and i tried fixing everything

First, i and j started at < 0 so it was impossible to go through the for loop, keeping x and y at 0 and causing a floating point exception by dividing by 0.
So i just defined i and j better and made a condition to them like this :

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

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

Next, when i tried running my program it was working but the output image was almost completely black and i was pretty sure it wasn't the result i shoul've got.

The problem was that i was adding up values to BYTE types that cannot exceed 255. So i just created 3 float values, defined them to 0.0, and added all the values to them to then divide them by rows * columns and giving the results to the RGBTRIPLE fields after rounding the result and turning it to int

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));

Thanks again to everyone each of your comments helped me to figure out the problem and solve it without giving me the solution directly

So at the end it gives this and it works nicely :

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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文