c - cs50 pset 4 filter blur [only one test passes]
问题描述
My code for blur function in pset 4 filter is not passing the tests, as below.
There are other failed test cases, but I will be only posting the first two.
:( 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"...
What does my code do, and how does my algorithm work?
We are given a 2D array of struct type RGBTRIPLE
and a variable called image
, containing three properties, rgbtRed, rgbtBlue, rgbtGreen
.
.
For every pixel (=RGBTRIPLE) within the image, inside the 2D array, I check its adjacent pixels (=RGBTRIPLE) and itself to calculate the average colors.
(which is in a total of 9 pixels to be checked for every pixels inside the 2D array. If any of the pixels do NOT exist, I do NOT add them.)
If anyone has any idea why my algorithm is not working to calculate the image colors properly, please let me know.
I have been working on this for a week, re-reading my code several times, but still fail to figure out my bug.
Here is my code.
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;
}
解决方案
推荐阅读
- java - 如何正确使用Activity中的界面?
- node.js - 捕获发送到 Messenger 聊天机器人的图像有哪些不同的方法
- asp.net - 位置路径拒绝不会在 system.web 中阻止静态文件夹
- php - 在 laravel 5.8 中仅显示当前经过身份验证的用户订单
- jquery - 为什么我的 PUT 请求会出现 404 状态错误?
- dart - 如何在 Flutter 中实现一个简单的倒数计时器构造函数到应用程序中?
- c# - 如何避免使用 Nest .NET 6.x 将重复项发布到 elasticsearch?
- batch-file - 批处理路径变量在 Windows Server 2016 中不起作用
- php - 使用 CodeIgniter 从 mysql 数据库生成 XML 表
- google-cloud-sql - 如何在云作曲家的气流中使用云 sql 执行 PostgreSQL SELECT 查询?