dLife Home Page

dlife.vision.filter
Class MatchFilter

java.lang.Object
  extended by java.util.Observable
      extended by dlife.vision.filter.Filter
          extended by dlife.vision.filter.MatchFilter
All Implemented Interfaces:
FilterMouseHandler, Observer

public class MatchFilter
extends Filter
implements FilterMouseHandler

Finds (and paints) all pixels that match a specified set of colors.

All pixels with colors that match the criterion expressed by this filter (a range of reds in the image above) are painted using a specified output color (blue in the image above).

The MatchFilter GUI (shown below) allows the user to specify the matching criterion that is to be used.

The MatchFilter GUI provides two distinct mechanisms for specifying the matching criterion. The left side of the GUI allows the user to specify a "Color List". When the "Color List" is being used only those pixels whose color is in the list will be matched. The right side allows the user specify a "Color Range". When the "Color Range" is being used all pixels whose red, green and blue values are within the specified ranges will be matched. More details on the specific GUI elements are provided below.

The MatchFilter GUI contains the following elements:

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

Constructor Summary
MatchFilter()
          Construct a new MatchFilter.
MatchFilter(ArrayList<Color> colorList)
          Construct a new MatchFilter that will match the colors specified in the colorList.
MatchFilter(int redMin, int redMax, int greenMin, int greenMax, int blueMin, int blueMax)
          Construct a new MatchFilter that matches all colors in the specified ranges for each of the color channels.
 
Method Summary
 boolean acceptFilteredVideoEvents()
          A MatchFilter accepts mouse events from the filtered video window.
 boolean acceptRawVideoEvents()
          A MatchFilter accepts mouse events from the raw video window.
 boolean addColor(Color newColor)
          Add the specified color to the list of pixel colors that will be matched by this MatchFilter in List Mode.
 void addColors(Collection<Color> newColors)
          Add a Collection of new colors to this MatchFilter.
 boolean canRedo()
          Indicates whether or not a color addition/removal operation can be redone.
 boolean canUndo()
          Indicates whether or not a color addition/removal operation can be undone.
 void clear()
          Clear the color list and the RGB ranges for this filter.
protected  FilterGUI createGUIObject()
          Create the GUI for this MatchFilter.
 BufferedImage filter(BufferedImage frame)
          Scan the full image and convert any pixels that meet the matching criterion to the output color specified for this Filter.
 String filteredVideoToolTip()
          Return the tool tip to be displayed over the filtered video window.
 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.
 Set<Color> getColorList()
          Get the list of pixel colors that are matched by this MatchFilter.
 int getMaxBlue()
          Get the maximum blue value that will be matched by this MatchFilter if range matching is being used and the blue channel is being used in the matching.
 int getMaxGreen()
          Get the maximum green value that will be matched by this MatchFilter if range matching is being used and the green channel is being used in the matching.
 int getMaxRed()
          Get the maximum red value that will be matched by this MatchFilter if range matching is being used and the red channel is being used in the matching.
 int getMinBlue()
          Get the minimum blue value that will be matched by this MatchFilter if range matching is being used and the blue channel is being used in the matching.
 int getMinGreen()
          Get the minimum green value that will be matched by this MatchFilter if range matching is being used and the green channel is being used in the matching.
 int getMinRed()
          Get the minimum red value that will be matched by this MatchFilter if range matching is being used and the red channel is being used in the matching.
 Color getOutputColor()
          Get the output color of this MatchFilter.
 boolean isListRangeSynchronized()
          Return whether the color list and the RGB ranges are operating in synchronized or independent mode.
