This site requires JavaScript, please enable it in your browser!
Greenfoot back
ironphoenix20
ironphoenix20 wrote ...

2017/5/1

Gaussian Blur Image

ironphoenix20 ironphoenix20

2017/5/1

#
Hi, I want to do a gaussian blur on my image and I found some code online to do this but I am confused with some aspects of this method. Here is the method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static GreenfootImage blur(BufferedImage image, int[] filter, int filterWidth) {
        if (filter.length % filterWidth != 0) {
            throw new IllegalArgumentException("filter contains a incomplete row");
        }
 
        final int width = image.getWidth();
        final int height = image.getHeight();
        final int sum = IntStream.of(filter).sum();
 
        int[] input = image.getRGB(0, 0, width, height, null, 0, width);
 
        int[] output = new int[input.length];
 
        final int pixelIndexOffset = width - filterWidth;
        final int centerOffsetX = filterWidth / 2;
        final int centerOffsetY = filter.length / filterWidth / 2;
 
        // apply filter
        for (int h = height - filter.length / filterWidth + 1, w = width - filterWidth + 1, y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int r = 0;
                int g = 0;
                int b = 0;
                for (int filterIndex = 0, pixelIndex = y * width + x;
                filterIndex < filter.length;
                pixelIndex += pixelIndexOffset) {
                    for (int fx = 0; fx < filterWidth; fx++, pixelIndex++, filterIndex++) {
                        int col = input[pixelIndex];
                        int factor = filter[filterIndex];
 
                        // sum up color channels seperately
                        r += ((col >>> 16) & 0xFF) * factor;
                        g += ((col >>> 8) & 0xFF) * factor;
                        b += (col & 0xFF) * factor;
                    }
                }
                r /= sum;
                g /= sum;
                b /= sum;
                // combine channels with full opacity
                output[x + centerOffsetX + (y + centerOffsetY) * width] = (r << 16) | (g << 8) | b | 0xFF000000;
            }
        }
 
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        result.setRGB(0, 0, width, height, output, 0, width);
        return createGreenfootImageFromBI(result);
    }
* createGreenfootImageFromBI() is just a method I have in my class to convert BufferedImage to GrennfootImage Anyway, my problem is I don't understand what the second and third parameter (filter and filterWidth) are in this method are. I am trying to call this method on my image (which I have as a BufferedImage) but what do I enter for the other two parameters? My image is 800 x 600.
ironphoenix20 ironphoenix20

2017/5/2

#
Why would I need those two other parameters? Can anyone explain it to me?
danpost danpost

2017/5/2

#
ironphoenix20 wrote...
Why would I need those two other parameters? Can anyone explain it to me?
I believe that the array (the second parameter) is the matrix used in the process and the int value (last parameter) is the number of columns (and rows) in the array. So, if the matrix was a 7x7 array, then '7' would be passed as the int value.
ironphoenix20 ironphoenix20

2017/5/2

#
but what is the matrix? what do i need to pass in as the matrix and number of columbs(and rows) in order to blur my image? The number of pixels in my image?
ironphoenix20 ironphoenix20

2017/5/2

#
but how do i pass in one number for the third parameter then since the width and height of my image are different?
danpost danpost

2017/5/2

#
ironphoenix20 wrote...
but what is the matrix? what do i need to pass in as the matrix and number of columbs(and rows) in order to blur my image? The number of pixels in my image?
No. The process involves iterating through all the pixels of the image and applying a change to each pixel. This change is determined by the color of that pixel and by the colors of its neighboring pixels. The amount of influence its neighbors has to the change is determined by a value within the matrix. The influential amount given to the pixel being changed is determined by the value smack dab in the center of the matrix and the other values of the matrix determine the influence of the neighboring pixels (left of center in the matrix is for pixels left of the pixel being changed; right - right, etc.). All values in the matrix that are equidistant from the center (as the crow flies) will have the same value and values diminish exponentially as they spread away from the center. The example of a matrix I have seen used values between zero and one. However, the array for your method uses int values. I could only presume that the center value will be fairly large and the outer values will be quite small.
ironphoenix20 ironphoenix20

2017/5/2

#
so, the array i pass in is basically my pixel and other surrounding pixels. how large should i make it then?
ironphoenix20 ironphoenix20

2017/5/2

#
But then how do i pass in my whole image as a parameter if the array is only for one pixel. How do I make the blur effect on my whole image using the one method call?
danpost danpost

2017/5/2

#
ironphoenix20 wrote...
so, the array i pass in is basically my pixel and other surrounding pixels. how large should i make it then?
No. The array is a set of values that give transform weight to the color of each pixel within a small area of your image. The matrix is used for each pixel within the image. As a test you could do something simple, like the following:
1
2
int[] matrix = new int[] { 1, 2, 1, 2, 6, 2, 1, 2, 1 };
for (int i=0; i<1; i++) blur(image.getBuffferedImage(), matrix, 3);
Test it with 'i<1' in line 2 to start. Then increase the '1' to '2' and then to '3', testing each time.
You need to login to post a reply.