平均模糊代码错误

发布于 2024-10-27 13:30:15 字数 2868 浏览 3 评论 0原文

PImage toAverageBlur(PImage sourceImg)

{

    PImage newImg = new PImage(sourceImg.width, sourceImg.height);
    float r0,r1,r2,r3,r4,r5,r6,r7,r8;
    float b0,b1,b2,b3,b4,b5,b6,b7,b8;
    float g0,g1,g2,g3,g4,g5,g6,g7,g8;
    float total;

    newImg.loadPixels();
    for(int i = 1; i < sourceImg.width-1; i++)
    {
      for(int j = 1; j < sourceImg.height-1; j++)
      {
        int pixelPosition = i*sourceImg.width + j;



         r1 = red(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         b1 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         g1 = green(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         float sumOne = (r1+b1+g1)/9;


         r2 = red(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         b2 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         g2 = green(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         float sumTwo = (r2+b2+g2)/9;


         r3 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         b3 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         g3 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         float sumThree = (r3+b3+g3)/9;


         r4 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         b4 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         g4 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         float sumFour = (r4+b4+g4)/9;


         r5 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         b5 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         g5 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         float sumFive = (r5+b5+g5)/9;


         r6 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         b6 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         g6 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         float sumSix = (r6+b6+g6)/9;


         r7 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         b7 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         g7 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         float sumSeven = (r7+b7+g7)/9;


         r8 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         b8 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         g8 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         float sumEight = (r8+b8+g8)/9;

         r0 = red(sourceImg.pixels[pixelPosition]);
         b0 = blue(sourceImg.pixels[pixelPosition]);
         g0 = green(sourceImg.pixels[pixelPosition]);
         float sumZero = (r0+b0+g0)/9;

         total = sumOne+sumTwo+sumThree+sumFour+sumFive+sumSix+sumSeven+sumEight+sumZero;

         newImg.pixels[pixelPosition] =  color(total);


      }
    }
   newImg.updatePixels();
    return newImg;
}
PImage toAverageBlur(PImage sourceImg)

{

    PImage newImg = new PImage(sourceImg.width, sourceImg.height);
    float r0,r1,r2,r3,r4,r5,r6,r7,r8;
    float b0,b1,b2,b3,b4,b5,b6,b7,b8;
    float g0,g1,g2,g3,g4,g5,g6,g7,g8;
    float total;

    newImg.loadPixels();
    for(int i = 1; i < sourceImg.width-1; i++)
    {
      for(int j = 1; j < sourceImg.height-1; j++)
      {
        int pixelPosition = i*sourceImg.width + j;



         r1 = red(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         b1 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         g1 = green(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]);
         float sumOne = (r1+b1+g1)/9;


         r2 = red(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         b2 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         g2 = green(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]);
         float sumTwo = (r2+b2+g2)/9;


         r3 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         b3 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         g3 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]);
         float sumThree = (r3+b3+g3)/9;


         r4 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         b4 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         g4 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]);
         float sumFour = (r4+b4+g4)/9;


         r5 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         b5 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         g5 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]);
         float sumFive = (r5+b5+g5)/9;


         r6 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         b6 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         g6 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]);
         float sumSix = (r6+b6+g6)/9;


         r7 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         b7 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         g7 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]);
         float sumSeven = (r7+b7+g7)/9;


         r8 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         b8 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         g8 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]);
         float sumEight = (r8+b8+g8)/9;

         r0 = red(sourceImg.pixels[pixelPosition]);
         b0 = blue(sourceImg.pixels[pixelPosition]);
         g0 = green(sourceImg.pixels[pixelPosition]);
         float sumZero = (r0+b0+g0)/9;

         total = sumOne+sumTwo+sumThree+sumFour+sumFive+sumSix+sumSeven+sumEight+sumZero;

         newImg.pixels[pixelPosition] =  color(total);


      }
    }
   newImg.updatePixels();
    return newImg;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-11-03 13:30:15

我认为它应该是 int PixelPosition = j*a.width + i;int PixelPosition = i*a.height + j;

假设你的图像是 100x10 这意味着有 1000 个元素。如果您正在查看元素 (50,5),则 i=50 且 j=5,原始代码显示 pixelPosition = i*a.width + j; 这意味着 PixelPosition = 50*100+5 = 5005 这远远超出了范围。如果你查看你的代码,你总是使用 (i +/- 1) * width + (j +/- 1) 所以对于 i 小于高度的行来说一切都会好的,但其他一切都会超出范围。

下一个问题是对红色、绿色和蓝色进行平均。您应该转换为灰色或其他颜色空间变量而不是合并颜色。

下一个问题是舍入误差。当 RGB 值很小时,每个总和都会被截断。使用浮点类型,然后在最后一步中进行转换。

下一个问题在最后一行:

aBlur.pixels[pixelPosition] = a.pixels[pixelPosition] + color(total);

aBlur似乎应该是输入图像的灰度模糊,但我不知道颜色空间是如何设置的,让我们假设它是RGB。您复制原始像素并添加模糊的彩色版本?

假设原始像素为 (150,150,150),得到的平均值为 170。如果函数 color(170) 生成像素 (170,170,170),则原始像素与平均值之和将为 (320,320,320)。所以现在的像素值与原始的模糊版本相差甚远。

我怀疑这条线应该是

aBlur.pixels[pixelPosition] = color(total);

I think it should be either int pixelPosition = j*a.width + i; or int pixelPosition = i*a.height + j;

Suppose your image is 100x10 that means there are 1000 elements. If you are looking at element (50,5) then i=50 and j=5, the original code says pixelPosition = i*a.width + j; that means pixelPosition = 50*100+5 = 5005 which is way out of range. If you look at your code you always use (i +/- 1) * width + (j +/- 1) so everything will be OK for the lines where i is less than the height but everything else is going to be out of range.

The next problem is averaging red, green and blue. You should either convert to grey or another colour space variable not merge colours.

The next problem is rounding error. Each sum is going to be truncated when the RGB values are small. Use a floating point type and then cast it in the last step.

The next problem is in the final line:

aBlur.pixels[pixelPosition] = a.pixels[pixelPosition] + color(total);

aBlur seems to be like it's supposed to be a greyscale blur of the input image, but I don't know how the colour space is set, let's assume it's RGB. You copy the original pixel and add in a colourised version of the blur ?

Suppose the original pixel is (150,150,150) and the averaged value you get is 170. If the function color(170) makes a pixel (170,170,170) then the sum of the original and average is going to be (320,320,320). So now the pixel value is far from a blurred version of the original.

I suspect this line is supposed to be

aBlur.pixels[pixelPosition] = color(total);
薄暮涼年 2024-11-03 13:30:15

我在这里写了关于如何进行模糊内核(以及一般的卷积)的非常全面的描述:

如何在不使用任何内置高斯函数的情况下对图像进行高斯模糊?

I've written a pretty comprehensive description of how to do blur kernels (and convolution in general) here:

How do I gaussian blur an image without using any in-built gaussian functions?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文