1. Member
Join Date
Feb 2010
Posts
1
Rep Power
0

## Array Question

I'm trying to put a filter over an image using arrays. For each given pixel, I need to take itself and the 8 surrounding pixels, multiply by the corresponding weights in the "coefs" array, and use the sum to set the new center pixel. So far, I've been able to make arrays for the pixel's 3 colors, and I have the coefs array. I have no clue how to make a new array that multiplies each entry in one array by each entry in another array.

Below is what I have so far. Any help is greatly appreciated.

Java Code:
```	public void filter(Image source, Image target) {
for (int x = 1; x < source.getWidth() - 1; x++) {
for (int y = 1; y < source.getHeight() - 1; y++) {

Color center = source.getPixelColor(x,y);
Color upleft = source.getPixelColor(x-1,y-1);
Color up = source.getPixelColor(x,y-1);
Color upright = source.getPixelColor(x+1,y-1);
Color right = source.getPixelColor(x+1,y);
Color downright = source.getPixelColor(x+1,y+1);
Color down = source.getPixelColor(x,y+1);
Color downleft = source.getPixelColor(x-1,y+1);
Color left = source.getPixelColor(x-1,y);

int[][] arrayRed = {
{upleft.getRed(), up.getRed(), upright.getRed()},
{left.getRed(), center.getRed(), right.getRed()},
{downleft.getRed(), down.getRed(), downright.getRed()}
};

int[][] arrayGreen = {
{upleft.getGreen(), up.getGreen(), upright.getGreen()},
{left.getGreen(), center.getGreen(), right.getGreen()},
{downleft.getGreen(), down.getGreen(), downright.getGreen()}
};

int[][] arrayBlue = {
{upleft.getBlue(), up.getBlue(), upright.getBlue()},
{left.getBlue(), center.getBlue(), right.getBlue()},
{downleft.getBlue(), down.getBlue(), downright.getBlue()}
};

double[][] coefs = {
{.0625, .125, .0625},
{.125, .25, .125},
{.0625, .125, .0625}
};

}
}
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Use two nested for loops:

Java Code:
```for(int row = 0; row < 3; row++) {
for(int col = 0; col < 3; col++) {
// do stuff
}
}```

You can process all three colour arrays within the same pair of loops. And also do the addition.

You might want to consider ConvolveOp (also described in the Java2D API guide)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•