edu.cmu.ri.createlab.video
Class VideoHelper

java.lang.Object
  extended by edu.cmu.ri.createlab.video.VideoHelper

public final class VideoHelper
extends Object

Author:
Tom Lauwers (tlauwers@birdbraintechnologies.com)

Constructor Summary
VideoHelper()
           
 
Method Summary
 int[] blobCalibration()
          Method for getting back calibration values for the blob detector method.
 int[] blobDetector(int[] calibrationVals, int sensitivity)
          The blob detector detects all of the pixels that are within a certain range of the CalibrationVals, where the width of the range is determined by the value sensitivity.
 void closeVideo()
          Closes the video stream.
 void closeVideoScreen()
          Closes the video window
 void drawCircle(int radius, int centerX, int centerY)
          Draws a circle on the camera image.
 void drawNothing()
          Call this if you want to no longer display a polygon on the camera image.
 void drawPolygon(Polygon poly)
          Draws a generic polygon into the image.
 void drawRectangle(int minX, int minY, int maxX, int maxY)
          Draws a rectangle in the video window.
 Color getAreaColor(int minX, int minY, int maxX, int maxY)
          Gets the AVERAGE Color value of the pixels in a portion of the image.
 int[] getAreaRGBValues(int minX, int minY, int maxX, int maxY)
          Gets the AVERAGE RGB values of the pixels in a portion of the image.
 BufferedImage getImage()
          Returns as a BufferedImage object the most recent image retrieved from the camera
 int getImageHeight()
          Get the image height
 int getImageWidth()
          Get the image width
 Color getPixelColor(int x, int y)
          Gets the color of a given pixel as a Java Color object at the coordinate specified by x,y
 int[] getPixelRGBValues(int x, int y)
          Gets the Red, Green, and Blue values of the pixel at the coordinate specified by x,y
 void initVideo()
          Initializes and starts a new video stream, which can be used to track objects, react to colors placed in the field of view of the camera, and to bring up a window of what the camera is seeing.
 void setFillPolygon(boolean setting)
          Sets whether the polygon is filled in or an outline.
 void setPolygonColor(Color polyColor)
          Sets the color of any polygon, rectangle, or circle drawn into the image.
 void showVideoScreen(String name)
          Displays a window that shows the camera image.
 void updateVideoScreen()
          Updates the image in the video window.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VideoHelper

public VideoHelper()
Method Detail

initVideo

public void initVideo()
Initializes and starts a new video stream, which can be used to track objects, react to colors placed in the field of view of the camera, and to bring up a window of what the camera is seeing. Note that this does NOT automatically bring up the window showing the camera image - call showVideoScreen() to show that.


closeVideo

public void closeVideo()
Closes the video stream.


getImage

public BufferedImage getImage()
Returns as a BufferedImage object the most recent image retrieved from the camera

Returns:
The image data

getImageHeight

public int getImageHeight()
Get the image height

Returns:
image height as an int

getImageWidth

public int getImageWidth()
Get the image width

Returns:
image width as an int

getPixelRGBValues

public int[] getPixelRGBValues(int x,
                               int y)
Gets the Red, Green, and Blue values of the pixel at the coordinate specified by x,y

Parameters:
x - The row of the pixel
y - The column of the pixel
Returns:
An 3-int array of the red, green, and blue values of the pixel. Values are 0 to 255 and represent the intensity of color.

getPixelColor

public Color getPixelColor(int x,
                           int y)
Gets the color of a given pixel as a Java Color object at the coordinate specified by x,y

Parameters:
x - The row of the pixel
y - The column of the pixel
Returns:
A Color object representing the color of the pixel

getAreaRGBValues

public int[] getAreaRGBValues(int minX,
                              int minY,
                              int maxX,
                              int maxY)
Gets the AVERAGE RGB values of the pixels in a portion of the image. The user specifies the minimum X,Y and the maximum X,Y coordinates and the method calculates the average values in the rectangle described by those coordinates.

Parameters:
minX - minimum X coordinate of rectangle
minY - minimum Y coordinate of rectangle
maxX - maximum X coordinate of rectangle
maxY - maximum Y coordinate of rectangle
Returns:
a 3 element array holding the red, green, and blue intensities of the area

getAreaColor

public Color getAreaColor(int minX,
                          int minY,
                          int maxX,
                          int maxY)
Gets the AVERAGE Color value of the pixels in a portion of the image. The user specifies the minimum X,Y and the maximum X,Y coordinates and the method calculates the average color in the rectangle described by those coordinates.

