c - CS50x - 过滤器更多
问题描述
我在c中对图像bmp进行过滤。pset 需要 Sobel 运算符。我不知道我错在哪里。
请帮帮我。
我基本上是在复制我的图像(因为原件会被更改。)
然后我将 3x3 值放入公式中
所以我们加和乘
最后我把结果代入公式:平方根 (Gx ^ 2 + Gy ^ 2)
如果超过 255 则必须为 255,因为 RGB 上升到 255 即为白色
如果有残缺的数字,则四舍五入到最接近的数字
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Variáveis
RGBTRIPLE temp[height][width];
int GR[3][3];
int GG[3][3];
int GB[3][3];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
float resultR, resultG, resultB;
// Cópia temporária do original
for (int tempi = 0; tempi < height; tempi++)
{
for (int tempj = 0; tempj < width; tempj++)
{
temp[tempi][tempj].rgbtRed = image[tempi][tempj].rgbtRed;
temp[tempi][tempj].rgbtGreen = image[tempi][tempj].rgbtGreen;
temp[tempi][tempj].rgbtBlue = image[tempi][tempj].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int countx = 0;
// Pegar 3X3
for (int x = i - 1; x < i + 2; x++)
{
int county = 0;
for (int y = j - 1; y < j + 2; y++)
{
if ((x < 0 || y < 0) || (x >= height || y >= width))
{
GR[countx][county] = 0;
GG[countx][county] = 0;
GB[countx][county] = 0;
}
else
{
GR[countx][county] = temp[x][y].rgbtRed;
GG[countx][county] = temp[x][y].rgbtGreen;
GB[countx][county] = temp[x][y].rgbtBlue;
}
county++;
}
countx++;
}
float sumxR = 0, sumyR = 0, sumxG = 0, sumyG = 0, sumxB = 0, sumyB = 0;
for (int ix = 0; ix <= 2; ix++)
{
for (int iy = 0; iy <= 2; iy++)
{
sumxR = sumxR + (GR[ix][iy] * Gx[ix][iy]);
sumxG = sumxG + (GG[ix][iy] * Gx[ix][iy]);
sumxB = sumxB + (GB[ix][iy] * Gx[ix][iy]);
sumyR = sumyR + (GR[ix][iy] * Gy[ix][iy]);
sumyG = sumyG + (GG[ix][iy] * Gy[ix][iy]);
sumyB = sumyB + (GB[ix][iy] * Gy[ix][iy]);
}
}
resultR = sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR);
resultG = sqrt(sumxG * sumxG) + sqrt(sumyG * sumyG);
resultB = sqrt(sumxB * sumxB) + sqrt(sumyB * sumyB);
if (resultR > 255)
{
resultR = 255;
}
if (resultG > 255)
{
resultG = 255;
}
if (resultB > 255)
{
resultB = 255;
}
image[i][j].rgbtRed = round(resultR);
image[i][j].rgbtGreen = round(resultG);
image[i][j].rgbtBlue = round(resultB);
}
}
}
解决方案
您需要更改sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR)
为sqrt((sumxR * sumxR) + (sumyR * sumyR))
,它们不一样
推荐阅读
- java - 如何跨微服务拥有通用 DTO 或实体
- python - 为什么字符串比较不比 Python 中的整数比较快?
- mysql - MariaDB 复制设置后出现重复键错误
- firebase - 通过 Firebase ID 验证用户
- postman - 如何提取原始响应数据并将其添加到邮递员中的环境变量中
- c++ - 类函数可以被线程调用并访问类数据吗?
- gcc - GNU Arm Embedded Toolchain: undefined reference to `__sync_synchronize'
- java - JSON 字符串无效
- xamarin - 有没有办法在没有自定义渲染器的情况下以 xamarin 表单实现 toast 消息
- r - R中带有平均值列的频率表