CS50 滤镜 'Edge' returns 白色图像
CS50 Filter 'Edge' returns white image
我一直在研究哈佛的 CS50 'Filter' 问题,并且在 edges
部分遇到了一点问题。
我一直在尝试用这个算法来解决它:
- 逐个像素地浏览整个图像。图像是
RGBTRIPLE
类型的矩阵,称为 image[height][width]
- 检查当前像素是角点、边缘还是中间某处('else' 情况)
- 将该像素和“相邻”像素与相应的
g(x)
和 g(y)
值相乘,计算 g(x)^2 + g(y)^2
的平方根,相应地对其进行舍入,并导出该值在另一个矩阵中 image2[height][width]
.
我在下面提供的代码是我解决问题的尝试。我使用了与 blur
算法中相同的验证(检查角、边或中间),它起作用了。
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE gx, gy;
RGBTRIPLE image2[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
gx.rgbtRed = 0;
gx.rgbtGreen = 0;
gx.rgbtBlue = 0;
gy.rgbtRed = 0;
gy.rgbtGreen = 0;
gy.rgbtBlue = 0;
if (i == 0 && j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
}
else if (i == 0 && j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
}
else if (i == height-1 && j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i][j].rgbtRed;
gy.rgbtGreen += -1 * image[i][j].rgbtGreen;
gy.rgbtBlue += -1 * image[i][j].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (i == height-1 && j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i-1][j-1].rgbtBlue;
}
else if (i == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += image[i][j-1].rgbtRed;
gy.rgbtGreen += image[i][j-1].rgbtGreen;
gy.rgbtBlue += image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (i == height-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
}
else if (j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
}
else
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
}
int redVal = (int)round(sqrt(gx.rgbtRed * gx.rgbtRed + gy.rgbtRed * gy.rgbtRed));
int greenVal = (int)round(sqrt(gx.rgbtGreen * gx.rgbtGreen + gy.rgbtGreen * gy.rgbtGreen));
int blueVal = (int)round(sqrt(gx.rgbtBlue * gx.rgbtBlue + gy.rgbtBlue * gy.rgbtBlue));
if (redVal > 255) redVal = 255;
if (greenVal > 255) greenVal = 255;
if (blueVal > 255) blueVal = 255;
image2[i][j].rgbtRed = redVal;
image2[i][j].rgbtGreen = greenVal;
image2[i][j].rgbtBlue = blueVal;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = image2[i][j].rgbtRed;
image[i][j].rgbtGreen = image2[i][j].rgbtGreen;
image[i][j].rgbtBlue = image2[i][j].rgbtBlue;
}
}
return;
}
我的代码有很多行,因为它多次使用相同的操作(这是我设法解决问题的最简单方法)。
此外,if
语句中乘法的常量只是矩阵 gx
和 gy
的元素。我无缘无故地用 gx
和 gy
表示这些乘法的结果。我不想为这两个声明两个单独的矩阵,所以我对它们进行了硬编码。
我什至尝试手动验证自己,并使用 printf()
检查角落是否正确识别,一切都相应地工作。
问题是,当我使用 check50
时,这是我的输出:
此外,提供的名为 yard.bmp
的图像被转换为如下所示的“yardEdges.bmp”:
从我的角度来看,这是很多白色,我无法从这张照片中识别出任何边缘。
首先,请不要评判我的编码方式,我只是发现这是解决 'edges'.
的最简单(也是最长和最无聊)的方式
此外,我认为验证(代码中的这 9 个 if
语句)不会导致任何问题,也不会导致其中的内容(乘法等)。我亲手验证了所有这些,我很确定它们是正确的(虽然我可能是错的)。
我并不是要找人来验证这些陈述。我只是好奇我做错了什么,也许有人能看到我没有看到的东西。
谢谢!
您在计算中使用了错误的数据类型:
RGBTRIPLE gx, gy;
这种类型能够为每个通道保存 8 位值。
当您为每个通道添加单独的组件时,您很容易在一个或多个通道中出现溢出。这会破坏您稍后将每个频道限制为 255
的尝试。
为避免这种情况,只需创建另一个结构:
struct {
uint32_t rgbtRed;
uint32_t rgbtGreen;
uint32_t rgbtBlue;
} gx,gy;
这将允许相加而不会溢出。
我一直在研究哈佛的 CS50 'Filter' 问题,并且在 edges
部分遇到了一点问题。
我一直在尝试用这个算法来解决它:
- 逐个像素地浏览整个图像。图像是
RGBTRIPLE
类型的矩阵,称为image[height][width]
- 检查当前像素是角点、边缘还是中间某处('else' 情况)
- 将该像素和“相邻”像素与相应的
g(x)
和g(y)
值相乘,计算g(x)^2 + g(y)^2
的平方根,相应地对其进行舍入,并导出该值在另一个矩阵中image2[height][width]
.
我在下面提供的代码是我解决问题的尝试。我使用了与 blur
算法中相同的验证(检查角、边或中间),它起作用了。
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE gx, gy;
RGBTRIPLE image2[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
gx.rgbtRed = 0;
gx.rgbtGreen = 0;
gx.rgbtBlue = 0;
gy.rgbtRed = 0;
gy.rgbtGreen = 0;
gy.rgbtBlue = 0;
if (i == 0 && j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
}
else if (i == 0 && j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
}
else if (i == height-1 && j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i][j].rgbtRed;
gy.rgbtGreen += -1 * image[i][j].rgbtGreen;
gy.rgbtBlue += -1 * image[i][j].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (i == height-1 && j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i-1][j-1].rgbtBlue;
}
else if (i == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += image[i][j-1].rgbtRed;
gy.rgbtGreen += image[i][j-1].rgbtGreen;
gy.rgbtBlue += image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (i == height-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
}
else if (j == 0)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
}
else if (j == width-1)
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
}
else
{
gx.rgbtRed += 0 * image[i][j].rgbtRed;
gx.rgbtGreen += 0 * image[i][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i][j].rgbtBlue;
gy.rgbtRed += 0 * image[i][j].rgbtRed;
gy.rgbtGreen += 0 * image[i][j].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j].rgbtBlue;
gx.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j-1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j-1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i-1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i-1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i-1][j].rgbtBlue;
gy.rgbtRed += -2 * image[i-1][j].rgbtRed;
gy.rgbtGreen += -2 * image[i-1][j].rgbtGreen;
gy.rgbtBlue += -2 * image[i-1][j].rgbtBlue;
gx.rgbtRed += image[i-1][j+1].rgbtRed;
gx.rgbtGreen += image[i-1][j+1].rgbtGreen;
gx.rgbtBlue += image[i-1][j+1].rgbtBlue;
gy.rgbtRed += -1 * image[i-1][j+1].rgbtRed;
gy.rgbtGreen += -1 * image[i-1][j+1].rgbtGreen;
gy.rgbtBlue += -1 * image[i-1][j+1].rgbtBlue;
gx.rgbtRed += -2 * image[i][j-1].rgbtRed;
gx.rgbtGreen += -2 * image[i][j-1].rgbtGreen;
gx.rgbtBlue += -2 * image[i][j-1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j-1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j-1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j-1].rgbtBlue;
gx.rgbtRed += 2 * image[i][j+1].rgbtRed;
gx.rgbtGreen += 2 * image[i][j+1].rgbtGreen;
gx.rgbtBlue += 2 * image[i][j+1].rgbtBlue;
gy.rgbtRed += 0 * image[i][j+1].rgbtRed;
gy.rgbtGreen += 0 * image[i][j+1].rgbtGreen;
gy.rgbtBlue += 0 * image[i][j+1].rgbtBlue;
gx.rgbtRed += -1 * image[i+1][j-1].rgbtRed;
gx.rgbtGreen += -1 * image[i+1][j-1].rgbtGreen;
gx.rgbtBlue += -1 * image[i+1][j-1].rgbtBlue;
gy.rgbtRed += image[i+1][j-1].rgbtRed;
gy.rgbtGreen += image[i+1][j-1].rgbtGreen;
gy.rgbtBlue += image[i+1][j-1].rgbtBlue;
gx.rgbtRed += 0 * image[i+1][j].rgbtRed;
gx.rgbtGreen += 0 * image[i+1][j].rgbtGreen;
gx.rgbtBlue += 0 * image[i+1][j].rgbtBlue;
gy.rgbtRed += 2 * image[i+1][j].rgbtRed;
gy.rgbtGreen += 2 * image[i+1][j].rgbtGreen;
gy.rgbtBlue += 2 * image[i+1][j].rgbtBlue;
gx.rgbtRed += image[i+1][j+1].rgbtRed;
gx.rgbtGreen += image[i+1][j+1].rgbtGreen;
gx.rgbtBlue += image[i+1][j+1].rgbtBlue;
gy.rgbtRed += image[i+1][j+1].rgbtRed;
gy.rgbtGreen += image[i+1][j+1].rgbtGreen;
gy.rgbtBlue += image[i+1][j+1].rgbtBlue;
}
int redVal = (int)round(sqrt(gx.rgbtRed * gx.rgbtRed + gy.rgbtRed * gy.rgbtRed));
int greenVal = (int)round(sqrt(gx.rgbtGreen * gx.rgbtGreen + gy.rgbtGreen * gy.rgbtGreen));
int blueVal = (int)round(sqrt(gx.rgbtBlue * gx.rgbtBlue + gy.rgbtBlue * gy.rgbtBlue));
if (redVal > 255) redVal = 255;
if (greenVal > 255) greenVal = 255;
if (blueVal > 255) blueVal = 255;
image2[i][j].rgbtRed = redVal;
image2[i][j].rgbtGreen = greenVal;
image2[i][j].rgbtBlue = blueVal;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = image2[i][j].rgbtRed;
image[i][j].rgbtGreen = image2[i][j].rgbtGreen;
image[i][j].rgbtBlue = image2[i][j].rgbtBlue;
}
}
return;
}
我的代码有很多行,因为它多次使用相同的操作(这是我设法解决问题的最简单方法)。
此外,if
语句中乘法的常量只是矩阵 gx
和 gy
的元素。我无缘无故地用 gx
和 gy
表示这些乘法的结果。我不想为这两个声明两个单独的矩阵,所以我对它们进行了硬编码。
我什至尝试手动验证自己,并使用 printf()
检查角落是否正确识别,一切都相应地工作。
问题是,当我使用 check50
时,这是我的输出:
此外,提供的名为 yard.bmp
的图像被转换为如下所示的“yardEdges.bmp”:
从我的角度来看,这是很多白色,我无法从这张照片中识别出任何边缘。
首先,请不要评判我的编码方式,我只是发现这是解决 'edges'.
的最简单(也是最长和最无聊)的方式此外,我认为验证(代码中的这 9 个 if
语句)不会导致任何问题,也不会导致其中的内容(乘法等)。我亲手验证了所有这些,我很确定它们是正确的(虽然我可能是错的)。
我并不是要找人来验证这些陈述。我只是好奇我做错了什么,也许有人能看到我没有看到的东西。
谢谢!
您在计算中使用了错误的数据类型:
RGBTRIPLE gx, gy;
这种类型能够为每个通道保存 8 位值。
当您为每个通道添加单独的组件时,您很容易在一个或多个通道中出现溢出。这会破坏您稍后将每个频道限制为 255
的尝试。
为避免这种情况,只需创建另一个结构:
struct {
uint32_t rgbtRed;
uint32_t rgbtGreen;
uint32_t rgbtBlue;
} gx,gy;
这将允许相加而不会溢出。