模糊滤镜在C中的结果仅导致图像略有变化。

huangapple go评论64阅读模式
英文:

Blur filter in C results in only a slightly changed image

问题

我正在尝试创建一个在 C 中实现的模糊滤镜,该滤镜获取主像素的相邻像素,计算其RGB值的平均值并将其存储在临时数组中,然后使用临时数组的值更改图像。看起来似乎正确,但它并不按预期工作,输出结果是一个非常轻微模糊的图像。我真的看不出我的错误,如果有人能帮助,我将非常感激,如果我做错了什么,对不起,我上周才开始学习 C 语言。

我检查了这个帖子
https://stackoverflow.com/questions/58296470/blurring-an-image-in-c-pixel-by-pixel-special-cases?rq=1
但我没有看到我哪里出错了。

我使用了以下数据结构:

typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} RGBTRIPLE;

以下是你提供的代码部分:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // 用于稍后使用的整数
    int j;
    int p;

    RGBTRIPLE temp[height][width];
    for(int n = 0; n < height; n++) // 循环以检查每个像素
    {
        for(int k = 0; k < width; k++)
        {
            int widx = 3;
            int hghtx = 3;
            // 用于边界情况的条件
            int y = 0;
            if(n == 0)
            {
            p = 0;
            hghtx = 2;
            }
            if(n == height - 1)
            {
            p = -1;
            hghtx = 2;
            }
            if(k == 0)
            {
            j = 0;
            widx = 2;
            }
            if(k == width - 1)
            {
            j = -1;
            widx = 2;
            }
            for(int u = 0; u < hghtx; u++) // 使用前面收集的条件创建主像素周围的像素矩阵
                for(int i = 0; i < widx; i++)
                if(y == 1) // 获取颜色的平均值并将其存储在 RGB 临时数组中
                {
                temp[n][k].rgbtGreen = temp[n][k].rgbtGreen + image[n + p + u][k + j + i].rgbtGreen / (hghtx * widx);
                temp[n][k].rgbtRed = temp[n][k].rgbtRed + image[n + p + u][k + j + i].rgbtRed / (hghtx * widx);
                temp[n][k].rgbtBlue = temp[n][k].rgbtBlue + image[n + p + u][k + j + i].rgbtBlue / (hghtx * widx);
                }
                else // 获取临时数组的第一个值
                {
                temp[n][k].rgbtGreen = (image[n + p + u][k + j + i].rgbtGreen) / (hghtx * widx);
                temp[n][k].rgbtRed = (image[n + p + u][k + j + i].rgbtRed) / (hghtx * widx);
                temp[n][k].rgbtBlue = (image[n + p + u][k + j + i].rgbtBlue) / (hghtx * widx);
                y++;
                }
        }
    }
    // 将原始图像更改为模糊的图像
    for(int n = 0; n < height; n++)
        for(int k = 0; k < width; k++)
        image[n][k] = temp[n][k];

}

希望这可以帮助你找出问题所在。如果你需要进一步的帮助,请随时提问。

英文:

i am trying to make a blur filter in c that takes the neighboring pixels of the main pixel, takes the avarage of the rgb values and stores it in the temp array, them changes the image using the temp array values, it seems correct but it is not working as intended, giving an output of a very slightly blured image. I realy dont see my mistake and would be very thankful if someone helped, sorry if i made something horrible, started learning c last week.

i checked this post
https://stackoverflow.com/questions/58296470/blurring-an-image-in-c-pixel-by-pixel-special-cases?rq=1
but i did not see were i went wrong.

im working with this data struct