static void main(String[] args)
          Main method that pops up a GUI for the MatchFilter to allow it to be tested.
 void matchBlue(boolean matchBlue)
          Set this match filter to use (or not use) the blue channels for matching.
 void matchGreen(boolean matchGreen)
          Set this match filter to use (or not use) the green channels for matching.Note: This setting is only used in Range Mode.
 boolean matchingBlue()
          Determine if this MatchFilter is matching on the blue channel.
 boolean matchingGreen()
          Determine if this MatchFilter is matching on the green channel.
 boolean matchingRed()
          Determine if this MatchFilter is matching on the red channel.
 void matchRed(boolean matchRed)
          Set this match filter to use (or not use) the red channels for matching.
 String rawVideoToolTip()
          Return the tool tip to be displayed over the raw video window.
 void redo()
          Redo the last undone color addition/removal.
 boolean removeColor(Color remColor)
          Remove the specified color from the list of pixel colors that are matched when in List Mode.
 void removeColors(Collection<Color> remColors)
          Remove a Collection of Colors from this MatchFilter.
 void setBlueRange(int min, int max)
          Set the range of blue values that will be matched by this MatchFilter when used in Range Mode.
 void setGreenRange(int min, int max)
          Set the range of green values that will be matched by this MatchFilter when used in Range Mode.
 void setListRangeSynchronized(boolean synched)
          Set whether the color list and the RGB ranges operate in synchronized or independent mode.
 void setOutputColor(Color outColor)
          Set the output color for this MatchFilter.
 void setRedRange(int min, int max)
          Set the range of red values that will be matched by this MatchFilter when used in Range Mode.
 void undo()
          Undo the color addition/removal operation that led to the current state of the filter.
 void update(Observable o, Object arg)
          This filter received a notification from a BufferedImageWithOverlay object indicating that the mouse was clicked or dragged.
 void useRangeMode(boolean rangeMode)
          Indicate if matching will be performed in Range Mode, using the range of RGB values, or in List Mode, using the list of pixel colors.
 boolean usingRangeMode()
          Check if this MatchFilter is using Range Mode or List Mode for color matching.
 
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

MatchFilter

public MatchFilter()
Construct a new MatchFilter. Initially the new filter will not match any colors, is set to use the list of pixel colors for matching, will match on all channels (red, green, blue), and will set matching pixels to yellow. The minimum values for the color channels are set to 255 and the maximum values are set to 0. Thus, in range mode, the filter will not match any colors. These values will be changed by the addition of the first color or by calls to the set range methods. The color list and the RGB ranges are synchronized.


MatchFilter

public MatchFilter(ArrayList<Color> colorList)
Construct a new MatchFilter that will match the colors specified in the colorList. The matching pixels will be set to yellow. The color list and the RGB ranges are synchronized.

Parameters:
colorList - the colors to match.

MatchFilter

public MatchFilter(int redMin,
                   int redMax,
                   int greenMin,
                   int greenMax,
                   int blueMin,
                   int blueMax)
Construct a new MatchFilter that matches all colors in the specified ranges for each of the color channels. The matching pixels will be set to yellow.

Parameters:
redMin - minimum red value to match
redMax - maximum red value to match
greenMin - minimum green value to match
greenMax - maximum green value to match
blueMin - minimum blue value to match
blueMax - maximum blue value to match
Method Detail

setListRangeSynchronized

public void setListRangeSynchronized(boolean synched)
Set whether the color list and the RGB ranges operate in synchronized or independent mode. If the call to this method changes the synchronization from false to true (i.e. the ranges might have been changed) then the current state of the filter is added to the undo list before the change is made.

Parameters:
synched - true to synchronize the list and RGB ranges and false to allow them to be independent.

isListRangeSynchronized

public boolean isListRangeSynchronized()
Return whether the color list and the RGB ranges are operating in synchronized or independent mode.

Returns:
true if the color list and RGB ranges are synchronized and false if they are not.

createGUIObject

protected FilterGUI createGUIObject()
Create the GUI for this MatchFilter.

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

getColorList

public Set<Color> getColorList()
Get the list of pixel colors that are matched by this MatchFilter. The returned list should not be modified. Modifications to the list will affect the pixels that are matched but changes will not be reflected in the GUI.

Returns:
the list of colors matched by this MatchFilter.

addColor

public boolean addColor(Color newColor)
Add the specified color to the list of pixel colors that will be matched by this MatchFilter in List Mode. If the newColor is already in the list of colors then it is not added again. This method also adjusts the range of RGB values that will be matched in Range Mode. If the color is added the state of the MatchFilter prior to the addition of the new color is added to the history list. All observers are notified of the change if the new color is added to the list.

Parameters:
newColor - the color to add to this MatchFilter.
Returns:
true if the color is added, false if it was not.

removeColor

public boolean removeColor(Color remColor)
Remove the specified color from the list of pixel colors that are matched when in List Mode. If the color list and range are synchronized this method also adjusts the range of RGB values. If the color to be removed is in the this then the current state is added to the history before the color is removed.

