0% found this document useful (0 votes)
13 views

Compimp

Uploaded by

anusayashinde10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Compimp

Uploaded by

anusayashinde10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 = row - neighberhoodSize; r <= row + neighberhoodSize; r++)

{ for(int c = col - neighberhoodSize; c <= col + neighberhoodSize; c++)

{ if( inBounds( image, r, c ) )

{ numNeighbors++;

red += image[r][c].getRed();

green += image[r][c].getGreen();

blue += image[r][c].getBlue();

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;

Class FilterExample
java.lang.Object
FilterExample

public class FilterExample extends Object

 Constructor Summary

Constructors

Constructor

Description

FilterExample()

 Method Summary

All MethodsInstance MethodsConcrete Methods

Modifier and Type

Method
Description

Color[][]

smooth(Color[][] image, int neighberhoodSize)

Methods inherited from class java.lang.Object

clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait


, wait

 Constructor Details

 FilterExample

public FilterExample()

 Method Details

 smooth
 public Color[][] smooth(Color[][] image,

int neighberhoodSize)

You might also like