BYTE  rgbtBlue;
BYTE  rgbtGreen;
BYTE  rgbtRed;
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// ints to use later
int j;
int p;
RGBTRIPLE temp[height][width];
for(int n = 0; n < height; n++) // loop to check every pixel
{
for(int k = 0; k < width; k++)
{
int widx = 3;
int hghtx = 3;
// conditionals for border cases
int y = 0;
if(n == 0)
{
p = 0;
hghtx = 2;
}
if(n == height - 1)
{
p = -1;
hghtx = 2;
}
if(k == 0)
{
j = 0;
widx = 2;
}
if(k == width - 1)
{
j = -1;
widx = 2;
}
for(int u = 0; u < hghtx; u++) // matrix of pixels around the main pixel using the conditionals gathered before
for(int i = 0; i < widx; i++)
if(y == 1) // takes the average of color and stores it in the RGB temp
{
temp[n][k].rgbtGreen = temp[n][k].rgbtGreen + image[n + p + u][k + j + i].rgbtGreen / (hghtx * widx);
temp[n][k].rgbtRed = temp[n][k].rgbtRed + image[n + p + u][k + j + i].rgbtRed / (hghtx * widx);
temp[n][k].rgbtBlue = temp[n][k].rgbtBlue + image[n + p + u][k + j + i].rgbtBlue / (hghtx * widx);
}
else // get first value of temp
{
temp[n][k].rgbtGreen = (image[n + p + u][k + j + i].rgbtGreen) / (hghtx * widx);
temp[n][k].rgbtRed = (image[n + p + u][k + j + i].rgbtRed) / (hghtx * widx);
temp[n][k].rgbtBlue = (image[n + p + u][k + j + i].rgbtBlue) / (hghtx * widx);
y++;
}
}
}
// changes the original image to the blured one
for(int n = 0; n < height; n++)
for(int k = 0; k < width; k++)
image[n][k] = temp[n][k];
}
</details>
# 答案1
**得分**: 1
我认为这是一些因素的结合。
1) 如果代码按照您的预期工作,那么您仍然会对3x3像素进行模糊处理,这在大尺寸图像上可能几乎不可察觉(我非常确定在4000x3000像素的图像上几乎不会被注意到)。
2) 代码存在一些问题。
- 正如@Fe2O3所说,第一行末尾,widx将变为2,并保持为2,直到图像的剩余部分。
- 您从temp[][]中读取数据而未对其进行初始化。我认为,如果您以发布模式(而不是调试模式)编译它,temp[][]将包含随机数据,而不是您可能期望的全零数据(正如@WeatherWane所指出的)。
- 您计算像素平均值的方式有点奇怪。如果您使用一个3x3像素的矩阵,每个像素的值应该在最终总和中除以9。但您将第一个像素除以2九次(实际上是/256),第二个像素除以2八次(所以它的像素/128)等等,直到最后一个像素除以2。所以基本上,这主要是底部右侧像素的值。
- 此外,由于您的RGB值只是字节,您可能希望首先将它们除以9,然后再进行相加,否则,您将获得溢出和不可预测的结果。
尝试使用调试器查看您实际计算的值,这可能会让您大开眼界 :)
<details>
<summary>英文:</summary>
I think it&#39;s a combination of things.
1) If the code worked the way you expect, you would be still doing a blur of just 3x3 pixels and that can be hardly noticeable, especially on large images (I&#39;m pretty sure it will be unnoticeable on an image 4000x3000 pixels)
2) There are some problems with the code.
- As @Fe2O3 says, at the end of the first line, widx will change to 2 and stay 2 for the rest of the image.
- you are reading from temp[][] without initializing it. I think that if you compile that in release mode (not debug), temp[][] will contain random data and not all zeros as you probably expect. (as @WeatherWane pointed out)
- The way you calculate the average of the pixels is weird. If you use a matrix 3x3 pixels, each pixel value shoud be divided by 9 in the final sum. But you divide the first pixel nine times by 2 (in effect doing /256), the second one eight times by 2 (so its pixel/128) etc. until the last one is divided by 2. So basically, it&#39;s mostly the value of the bottom right pixel.
- also, since your RGB values are just bytes, you may want to divide them first and only then add them, because otherwise, you&#39;ll get overflows with wild results.
Try using a debugger to see the values you are actually calculating. It can be quite an eye opener :)
</details>

huangapple
  • 本文由 发表于 2023年2月14日 07:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定