Parameters:
minX - minimum X coordinate of rectangle
minY - minimum Y coordinate of rectangle
maxX - maximum X coordinate of rectangle
maxY - maximum Y coordinate of rectangle
Returns:
a Color object holding the average color of the area

blobCalibration

public int[] blobCalibration()
Method for getting back calibration values for the blob detector method. Draws a rectangle on the screen and holds it there for five seconds. To calibrate on an object, make sure that it is entirely within the rectangle. Calibration occurs at the end of the method, so it is only necessary to have the object positioned properly at the end of the five seconds.

Returns:
a 3 element array of red, green, and blue color values of the blob to be tracked

blobDetector

public int[] blobDetector(int[] calibrationVals,
                          int sensitivity)
The blob detector detects all of the pixels that are within a certain range of the CalibrationVals, where the width of the range is determined by the value sensitivity. What the algorithm does is: 1. For every pixel, it compares the RGB values of that pixel to the calibration values; if the pixel's R, G, AND B values are within the calibration values +/- the sensitivity, then the pixel is counted. 2. Take the average of all the counted pixels' coordinates to get the center of the blob. 3. Finds the edges of the blob by traversing the rows and columns of the image and setting an edge when 1/10 of the total counted pixels have been seen. Traversal is from top to bottom and left to right to find the top and left edges respectively, and from bottom to top and right to left to find the bottom and right edges. The detector returns an array of six ints - elements 0 and 1 are the x,y coordinates of the center of the blob, elements 2 and 3 are the minimum and maximum x coordinates, while elements 4 and 5 are the min and max y coordinates.

Parameters:
calibrationVals - An array containing the RGB values of the pixel to look for
sensitivity - The sensitivity of the detector - higher values lead to more noise, while low values might not pick up very much of the object being tracked. A suggested value for a brightly colored object is 10.
Returns:
An array containing the center, top left, and bottom right x,y coordinates of the blob.

showVideoScreen

public void showVideoScreen(String name)
Displays a window that shows the camera image. Note that the image must be updated through program calls to the method updateVideoScreen().

Parameters:
name - the name to give the window

updateVideoScreen

public void updateVideoScreen()
Updates the image in the video window. Note that this method also updates the image data in the same way that getImage() does, so it is not necessary to call both getImage and updateVideoScreen. Rather, call getImage() if your program does not display a video window, and use updateVideoScreen() if it does display a window.


closeVideoScreen

public void closeVideoScreen()
Closes the video window


drawRectangle

public void drawRectangle(int minX,
                          int minY,
                          int maxX,
                          int maxY)
Draws a rectangle in the video window. Useful for displaying where the blob tracker thinks a blob is located. Once called, the rectangle will be persistent across all calls of updateVideoScreen. To remove it, call drawNothing. To change its color, call setPolygonColor. To change whether the rectangle is an outline or filled in, call setFillPolygon.

Parameters:
minX - minimum X coordinate of rectangle
minY - maximum X coordinate of rectangle
maxX - minimum Y coordinate of rectangle
maxY - maximum Y coordinate of rectangle

drawCircle

public void drawCircle(int radius,
                       int centerX,
                       int centerY)
Draws a circle on the camera image. Useful for displaying where the blob tracker thinks a blob is located. Once called, the circle will be persistent across all calls of updateVideoScreen. To remove it, call drawNothing. To change its color, call setPolygonColor. To change whether the rectangle is an outline or filled in, call setFillPolygon.

Parameters:
radius - The radius of the circle in pixels
centerX - The X coordinate of the center of the circle
centerY - The Y coordinate of the center of the circle

drawNothing

public void drawNothing()
Call this if you want to no longer display a polygon on the camera image.


setPolygonColor

public void setPolygonColor(Color polyColor)
Sets the color of any polygon, rectangle, or circle drawn into the image.

Parameters:
polyColor - The color to set the polygon to.

setFillPolygon

public void setFillPolygon(boolean setting)
Sets whether the polygon is filled in or an outline.

Parameters:
setting - true sets the polygon to be filled in, false sets it to outline

drawPolygon

public void drawPolygon(Polygon poly)
Draws a generic polygon into the image. Note that once called, the polygon will be persistent across all calls of updateVideo. To remove it, call drawNothing. To change its color, call setPolygonColor. To change whether the rectangle is an outline or filled in, call setFillPolygon.

Parameters:
poly - The polygon object to draw into the image