我已经在cs50 ide中用c开发了一个功能,可以对位图图像进行框式模糊处理,乍看之下它似乎会使图像模糊化。根据check50,它不能正确模糊角,但是中间像素和边缘像素是正确的。除此之外,我在运行代码时还会遇到多个运行时错误。
该功能应该循环遍历图像中的每个像素,并将当前像素及其周围像素的RGB值分成3个数组,分别是red [],green []和blue []。如果周围的像素不存在,则RGB值不会放入颜色阵列中。
然后,它分别对红色,绿色和蓝色值进行平均。它将这些值保存在称为temp [] []的临时2d数组中。计算完每个像素的平均值后,图像将被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 ..."