Average blur code bug
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(sou开发者_开发问答rceImg.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;
}
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);
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?
精彩评论