TrackThemColors
specs
Your computer must be connected to a source of video by an internal socket, third party video digitizing board, USB or web cam. The software uses VFW to access the video device.Note that DV cameras connected via FireWire will not work with this Xtra at this stage.Use the attached application Vidmem.exe to test if your video is properly attached and installed. Also try changing sizes and bit depths to determine the capabilities of your setup.
The Xtra lets you gather information about events in the video, you can track objects according to their brightness, color, or pattern. The Xtra also lets you display the video on stage, Grab a video frame to a cast member and get the color value of pixels in the video.
Opening the Xtra.
Put the Xtra in the "Xtras" folder near your Director application. Then call -
set temp = Xtra "TrackThemColors"
set TrackObj = new(temp)
Have TrackObj declared as a Global variable since you will be referring to it later.
You will probably be doing this in the beginning of the movie so your handler will probably look like this:
on StartMovie
Global TrackObj
if not TrackObj then
set temp = Xtra "TrackThemColors"
set TrackObj = new(temp)
end if
end StartMovie
Closing the Xtra.
When you are done you should close the Xtra by assigning 0 to the variable.
on StopMovie
global TrackObj
set TrackObj = 0
end
Starting the video flow.
In order to access the video stream, the Xtra has to establish a connection with the video capture device and allocate memory. None of the other methods in this Xtra will work without calling the InitVideo() first. You should call the InitVideo() method only once, and be sure to call CleanUp() before you leave.
The InitVideo(TrackObj, Width, height, source) method takes four arguments:
TrackObj - The instance number returned from New().
Width - defines the width in Pixels of the video frame you want.
Height - defines the height in Pixels of the video frame you want.
source - defines the video digitizer that will be used. In most cases use 1. If you have more than one video digitizers installed in your computer . If you specify a number higher than the number of sources in your computer , the Init will fail
The InitVideo () method returns a list with three items [Wdth, Height, bytesPerPixel]:
Width - returns the actual width in pixels of the video frame.
Height - returns the actual height in pixels of the video frame.
bytesPerPixel - returns the bit depth of the video capture (should be either 24 or 32)
Note. On Windows, different video boards and driver have different behavior. the size and bit depth they operate in varies. It is important to check the return value of this method and see that you have actually received the size you have requested. Often you will not.
Stopping the video flow.
Once you are done tracking, you should call the CleanUp() method. Do not attempt to call InitVideo() again without calling the CleanUp() first.
The CleanUp(TrackObj) method takes one argument.
TrackObj - The instance number returned from New().
Opening the Video Settings dialog box
To open the standard Video Settings dialog boxes which allow you to change the video source or change the parameters for your camera, use the VideoSettings() method. This is very useful for changing the brightness, saturation and contrast of the video to create better conditions for the tracking, always use 24 RGB or 32 RGBA
The VideoSettings(TrackObj, whichDialog) takes two arguments.
TrackObj - The instance number returned from New().
whichDialog - 1 opens the format dialog box , 2- source dialog box. 3 - display dialog box. 4- compression dialog box
The VideoSettings () method returns a list with three items [Width, Height, bytesPerPixel]:
Width - returns the actual width in pixels of the video frame.
Height - returns the actual height in pixels of the video frame.
bytesPerPixel - returns the bit depth of the video capture (should be either 24 or 32)
it is important that you examine the return values of this method and see that the changes you requested have actually been applied. Not all dialogs available on some drivers
Viewing the video source on stage
To View the video source on the screen you should call ShowVideo()
The ShowVideo(TrackObj) method takes three arguments.
TrackObj - The instance number returned from New().
sourceRect - A rect defining he area in the frame you want to show, this allows you to select a portion of the frame to be displayed.
destRect - A rect defining the area on stage where you want the video shown. This allows you to stretch the video.
Please note that the ShowVideo() method should be called repeatedly, to get a continuously changing display of the video source. Also note that ShowVideo is "direct to stage", if you wish to have sprites overlaid on top of the video you should use GrabToCast()
Grabbing a video frame to a cast member
You can use the Xtra to grab frames from the video to a cast member. This is useful for creating background images for visualizing the tracking and for other purposes.
To grab a video frame to a cast member use the GrabToCast() method.
The GrabToCast (TrackObj, CastMemberNum) takes two parameters.
TrackObj - The instance number returned from New().
CastMemberNum - The number of the cast member to which you wish to grab the frame. (Have an Image cast member waiting in that location)
Tracking a specific color
To track a specific color of interest in the video source use the TrackColors() method. this method will look for the point in the video most similar to the color you define.
The TrackColors(TrackObj, R,G,B) takes four arguments
TrackObj - The instance number returned from New().
R,G,B - the color values of the color you wish to track (0-255)
The TrackColors() method returns a list containing 4 items [pixelPoint,R,G,B]:
pixelPoint - A point describing the location in the video of the pixel closest in color to the color you requested.
R,G,B, the color values of the pixel most similar to the color you requested. You can use these values to check if the pixel is similar enough for your needs.
Tracking blobs of specific color
To track all blobs of a specific color of interest in the video source use the TrackColos(Blobs) method. this method will return all the areas of the video that are similar to the color you define.
The TrackColos(TrackObj, R,G,B,similarity, padding) takes four arguments
TrackObj - The instance number returned from New().
R,G,B - The color values of the color you wish to track (0-255)
similarity - How similar must a pixel be in order to be added to the blobs.
padding - How close together must two blobs be to be consolidated into a single blob,
The TrackColorBlobs() method returns a list containing any number of rects [blob, blob, blob...]
blob - A rect defining the area of a blob of color.
Tracking change in the video over time
To track changes in the video between frames, use the TrackChangeBlobs() method. This method looks for differences between the current frame and either the previous frame or a reference frame. A typical application for a reference frame would be to make a reference frame of an empty room and then any person or object would be considered change, even if they are not moving. If in the same environment you would track change from frame to frame, only objects that are in motion would show up.
The TrackChangeBlobs(TrackObj, change, fromRefrence, padding) takes four parameter:
TrackObj - The instance number returned from New().
change - A number between 0 - 442 . The Xtra will not consider as change any difference under the specified value.(The smaller the number the more sensitive to change the result).
fromRefrence - 0 -compare this frame to the previous frame. 1 - Compare this frame to a reference frame (use Remember() to define a reference frame, see below) .
padding - How close together must two blobs be to be consolidated into a single blob,
The TrackChangeBlobs method groups neighboring pixels into rects and returns the rects in a list: [blob, blob, blob...] with as many rects as were found in the video.
blob - A rect defining an area of change.
Creating a reference frame for change and pattern tracking
When the patterns you want to track are in the frame call remember() the Xtra then takes a frame of the video and stores it in memory for reference . The Remember(TrackObj) method takes one parameter.
TrackObj - The instance number returned from New().
This is a good time to also grab a frame to a cast member for you to use as a reference by using GrabToCast() (see details above)
Tracking patterns
To follow patterns in the video, use the TrackPattern() method. Sometimes it is useful to track not a particular color , but a particular arrangement of pixels (an eye, ear, car). To do so you must supply the Xtra with a reference frame to compare using the Remember() method, then you need to specify the location ,in the reference frame, of the patterns you are looking for, and finally you should specify an area in which to look for the pattern (it is important to specify a small area to search for both speed and reliability).
The TrackPattern(TrackObj, patRect, searchRect) takes three parameters.
TrackObj - The instance number returned from New().
patRect - A rect defining the area in the reference frame to be used as a pattern.
SearchRect - A rect defining the area that should be searched.
The TrackPattern() method returns a list with 3 items [matchPoint, similarity]
matchPoint - A point holding the coordinates of the point that best matches the center of the pattern.
similarity. A number between 0- 255 defining the confidence the software has in the match it has found (the lower the better). you should monitor this number and only use results that have a low match number.
Getting a hold of the pixels in the video:
Sometimes you might want to get a hold of the actual pixels of the video frame. This is useful for analyzing the colors and for creating any other tracking method that is not supplied by the Xtra. Getting the pixels into director is surprisingly fast, and you should not hesitate to ask for thousands of points at a time. The two methods that send back pixel colors are GetColors() and GetAllColors().
The GetColors() method lets you define the points you are interested in and returns the colors of those points.
The GetColors(TrackObj, points) takes two arguments:
TrackObj - The instance number returned from New().
points - A list of points to be returned [point1, point2, point3...]
The GetColors() method returns a list with the following sequence : [red1, green1, blue1, horizontal1, vertical1, red2, green2, blue2, horizontal2, vertical2...]
red -the red component of the pixel 0-255.
green -the green component of the pixel 0-255.
blue -the blue component of the pixel 0-255.
horizontal - the horizontal location of the pixel - this is an echo of the argument you sent as a point .
vertical - the vertical location of the pixel - this is an echo of the argument you sent as a point .
The GetAllColors lets get a large amount of pixels fast. You define an area and the Xtra returns all the pixels in that area. This method is faster than GetColors() but the results are not formatted as conveniently.
The GetAllColors(TrackObj, area) takes two arguments:
TrackObj - The instance number returned from New().
area - a rect of the area of interest.
The GetAllColors() method returns a list of colors in the following sequence [red1, green1, blue1, red2, green2, blue2, red3, green3, blue3...]
red -the red component of the pixel 0-255.
green -the green component of the pixel 0-255.
blue -the blue component of the pixel 0-255.
using these results is not that straight forward.
Say you want to receive all the pixels from an area at the upper left corner of the video . Say the rect you are defining is rect(0,0,100,100).
Therefore your call will look like this :
put GetAllColors(TrackObj, rect(0,0,100,100)) into TheResult
therefor the Xtra will return a list with 3 X 100 x 100 =30,000 items
Than say you want to see the color of pixel 10 - horizontal , 50 - vertical .
Set TheWidth = 100 -- remember we asked for a 100*100 rect
set PixelH = 10 -- our pixels horizontal location
Set PixelV = 50 -- our pixels vertical location
Set OurPixel = PixelH *3 + PixelV *TheWidth
set red =theresult [OurPixel]
set green =theresult [OurPixel+1]
set blue =theresult [OurPixel+2]
This looks complicated but you only have to figure it out once...
Getting a hold of the brightness of pixels in the video:
Similar to the methods that return pixel colors, but these return the brightness instead . The two methods that send back pixel brightness are GetGrays() and GetAllGrays().
The GetGrays() method lets you define the points you are interested in and returns the colors of those points.
The GetGrays(TrackObj, points) takes two arguments:
TrackObj - The instance number returned from New().
points - A list of points to be returned [point1, point2, point3...]
The GetGrays() method returns a list with the following sequence : [brightness1, horizontal1, vertical1, brightness2, horizontal2, vertical2...]
brightness -the average gray level of the pixel 0-255.
horizontal - the horizontal location of the pixel - this is an echo of the argument you sent as a point .
vertical - the vertical location of the pixel - this is an echo of the argument you sent as a point .
The GetAllGrays lets get a large amount of pixels fast. You define an area and the Xtra returns all the pixels in that area. This method is faster than GetGrays() but the results are not formatted as conveniently.
The GetAllGrays(TrackObj, area) takes two arguments:
TrackObj - The instance number returned from New().
area - a rect of the area of interest.
The GetAllGrays() method returns a list of brightnesses in the following sequence [brightness1,brightness2, brightness3...] the number of items will be the width of the area of interest X the height of the area of interest.
brightness -the average gray level of the pixel 0-255.
Capturing AVI movies
You can capture AVI movies of the video. Use the compression dialog box in VideoSettings() to select the compression and frame rate. The size will be the size of video preview. you can continue to track and preview the video while you capture, but this may reduce the quality of the movie, and some drivers may stop capturing if you try to track while recording
To record an AVI movie use the MovieCaptureStart() method. The MovieCaptureStart(TrackObj, "filePath") method takes 2 parameters:
TrackObj - The instance number returned from New().
filePath - A string defining the full pathname of the AVI file you wish to create (example "C:\myMovie.avi") , if a file of that name exists, it will be overwritten.
the MovieCaptureStart() method returns 1 item -error
error - 1 everything is fine. 0 - couldn't capture.
To stop recording an AVI movie use the MovieCaptureStop() method. the MovieCaptureStop(TrackObj) method takes one prameter :
TrackObj - The instance number returned from New().