【发布时间】:2020-07-20 11:30:10
【问题描述】:
这是我在 CS50x 课程的滤镜课程中的模糊代码。当我这样做时,图像像素保持完全相同,甚至不会改变。谁能告诉我哪里出错了?
语言是 C。我的步骤的原因显示在每个步骤之前的标题中。基本上,我遍历所有像素并将相邻像素添加到其中,然后除以添加的总数以及原始像素。
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
RGBTRIPLE total[height][width];
// Save all the initial colour values into a new variable
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
// Loop through the pixels and calculate average value for each
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
// Clear the pixel's colour values
total[i][j].rgbtRed = 0;
total[i][j].rgbtBlue = 0;
total[i][j].rgbtGreen = 0;
int counter = 0;
// Loops through the blocks 1 column and 1 row away from the pixel
for(int k = i - 1; k < i + 2; k++)
{
// If k is less than 0 or more than height, skip that step
if(k < 0 || k > (height - 1))
{
return;
}
for(int l = j - 1; l < j + 2; l++)
{
// If l is less than 0 or more than wddth, skip that step
if(l < 0 || l > (width - 1))
{
return;
}
total[i][j].rgbtRed = temp[k][l].rgbtRed + total[i][j].rgbtRed;
total[i][j].rgbtBlue = temp[k][l].rgbtBlue + total[i][j].rgbtBlue;
total[i][j].rgbtGreen = temp[k][l].rgbtGreen + total[i][j].rgbtGreen;
counter++;
}
}
// Divide the total by the number of counter
image[i][j].rgbtRed = round((float) total[i][j].rgbtRed / counter);
image[i][j].rgbtGreen = round((float) total[i][j].rgbtGreen / counter);
image[i][j].rgbtBlue = round((float) total[i][j].rgbtBlue / counter);
}
}
return;
}
【问题讨论】:
-
循环中的
return语句不会有帮助。尝试用continue替换它们。 (return语句将立即退出函数;continue将跳过循环的当前迭代并从下一个迭代继续。)