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.*; /** * Convolution is the code for applying the convolution operator. * * @author: Simon Horne */ public class Convolution extends Thread { /** * Default no-arg constructor. */ public Convolution() { } /** * Takes an image (grey-levels) and a kernel and a position, * applies the convolution at that position and returns the * new pixel value. * * @param input The 2D double array representing the image. * @param x The x coordinate for the position of the convolution. * @param y The y coordinate for the position of the convolution. * @param k The 2D array representing the kernel. * @param kernelWidth The width of the kernel. * @param kernelHeight The height of the kernel. * @return The new pixel value after the convolution. */ public static double singlePixelConvolution(double [][] input, int x, int y, double [][] k, int kernelWidth, int kernelHeight){ double output = 0; for(int i=0;i255){ grey = 255; }else if(greys[i]<0){ grey = 0; }else{ grey = (int) Math.round(greys[i]); } result[i] = (new Color(grey,grey,grey)).getRGB(); } return result; } /** * Applies the convolution2D algorithm to the input array as many as * iterations. * @param input the 2D double array representing the image * @param width the width of the image * @param height the height of the image * @param kernel the 2D array representing the kernel * @param kernelWidth the width of the kernel * @param kernelHeight the height of the kernel * @param iterations the number of iterations to apply the convolution * @return the 2D array representing the new image */ public double [][] convolutionType1(double [][] input, int width, int height, double [][] kernel, int kernelWidth, int kernelHeight, int iterations){ double [][] newInput = (double [][]) input.clone(); double [][] output = (double [][]) input.clone(); for(int i=0;i255) outputInts[i] = 255; if(outputInts[i]<0) outputInts[i] = 0; int g = outputInts[i]; outputInts[i] = (new Color(g,g,g)).getRGB(); } return outputInts; } }