我的pset 4滤镜模糊功能代码未通过测试,如下所示。
还有其他失败的测试用例,但我将只发布前两个。
:( blur correctly filters middle pixel
expected "127 140 149\n", not "169 187 199\n"
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 0...
checking for output "127 140 149\n"...
Expected Output: 127 140 149
Actual Output: 169 187 199
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "101 120 131\n"
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 1...
checking for output "80 95 105\n"...
Expected Output: 80 95 105
Actual Output: 101 120 131
:) blur correctly filters pixel in corner
Log testing with sample 3x3 image
first row: (10, 20, 30), (40, 50, 60), (70, 80, 90)
second row: (110, 130, 140), (120, 140, 150), (130, 150, 160)
third row: (200, 210, 220), (220, 230, 240), (240, 250, 255)
running ./testing 3 2...
checking for output "70 85 95\n"...
我的代码做什么,我的算法如何工作?
我们获得了结构类型为RGBTRIPLE
的2D数组和一个名为image
的变量,其中包含三个属性rgbtRed, rgbtBlue, rgbtGreen
。
。
对于2D数组内图像中的每个像素(= RGBTRIPLE),我检查其相邻像素(= RGBTRIPLE)及其本身以计算平均颜色。
(对于2D数组内的每个像素,总共要检查9个像素。 如果不存在任何像素,则不添加它们。)
如果有人知道为什么我的算法无法正确计算图像颜色,请告诉我。
我已经做了一个星期,重新阅读了几次代码,但是仍然找不到我的错误。
这是我的代码。
void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int tmpRGB[3] = {0};
addSelfAndAdjacent(i, j, height, width, image, tmpRGB);
image[i][j].rgbtRed = tmpRGB[0];
image[i][j].rgbtGreen = tmpRGB[1];
image[i][j].rgbtBlue = tmpRGB[2];
}
}
return;
}
void addSelfAndAdjacent(int h, int w, int height, int width, RGBTRIPLE image[height][width], int *tmpRGB)
{
// self, top, right, bottom, left, upperRight, lowerRight, lowerLeft, upperLeft
int x[] = { 0, 0, 1, 0, -1, 1, 1, -1, -1};
int y[] = { 0, -1, 0, 1, 0, -1, 1, 1, -1};
int counter = 0;
for (int i = 0; i < 9; i++)
{
if (isValid((h + y[i]), (w + x[i]), height, width))
{
counter++;
*(tmpRGB + 0) += image[h + y[i]][w + x[i]].rgbtRed;
*(tmpRGB + 1) += image[h + y[i]][w + x[i]].rgbtGreen;
*(tmpRGB + 2) += image[h + y[i]][w + x[i]].rgbtBlue;
}
}
*(tmpRGB + 0) = round(*(tmpRGB + 0) / counter);
*(tmpRGB + 1) = round(*(tmpRGB + 1) / counter);
*(tmpRGB + 2) = round(*(tmpRGB + 2) / counter);
return;
}
bool isValid(int y, int x, int height, int width)
{
if (y < 0 || y > height - 1 || x < 0 || x > width - 1)
{
return false;
}
return true;
}