如何解决用 C 中的框 BLUR 模糊 .BMP
恢复。我在下面写的代码没有给我一个模糊的图像。但取而代之的是黑色图像(像素边缘除外)。 为了测试,我刚刚尝试了内部 PIXELS,没有边缘 RGBTRIPLE 像素,因为我想看看到目前为止我是否做得很好。
有人能告诉我我的代码有什么问题吗? muy BUFFER RGB newImage 是否正确完成?如果 image[height][width].rgbtRed 表示 newImage[height][width].rgbtRed 会存在??提前致谢
// Blur image
void blur(int height,int width,RGBTRIPLE image[height][width])
{
//create a buffer to allocate new pixels.
RGBTRIPLE newImage[height][width];
// iterate through each row
for (int i = 0 ; i < height ; i++)
{
// iterate through each column passing through every RGBTRIPLE
for ( int j = 0; j < width ; j++)
{
int redSum = 0;
int greenSum = 0;
int blueSum = 0;
//counter for pixels being add in every avg.
//int count = 0;
// take the average amount of RGB of every pixel in a range of 3x3 pixels
// avg for left top corner
/*if ( r == 0 && l == 0) {
}
// avg left bottom corner
else if ( r == height - 1 && l == 0 ){
}
// avg right top corner
else if ( r == 0 && l == width - 1){
}
// avg left bottom corner
else if ( r == height - 1 && l == width - 1){
}
// avg top side
else if ( r == 0 && l > 0 && l != width - 1) {
}
// avg right side
else if ( l == width - 1 && r > 0 && r != height - 1) {
}
// avg bottom side
else if ( r == height - 1 && l > 0 && l != width - 1) {
}
// avg left side
else if ( l == 0 && r > 0 && r != height - 1){
}*/
for ( int iy = -1 ; iy >= 1 ; iy++)
{
for ( int jx = -1 ; jx >= 1 ; jx++)
{
redSum = redSum + image[i + iy][j + jx].rgbtRed;
greenSum = greenSum + image[i + iy][j + jx].rgbtGreen;
blueSum = blueSum + image[i + iy][j + jx].rgbtBlue;
//count++;
}
}
int avgRed = redSum / 9;
int avgGreen = greenSum / 9;
int avgBlue = blueSum / 9;
newImage[i][j].rgbtRed = avgRed;
newImage[i][j].rgbtGreen = avgGreen;
newImage[i][j].rgbtBlue = avgBlue;
}
}
for ( int y = 1 ; y < height - 1; y++)
{
for ( int x = 1; x < width - 1; x++)
{
image[y][x].rgbtRed = newImage[y][x].rgbtRed;
image[y][x].rgbtGreen = newImage[y][x].rgbtGreen;
image[y][x].rgbtBlue = newImage[y][x].rgbtBlue;
}
}
return;
}
解决方法
注释中尚未提及的一个主要错误是 redSum
、greenSum
和 blueSum
仅初始化为 0 一次,而在处理每个像素之前必须将它们设置为零,所以最好在 for ( int py = 0 ; py >= 2 ; py++)
循环之前移动定义。
令人难以置信的是,许多看着代码的眼睛(包括我的)竟然会忽略主要的、简单的错误——内循环(在它们的原始版本和编辑后的版本中)从未执行过:
for ( int iy = -1 ; iy >= 1 ; iy++)
{
for ( int jx = -1 ; jx >= 1 ; jx++)
当然,iy >= 1
条件从一开始就是假的;你肯定是指iy <= 1
和jx <= 1
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。