我遇到一些问题,无法满足CS50的问题集4中的边缘过滤器要求。可以在以下位置找到问题的描述:https://cs50.harvard.edu/x/2020/psets/4/filter/more/。我的代码对我来说很有意义,所以希望有人可以对此有所了解,因为我已经连续几天盯着我的代码):显然,我的代码确实可以编译,所以我不认为有本身有任何错误,但可能是我思维中的逻辑缺陷。
我的代码如下:
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE tempimage[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
tempimage[i][j] = image[i][j];
int sGxRed = 0;
int sGxGreen = 0;
int sGxBlue = 0;
int sGyRed = 0;
int sGyGreen = 0;
int sGyBlue = 0;
if (j + 1 < width)
{
sGxRed += 2 * image[i][j + 1].rgbtRed;
sGxGreen += 2 * image[i][j + 1].rgbtGreen;
sGxBlue += 2 * image[i][j + 1].rgbtBlue;
}
if (j > 0)
{
sGxRed += -2 * image[i][j - 1].rgbtRed;
sGxGreen += -2 * image[i][j - 1].rgbtGreen;
sGxBlue += -2 * image[i][j - 1].rgbtBlue;
}
if (i + 1 < height)
{
sGyRed += 2 * image[i + 1][j].rgbtRed;
sGyGreen += 2 * image[i + 1][j].rgbtGreen;
sGyBlue += 2 * image[i + 1][j].rgbtBlue;
}
if (i > 0)
{
sGyRed += -2 * image[i - 1][j].rgbtRed;
sGyGreen += -2 * image[i - 1][j].rgbtGreen;
sGyBlue += -2 * image[i - 1][j].rgbtBlue;
}
if (i + 1 < height && j + 1 < width)
{
sGxRed += image[i + 1][j + 1].rgbtRed;
sGxGreen += image[i + 1][j + 1].rgbtGreen;
sGxBlue += image[i + 1][j + 1].rgbtBlue;
sGyRed += image[i + 1][j + 1].rgbtRed;
sGyGreen += image[i + 1][j + 1].rgbtGreen;
sGyBlue += image[i + 1][j + 1].rgbtBlue;
}
if (i > 0 && j + 1 < width)
{
sGxRed += image[i - 1][j + 1].rgbtRed;
sGxGreen += image[i - 1][j + 1].rgbtGreen;
sGxBlue += image[i - 1][j + 1].rgbtBlue;
sGyRed += -1 * image[i - 1][j + 1].rgbtRed;
sGyGreen += -1 * image[i - 1][j + 1].rgbtGreen;
sGyBlue += -1 * image[i - 1][j + 1].rgbtBlue;
}
if (i > 0 && j > 0)
{
sGxRed += -1 * image[i - 1][j - 1].rgbtRed;
sGxGreen += -1 * image[i - 1][j - 1].rgbtGreen;
sGxBlue += -1 * image[i - 1][j - 1].rgbtBlue;
sGyRed += -1 * image[i - 1][j - 1].rgbtRed;
sGyGreen += -1 * image[i - 1][j - 1].rgbtGreen;
sGyBlue += -1 * image[i - 1][j - 1].rgbtBlue;
}
if (i + 1 < height && j > 0)
{
sGxRed += -1 * image[i + 1][j - 1].rgbtRed;
sGxGreen += -1 * image[i + 1][j - 1].rgbtGreen;
sGxBlue += -1 * image[i + 1][j - 1].rgbtBlue;
sGyRed += image[i + 1][j - 1].rgbtRed;
sGyGreen += image[i + 1][j - 1].rgbtGreen;
sGyBlue += image[i + 1][j - 1].rgbtBlue;
}
int sRed = 0;
int sGreen = 0;
int sBlue = 0;
sRed = round((float)(sqrt(sGxRed^2 + sGyRed^2)));
if (sRed > 255)
{
sRed = 255;
}
sGreen = round((float)(sqrt(sGxGreen^2 + sGyGreen^2)));
if (sGreen > 255)
{
sGreen = 255;
}
sBlue = round((float)(sqrt(sGxBlue^2 + sGyBlue^2)));
if (sBlue > 255)
{
sBlue = 255;
}
tempimage[i][j].rgbtRed = sRed;
tempimage[i][j].rgbtGreen = sGreen;
tempimage[i][j].rgbtBlue = sBlue;
}
}
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
image[i][j].rgbtRed = tempimage[i][j].rgbtRed;
image[i][j].rgbtGreen = tempimage[i][j].rgbtGreen;
image[i][j].rgbtBlue = tempimage[i][j].rgbtBlue;
}
}
return;
}
我对编码非常陌生。在学习本课程之前,我只尝试过一点VBA。这是我第一次用C语言编写代码,CS50的学习曲线非常陡峭,如果我的问题不重要,请原谅!
提前谢谢大家! :)
P.S除了我的实际问题,请随时评论我的代码如何更有效/更短。我总是持不同观点!
答案 0 :(得分:1)
常见的误解:^
并不意味着功能强大,它意味着按位XOR,这不是您想要的。将其替换为math.h中的pow()
。
或者特别是在2的幂的情况下,只需执行variable * variable
。