【发布时间】:2021-05-25 19:35:23
【问题描述】:
继续。我在下面编写的代码没有给我一个模糊的图像。但取而代之的是黑色图像(像素边缘除外)。 为了测试,我刚刚尝试了内部 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;
}
【问题讨论】:
-
这个:
for ( int py = 0 ; py >= 2 ; py++)看起来不对。如果您更正比较,您将越界访问像素缓冲区,因为与您所说的相反,您不会只考虑内部像素,至少不会在大量测试被注释掉的情况下。 -
请,请不要使用变量名 l。是
l还是1?在单型字体中,您可以通过眯眼辨别,但在比例字体中则不行。它只是使代码很难阅读。无论如何,将r和l一起使用会欺骗读者认为它们是right和left,但它们是row和column。 -
/*if ( r == 0 && l == 0) { } // avg left bottom corner else if ( r == height - 1 && l == 0 ){ ... else if ( l == 0 && r > 0 && r != height - 1){ }*/什么都不做。为什么会在那里? -
@WeatherVane 谢谢。我会考虑到这一点。
-
@chux-ReinstateMonica 这是我的下一步行动......但我正在尝试解决第一个问题。