import java.applet.*; import java.awt.*; import java.awt.image.*; import java.net.*; import java.util.*; import java.io.*; import java.lang.Math; import java.awt.Color.*; /** * ConSmooth is an algorithm to conservatively smooth an image * using a 3x3 kernel. * * @author Simon Horne. */ public class ConSmooth extends Thread { /** * Default no-arg constructor. */ public ConSmooth() { } /** * Finds the maximum value from a 3x3 pixel neighbourhood overlaid with * a 3x3 kernel (on/off). The centre pixel of the kernel is ignored. * * @param input The 2D array of pixel values representing the image. * @param kernel The array representing the kernel. * @param w The width of the image. * @param h The height of the image. * @param x The x coordinate of the centre of the 3x3 neighbourhood. * @param y The y coordinate of the centre of the 3x3 neighbourhood. * @return The maximum value. */ public static int maxNeighbour(int [][] input, int [] kernel, int w, int h, int x, int y) { int [] neighbour = new int [9]; boolean [] neighbourPresent = new boolean [9]; int max; for(int j=0;j<3;++j){ for(int i=0;i<3;++i){ if((kernel[(3*j)+i]==1)&&(((x-1+i) > 0)&&((x-1+i) < w)&&((y-1+j) > 0)&&((y-1+j) < h))) { neighbour[(3*j)+i] = input[x-1+i][y-1+j]; neighbourPresent[(3*j)+i] = true; }else{ neighbour[(3*j)+i] = 0; neighbourPresent[(3*j)+i] = false; } } } max = 0; for(int i=0;i<9;++i){ if((neighbourPresent[i])&&(neighbour[i]>max)&&(i!=4)){ max = neighbour[i]; } } return max; } /** * Finds the minimum value from a 3x3 pixel neighbourhood overlaid with * a 3x3 kernel (on/off). The centre pixel of the kernel is ignored. * * @param input The 2D array of pixel values representing the image. * @param kernel The array representing the kernel. * @param w The width of the image. * @param h The height of the image. * @param x The x coordinate of the centre of the 3x3 neighbourhood. * @param y The y coordinate of the centre of the 3x3 neighbourhood. * @return The minimum value. */ public static int minNeighbour(int [][] input, int [] kernel, int w, int h, int x, int y) { int [] neighbour = new int [9]; boolean [] neighbourPresent = new boolean [9]; int min; for(int j=0;j<3;++j){ for(int i=0;i<3;++i){ if((kernel[(j*3)+i]==1)&&(((x-1+i) > 0)&&((x-1+i) < w)&&((y-1+j) > 0)&&((y-1+j) < h))){ neighbour[(3*j)+i] = input[x-1+i][y-1+j]; neighbourPresent[(3*j)+i] = true; }else{ neighbour[(3*j)+i] = 0; neighbourPresent[(3*j)+i] = false; } } } min = 255; for(int i=0;i<9;++i){ if((neighbourPresent[i])&&(neighbour[i]max){ outputArrays[i][j]=max; }else if(inputArrays[i][j]max){ outputArrays[i][j]=max; }else if(inputArrays[i][j]