Computer Vision 1 -- Webcam -- follow

wc_follow_hand.jpg
smallarrows.GIF

You will grab a color from the screen with the mouse. The flower will folow it.


Download Josh Nimoy's Webcam xtra:   http://webcamxtra.sourceforge.net/


Lingo script to get an object to follow your hand.

Click here to download the sample file


Make 2 images in photoshop:
Name 1 of them grabcam.tif 320x240, sprite 1
The other should be a small image like a flower 64x64 pixels, sprite 2.


Button behavior to bring up webcam settings:
Make a button attach this script to it as a behavior.
global webcamxtra
on mouseup
  webcamxtra.settings()
end


Frame 1 script
Apply this script below to frame 1.

global webcamxtra
property w,h
property selectedcolor

on beginsprite
  w = 320
  h = 240
  webcamxtra = new (xtra "webcamxtra") --call up xtra
  webcamxtra.start(w,h)  --initiate webcam and set size
  selectedcolor = rgb(255,0,0) --default selected color
end

on
mouseDown
  --select color around mouse
  selectedcolor = webcamxtra.average(the mouseH-5,the mouseV-5,the mouseH+5,the mouseV+5)
  --fill track color square with selected color
  (the stage).image.fill(310,245,320,255,selectedcolor)
end

on
exitframe
  --tell it what color to track, grabbing from sprite 8
  webcamxtra.trackColor(selectedcolor.red,selectedcolor.green,selectedcolor.blue,60)
 
  --process one frame
  webcamxtra.update()
 
  --draw the video image on screen
  img = webcamxtra.cameraImage()
  member("grabcam").image.copypixels(img, rect(0,0,w,h),rect(0,0,w,h))
 
  --flower follows color
  gpix = webcamxtra.globCenters()
  if gpix.count > 0 then sprite(2).loc = gpix[1]
 
  go to the frame --loop on frame 1
end

on
endsprite
  webcamxtra.stop() --must do this
end