dLife Home Page

dlife.vision.filter
Class ConvolutionFilter

java.lang.Object
  extended by java.util.Observable
      extended by dlife.vision.filter.Filter
          extended by dlife.vision.filter.ConvolutionFilter
Direct Known Subclasses:
GaussianFilter, LaplacianFilter

public class ConvolutionFilter
extends Filter

Filters an image by convolving a kernel over the image. A new value for each pixel is computed by placing the convolution kernel over the image with the pixel at the center. The values in the convolution kernel are then used to compute a weighted sum of the pixels covered by the kernel. The value of the pixel at the center of the kernel is then set to the weighted sum.

The ConvolutionFilter GUI (shown below) allows the user to enter values for a convolution kernel of up to 7x7. The 1x1 kernel below will make the image 1/2 as bright.

NOTES:

The convolution operation forms the basis for a wide variety of image filters. For example the GaussianFilter and LaplacianFilter are both sub-classes of the ConvolutionFilter. More information and a brief introduction to Convolution Matrices (filters) can be found in the gimp manual (Note: the writing on this page is a little rough, but it provides a good feel for convolution filters).

Version:
Jul 10, 2008
Author:
Grant Braught, Dickinson College

Nested Class Summary
protected  class ConvolutionFilter.ConvolutionFilterGUI
          GUI for a ConvolutionFilter.
 
Constructor Summary
ConvolutionFilter()
          Construct a new ConvolutionFilter.
ConvolutionFilter(float[][] kernel, int pixelRow, int pixelCol)
          Construct a new ConvolutionFilter using the specified kernel.
 
Method Summary
protected  FilterGUI createGUIObject()
          Get the GUI for this ConvolutionFilter.
 BufferedImage filter(BufferedImage frame)
          Apply the convolution kernel to the image in frame and return the result.
 String getCode(String name)
          Get a String containing the Java code that would be necessary to create this Filter in its current state and add it to the specified VisionClient.
 float[][] getKernel()
          Get the convolution kernel from this ConvolutionFilter.
static void main(String[] args)
          Main method that pops up a GUI for the ConvolutionFilter to allow it to be tested.
 void setKernel(float[][] newKernel, int pixelRow, int pixelCol)
          Set the convolution kernel for this ConvolutionFilter.
 void setKernelCell(int row, int col, float value)
          Set the value of one of the cells in the convolution kernel for this ConvolutionFilter.
 
Methods inherited from class dlife.vision.filter.Filter
getGUI, getID, isOn, turnOn
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConvolutionFilter

public ConvolutionFilter()
Construct a new ConvolutionFilter.


ConvolutionFilter

public ConvolutionFilter(float[][] kernel,
                         int pixelRow,
                         int pixelCol)
Construct a new ConvolutionFilter using the specified kernel. The elements of kernel are copied into the kernel of this ConvolutionFilter. The element at pixelRow,pixelCol appears at the center of this ConvolutionFilter. Any elements of the provided kernel that do not fit within a 7x7 kernel are ignored. An elements of the 7x7 kernel that are not specified are set to 0.

Parameters:
kernel - the convolution kernel.
pixelRow - the index of the element of kernel that is over the pixel currently being processed.
pixelCol - the index of the element of kernel that is over the pixel currently being processed.
Method Detail

createGUIObject

protected FilterGUI createGUIObject()
Get the GUI for this ConvolutionFilter.

Specified by:
createGUIObject in class Filter
Returns:
a reference to the GUI.

getKernel

public float[][] getKernel()
Get the convolution kernel from this ConvolutionFilter.

Returns:
the kernel as a 7x7 array of double values.

setKernel

public void setKernel(float[][] newKernel,
                      int pixelRow,
                      int pixelCol)
Set the convolution kernel for this ConvolutionFilter.

Parameters:
newKernel - the new kernel.
pixelRow - the index of the element of kernel that is over the pixel currently being processed.
pixelCol - the index of the element of kernel that is over the pixel currently being processed.

setKernelCell

public void setKernelCell(int row,
                          int col,
                          float value)
Set the value of one of the cells in the convolution kernel for this ConvolutionFilter.

Parameters:
row - the row of the cell to set.
col - the column of the cell to set.
value - the value to place in the cell.

filter

public BufferedImage filter(BufferedImage frame)
Apply the convolution kernel to the image in frame and return the result. If the convolution kernel is all 0's then the original image is returned unfiltered.

Specified by:
filter in class Filter
Parameters:
frame - the image to be filtered.
Returns:
the filtered image.

getCode

public String getCode(String name)
Get a String containing the Java code that would be necessary to create this Filter in its current state and add it to the specified VisionClient. The code generated should use the ID of this filter as part of any variable name to ensure that its variables have a unique identifier.

Specified by:
getCode in class Filter
Parameters:
name - the name of the Camera device to which the generated code should add the Filter.
Returns:
the Java code to create this Filter.

main

public static void main(String[] args)
                 throws Exception
Main method that pops up a GUI for the ConvolutionFilter to allow it to be tested.

Parameters:
args - none
Throws:
Exception

dLife Home Page