Results 1 to 2 of 2

Thread: Array Question

  1. #1
    sc001 is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default 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. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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)

Similar Threads

  1. need help with question(method & array)
    By highschool in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 05:06 PM
  2. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  3. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM
  4. file/ array question
    By mayhewj7 in forum New To Java
    Replies: 10
    Last Post: 02-18-2009, 03:54 PM
  5. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 04:44 PM

Posting Permissions

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