Parameters:
remColor - the color to be removed from this MatchFilter.
Returns:
true if the color was removed and false if it was not (i.e. it was not in the list).

useRangeMode

public void useRangeMode(boolean rangeMode)
Indicate if matching will be performed in Range Mode, using the range of RGB values, or in List Mode, using the list of pixel colors. If the mode is changed then the observers are notified.

Parameters:
rangeMode - true to use Range Mode for matching and false to use List Mode.

usingRangeMode

public boolean usingRangeMode()
Check if this MatchFilter is using Range Mode or List Mode for color matching.

Returns:
true if Range Mode is being used and false if List Mode is being used.

addColors

public void addColors(Collection<Color> newColors)
Add a Collection of new colors to this MatchFilter. If the color list and the RGB ranges are synchronized the RGB ranges will be updated so that they match all of the colors in the list.

Parameters:
newColors - the Collection of Colors to add.

removeColors

public void removeColors(Collection<Color> remColors)
Remove a Collection of Colors from this MatchFilter. This method has the same effect as removing each color in the list individually using the removeColor method.

Parameters:
remColors - the Collection of colors to remove.
See Also:
removeColor(Color)

setOutputColor

public void setOutputColor(Color outColor)
Set the output color for this MatchFilter. All pixels meeting the matching criterion are set to the outColor. If the output color is changed the observers are notified.

Parameters:
outColor - the color for all matched pixels.

getOutputColor

public Color getOutputColor()
Get the output color of this MatchFilter.

Returns:
the output color.

clear

public void clear()
Clear the color list and the RGB ranges for this filter. The mode (Range/List) and whether or not the list and ranges are synchronized are not affected by the clear operation. If the filter was not already cleared then the cleared state of the filter is added to the history as a new state that can can be undone if desired. The observers are also notified if the filter is cleared.


getMaxRed

public int getMaxRed()
Get the maximum red value that will be matched by this MatchFilter if range matching is being used and the red channel is being used in the matching.

Returns:
the maximum red value that is matched or -1 if no colors have been added and no red range has been set.

getMinRed

public int getMinRed()
Get the minimum red value that will be matched by this MatchFilter if range matching is being used and the red channel is being used in the matching.

Returns:
the minimum red value that is matched.

getMaxGreen

public int getMaxGreen()
Get the maximum green value that will be matched by this MatchFilter if range matching is being used and the green channel is being used in the matching.

Returns:
the maximum green value that is matched.

getMinGreen

public int getMinGreen()
Get the minimum green value that will be matched by this MatchFilter if range matching is being used and the green channel is being used in the matching.

Returns:
the minimum green value that is matched.

getMaxBlue

public int getMaxBlue()
Get the maximum blue value that will be matched by this MatchFilter if range matching is being used and the blue channel is being used in the matching.

Returns:
the maximum blue value that is matched.

getMinBlue

public int getMinBlue()
Get the minimum blue value that will be matched by this MatchFilter if range matching is being used and the blue channel is being used in the matching.

Returns:
the minimum blue value that is matched.

setRedRange

public void setRedRange(int min,
                        int max)
Set the range of red values that will be matched by this MatchFilter when used in Range Mode. If the color list and RGB ranges are synchronized the color list will be modified. Colors no longer matched will be removed from the list and colors that were previously removed but now match will be re-added to the list. The observers will also be notified if the range is changed.

Parameters:
min - the minimum red value to be matched.
max - the maximum red value to be matched.
Throws:
IllegalArgument - exception if min or max is not in the range [0...255] or if min > max.

setGreenRange

public void setGreenRange(int min,
                          int max)
Set the range of green values that will be matched by this MatchFilter when used in Range Mode. If the color list and RGB ranges are synchronized the color list will be modified. Colors no longer matched will be removed from the list and colors that were previously removed but now match will be re-added to the list. The observers will also be notified if the range is changed.

Parameters:
min - the minimum green value to be matched.
max - the maximum green value to be matched.
Throws:
IllegalArgument - exception if min or max is not in the range [0...255] or if min > max.

setBlueRange

public void setBlueRange(int min,
                         int max)
