c - 为什么C模糊滤镜不模糊角落?
问题描述
我在 cs50 ide 的 c 中开发了一个函数来框模糊位图图像,乍一看它似乎模糊了图像。根据check50,它没有正确模糊角落,但中间像素和边缘像素是正确的。除此之外,当我运行我的代码时,我会遇到多个运行时错误。
该函数应该循环遍历图像中的每个像素,并将当前像素及其周围像素的 RGB 值放入 3 个数组中,red[]、green[] 和 blue[]。如果周围像素不存在,则不会将 RGB 值放入颜色数组中。
然后,它分别对红色、绿色和蓝色值进行平均。它将这些值保存在一个名为 temp[][] 的临时二维数组中。在计算出每个像素的平均值后,图像会被 temp[] 数组中的颜色值覆盖。
我认为检查丢失像素的逻辑有缺陷,但我一直无法找到问题所在。另外,不明白为什么我会遇到运行时错误。我应该如何正确模糊角落像素并消除运行时错误?
这是我的代码:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
//Define variables
RGBTRIPLE temp[height][width];
int red[9];
int green[9];
int blue[9];
int counter;
int redavg;
int blueavg;
int greenavg;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
//This block of code is for the middle pixel of the 3 by 3 box
counter = 0.00;
red[counter] = image[j][i].rgbtRed;
green[counter] = image[j][i].rgbtGreen;
blue[counter] = image[j][i].rgbtBlue;
counter++;
//Check to see if the pixel is on the top edge
if (i != 0)
{
red[counter] = image[j][i-1].rgbtRed;
green[counter] = image[j][i-1].rgbtGreen;
blue[counter] = image[j][i-1].rgbtBlue;
counter++;
//Checks to see if pixel is on the right edge
if (j != width - 1)
{
red[counter] = image[j+1][i-1].rgbtRed;
green[counter] = image[j+1][i-1].rgbtGreen;
blue[counter] = image[j+1][i-1].rgbtBlue;
counter++;
red[counter] = image[j+1][i].rgbtRed;
green[counter] = image[j+1][i].rgbtGreen;
blue[counter] = image[j+1][i].rgbtBlue;
counter++;
}
//Checks to see if pixel is on the right edge
if (j != 0)
{
red[counter] = image[j-1][i-1].rgbtRed;
green[counter] = image[j-1][i-1].rgbtGreen;
blue[counter] = image[j-1][i-1].rgbtBlue;
counter++;
red[counter] = image[j-1][i].rgbtRed;
green[counter] = image[j-1][i].rgbtGreen;
blue[counter] = image[j-1][i].rgbtBlue;
counter++;
}
}
//Check to see if the pixel is on the bottom edge
if (i != height - 1)
{
red[counter] = image[j][i+1].rgbtRed;
green[counter] = image[j][i+1].rgbtGreen;
blue[counter] = image[j][i+1].rgbtBlue;
counter++;
//Checks to see if pixel is on the right edge
if (j != width - 1)
{
red[counter] = image[j+1][i+1].rgbtRed;
green[counter] = image[j+1][i+1].rgbtGreen;
blue[counter] = image[j+1][i+1].rgbtBlue;
counter++;
if (i == 0)
{
red[counter] = image[j+1][i].rgbtRed;
green[counter] = image[j+1][i].rgbtGreen;
blue[counter] = image[j+1][i].rgbtBlue;
counter++;
}
}
//Checks to see if pixel is on the left edge
if (j != 0)
{
red[counter] = image[j-1][i+1].rgbtRed;
green[counter] = image[j-1][i+1].rgbtGreen;
blue[counter] = image[j-1][i+1].rgbtBlue;
counter++;
if (i == 0)
{
red[counter] = image[j-1][i].rgbtRed;
green[counter] = image[j-1][i].rgbtGreen;
blue[counter] = image[j-1][i].rgbtBlue;
counter++;
}
}
}
//take red blue and green, average them, and put them into tmp array
for (int k = 0; k < counter; k++)
{
redavg += red[k];
blueavg += blue[k];
greenavg += green[k];
}
redavg = round(redavg/(float)counter);
greenavg = round(greenavg/(float)counter);
blueavg = round(blueavg/counter);
temp[j][i].rgbtRed = redavg;
temp[j][i].rgbtGreen = greenavg;
temp[j][i].rgbtBlue = blueavg;
redavg = 0;
blueavg = 0;
greenavg = 0;
}
}
for (int m = 0; m < width; m++)
{
for (int n = 0; n < height; n++)
{
image[n][m].rgbtRed = temp[n][m].rgbtRed;
image[n][m].rgbtGreen = temp[n][m].rgbtGreen;
image[n][m].rgbtBlue = temp[n][m].rgbtBlue;
}
}
return;
}
我收到这些运行时错误:
helpers.c:299:32: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:300:34: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:301:33: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:307:36: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:308:38: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:309:37: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:323:36: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:324:38: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:325:37: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
check50 告诉我:
:) blur correctly filters middle pixel
:) blur correctly filters pixel on edge
:( blur correctly filters pixel in corner
expected "70 85 95\n", not "70 136 55\n"
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 128 89\n80 ..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 135 71\n80 ..."
解决方案
推荐阅读
- java - 应用程序实际使用的是这两个 java 版本中的哪一个?
- javascript - Javascript游戏,连接按钮作为输入
- python - 如何使用实际使用的库在 Python 中创建 requirements.txt?
- python - 尝试使用 ALAudioDevice 服务创建新模块和订阅时出错
- javascript - 从 index.js 导出变量并将其导入另一个类 - react.js
- python-3.x - 起始词匹配
- javascript - Javascript正则表达式匹配特定字符串
- mysql-workbench - 输入关键字后如何自动大写MySQL?
- c# - 有没有办法使用类似于 .AsImplementedInterfaces() 的 JSON 配置在 Autofac 中为组件注册所有接口
- mysql - mysql - 如何仅更新从多个重复行中找到的第一行