[Etoys] [FUN]Track color with camera

karl karl.ramberg at comhem.se
Mon Feb 4 15:31:14 EST 2008


I have used Wiebe Barons WebCamMorph and added some Etoy support to it. 
It is possible to track a color with the camera and a ellipse on the 
screen reflect the movement. I'll see if I can make it into a input 
feature like the eyeToy to the Playstation2. Enjoy

Karl
-------------- next part --------------
'From etoys2.3 of 1 December 2007 [latest update: #1891] on 4 February 2008 at 9:24:40 pm'!
"Change Set:		TrackColorWithCamera
Date:			4 February 2008
Author:			Karl Ramberg

A simple experiment to track colors with the camera. Use viewer to change tracking color and color tolerance.
Based on Wiebe Barons WebCamMorph"!

VideoMorph subclass: #VideoColorTrackerMorph
	instanceVariableNames: 'trackColor ball colorTolerance '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'VideoForSqueak-Morphic'!

!Player methodsFor: '*VideoForSqueak-slot getters/setters' stamp: 'kfr 2/4/2008 16:53'!
getColorTolerance
	"Answer the numeric value contained in my costume"
	^ costume renderedMorph getColorTolerance! !

!Player methodsFor: '*VideoForSqueak-slot getters/setters' stamp: 'kfr 2/4/2008 16:55'!
getTrackColor
	"Set the track color value in my costume"
	^ costume renderedMorph getTrackColor! !

!Player methodsFor: '*VideoForSqueak-slot getters/setters' stamp: 'kfr 2/4/2008 16:55'!
setColorTolerance: aNumber
	"Set the numeric value in my costume"
	^ costume renderedMorph setColorTolerance: aNumber! !

!Player methodsFor: '*VideoForSqueak-slot getters/setters' stamp: 'kfr 2/4/2008 16:55'!
setTrackColor: aColor
	"Set the track color value in my costume"
	^ costume renderedMorph setTrackColor: aColor! !


!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 13:17'!
addOptionalHandlesTo: aHalo box: box 
	super addOptionalHandlesTo: aHalo box: box.
	(aHalo
		addHandleAt: box rightCenter
		color: Color blue
		icon: 'Halo-Paint'
		on: #mouseDown
		send: #getColor
		to: self)
	! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 17:00'!
defaultFloatPrecisionFor: aGetSelector 
	"Answer a number indicating the default float precision to be  
	used in a numeric readout for which the receiver provides the 
	data. Individual morphs can override this. Showing fractional  
	values for readouts of getCursor was in response to an explicit 
	request from ack"
	(#(#getColorTolerance ) includes: aGetSelector)
		ifTrue: [^ 0.01].
	^ super defaultFloatPrecisionFor: aGetSelector.! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 13:17'!
getColor
	Cursor crossHair
		showWhile: [[Sensor redButtonPressed] whileTrue.
			[Sensor redButtonPressed]
				whileFalse: [World doOneCycle].
			trackColor _ (Display colorAt: Sensor cursorPoint)]! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 16:51'!
getColorTolerance
	^colorTolerance! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 16:52'!
getTrackColor
	^trackColor! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 21:18'!
initialize
	super initialize.
	trackColor := Color red.
	colorTolerance _ 0.2.
	ball := EllipseMorph new openInWorld! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 16:51'!
setColorTolerance: aNumber
	colorTolerance _ aNumber! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 16:52'!
setTrackColor: aColor
	trackColor _ aColor! !

!VideoColorTrackerMorph methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 21:18'!
step
	"Track a color in the camera and move the ball acordingly"
	| avx avy div pos |
	super step.
	avx := avy := div := 0.
	1
		to: self scaledForm height
		by: 16
		do: [:y | 1
				to: self scaledForm width
				by: 16
				do: [:x | (trackColor
							diff: (self scaledForm colorAt: x @ y))
							< colorTolerance "tolerance for color difference"
						ifTrue: [self scaledForm colorAt: x @ y put: Color green. "show where trackColor is"
							avx := avx + x.
							avy := avy + y.
							div := div + 1]]].
	div = 0
		ifFalse: [pos := (Display width - (Display width / self scaledForm width * (avx // div)) @ (Display height / self scaledForm height * (avy // div))) rounded.
			ball position: ball position * 2 + pos // 3]! !


!VideoColorTrackerMorph class methodsFor: 'as yet unclassified' stamp: 'kfr 2/4/2008 17:03'!
additionsToViewerCategories
	"Answer a list of (<categoryName> <list of category specs>) pairs that characterize the phrases this kind of morph wishes to add to various Viewer categories."
	^ #(
	
	(video
		(
			
			
			(slot trackColor 'The color to track in camera' Color readWrite	Player getTrackColor Player setTrackColor:)
			(slot colorTolerance 'The tolerance of the track color' Number readWrite Player getColorTolerance Player setColorTolerance:)
			
		)
	)
)
! !

VideoMorph subclass: #VideoColorTrackerMorph
	instanceVariableNames: 'trackColor ball colorTolerance'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'VideoForSqueak-Morphic'!


More information about the etoys-dev mailing list