0% found this document useful (0 votes)
17 views2 pages

Public Class: Import

This document contains the code for a FilterExample class that smooths an image by averaging the colors of neighboring pixels. The smooth method takes in an image and neighborhood size, creates a result array, and loops through each pixel to calculate the average color of surrounding pixels using the aveOfNeighbors helper method. AveOfNeighbors calculates the average red, green, and blue values of pixels within the neighborhood size by adding their color values and dividing by the number of neighbors. Additional helper methods check if coordinates are in bounds and that the image is rectangular.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Public Class: Import

This document contains the code for a FilterExample class that smooths an image by averaging the colors of neighboring pixels. The smooth method takes in an image and neighborhood size, creates a result array, and loops through each pixel to calculate the average color of surrounding pixels using the aveOfNeighbors helper method. AveOfNeighbors calculates the average red, green, and blue values of pixels within the neighborhood size by adding their color values and dividing by the number of neighbors. Additional helper methods check if coordinates are in bounds and that the image is rectangular.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C:\Users\Deeksha\Downloads\FilterExample.

java

Friday, December 19, 2014 7:07 PM

// Mike Scott
// 2d array manipulation examples
//import
import java.awt.Color;

public class FilterExample


{
/*
*pre: image != null, image.length > 1, image[0].length > 1
* image is a rectangular matrix, neighberhoodSize > 0
*post: return a smoothed version of image
*/
public Color[][] smooth(Color[][] image, int neighberhoodSize)
{
//check precondition
assert image != null && image.length > 1 && image[0].length > 1
&& ( neighberhoodSize > 0 ) && rectangularMatrix( image )
: "Violation of precondition: smooth";
Color[][] result = new Color[image.length][image[0].length];
for(int row = 0; row < image.length; row++)
{
for(int col = 0; col < image[0].length; col++)
{
result[row][col] = aveOfNeighbors(image, row, col,
neighberhoodSize);
}
}
return result;
}

// helper method that determines the average color of a neighberhood


// around a particular cell.
private Color aveOfNeighbors(Color[][] image, int row, int col, int
neighberhoodSize)
{
int numNeighbors = 0;
int red = 0;
int green = 0;
int blue = 0;
for(int r =
{
for(int
{
if(
{

row - neighberhoodSize; r <= row + neighberhoodSize; r++)


c = col - neighberhoodSize; c <= col + neighberhoodSize; c++)
inBounds( image, r, c ) )
numNeighbors++;
red += image[r][c].getRed();
green += image[r][c].getGreen();
blue += image[r][c].getBlue();

}
}
}

-1-

C:\Users\Deeksha\Downloads\FilterExample.java

Friday, December 19, 2014 7:07 PM

assert numNeighbors > 0;


return new Color( red / numNeighbors, green / numNeighbors, blue /
numNeighbors );
}
//helper method to determine if given coordinates are in bounds
private boolean inBounds(Color[][] image, int row, int col)
{
return (row >= 0) && (row <= image.length) && (col >= 0)
&& (col < image[0].length);
}
//private method to ensure mat is rectangular
private boolean rectangularMatrix( Color[][] mat )
{
boolean isRectangular = true;
int row = 1;
final int COLUMNS = mat[0].length;
while( isRectangular && row < mat.length )
{
isRectangular = ( mat[row].length == COLUMNS );
row++;
}
return isRectangular;
}
}

-2-

You might also like