Set the range of blue values that will be matched by this MatchFilter when used in Range Mode. If the color list and RGB ranges are synchronized the color list will be modified. Colors no longer matched will be removed from the list and colors that were previously removed but now match will be re-added to the list. The observers will also be notified if the range is changed.

Parameters:
min - the minimum blue value to be matched.
max - the maximum blue value to be matched.
Throws:
IllegalArgument - exception if min or max is not in the range [0...255] or if min > max.

matchingRed

public boolean matchingRed()
Determine if this MatchFilter is matching on the red channel. Note: This setting is only used in Range Mode. It is ignored in List Mode.

Returns:
true if the red channel values are being used for matching by this filter and false if they are not.

matchRed

public void matchRed(boolean matchRed)
Set this match filter to use (or not use) the red channels for matching. Note: This setting is only used in Range Mode. It is ignored in List Mode. If the color list and RGB ranges are synchronized the color list will be modified. Colors no longer matched will be removed from the list and colors that were previously removed but now match will be re-added to the list. If the color list is changed then the current state will be added to the history before changing the state. The observers will also be notified if the range is changed.

Parameters:
matchRed - true to use the red channels, and false to ignore them.

matchingGreen

public boolean matchingGreen()
Determine if this MatchFilter is matching on the green channel. Note: This setting is only used in Range Mode. It is ignored in List Mode.

Returns:
true if the green channel values are being used for matching by this filter and false if they are not.

matchGreen

public void matchGreen(boolean matchGreen)
Set this match filter to use (or not use) the green channels for matching.Note: This setting is only used in Range Mode. It is ignored in List Mode.

Parameters:
matchGreen - true to use the green channels, and false to ignore them.

matchingBlue

public boolean matchingBlue()
Determine if this MatchFilter is matching on the blue channel. Note: This setting is only used in Range Mode. It is ignored in List Mode.

Returns:
true if the blue channel values are being used for matching by this filter and false if they are not.

matchBlue

public void matchBlue(boolean matchBlue)
Set this match filter to use (or not use) the blue channels for matching. Note: This setting is only used in Range Mode. It is ignored in List Mode.

Parameters:
matchBlue - true to use the blue channels, and false to ignore them.

canUndo

public boolean canUndo()
Indicates whether or not a color addition/removal operation can be undone.

Returns:
true if a color addition/removal operation can currently be undone.

canRedo

public boolean canRedo()
Indicates whether or not a color addition/removal operation can be redone.

Returns:
true if a color addition/removal operation can currently be redone.

undo

public void undo()
Undo the color addition/removal operation that led to the current state of the filter.

Throws:
IllegalStateException - if the operation cannot be undone.
See Also:
canUndo()

redo

public void redo()
Redo the last undone color addition/removal.

Throws:
IllegalStateException - if the operation cannot be undone.
See Also:
canRedo()

filter

public BufferedImage filter(BufferedImage frame)
Scan the full image and convert any pixels that meet the matching criterion to the output color specified for this Filter.

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

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.

acceptFilteredVideoEvents

public boolean acceptFilteredVideoEvents()
A MatchFilter accepts mouse events from the filtered video window.

Specified by:
acceptFilteredVideoEvents in interface FilterMouseHandler
Returns:
true

acceptRawVideoEvents

public boolean acceptRawVideoEvents()
A MatchFilter accepts mouse events from the raw video window.

Specified by:
acceptRawVideoEvents in interface FilterMouseHandler
Returns:
true

filteredVideoToolTip

public String filteredVideoToolTip()
Return the tool tip to be displayed over the filtered video window.

Specified by:
filteredVideoToolTip in interface FilterMouseHandler
Returns:
the tool tip.

rawVideoToolTip

public String rawVideoToolTip()
Return the tool tip to be displayed over the raw video window.

Specified by:
rawVideoToolTip in interface FilterMouseHandler
Returns:
the tool tip.

update

public void update(Observable o,
                   Object arg)
This filter received a notification from a BufferedImageWithOverlay object indicating that the mouse was clicked or dragged. Process that notification by getting the colors associated with the click/drag and adding them to this filter.

Specified by:
update in interface Observer
Parameters:
o - the observable object that notified this filter.
arg - the argument from the observable.

main

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

Parameters:
args - none
Throws:
InterruptedException

dLife Home Page