Converting a color form to black and white (was: RE: Using video input in Squeak)

Dean_Swan at Mitel.COM Dean_Swan at Mitel.COM
Tue Jul 20 23:25:11 UTC 2004


Rob,

I'm not sure a simple color map will do the trick.  What you want is the
Y channel of a 32 bit bitmap (i.e. RGB).  Class JpegReadWriter implements 
a
JPEG decoder that includes the necessary color space conversions, but from
YCrCb to RGB.  You need from RGB to YCrCb, so you'd have to implement the
inverse of what is there already.  Class JpegReadWriter2 at some point
implements what you need, but all the heavy lifting is done by plugin
primitives which are mostly in C.

There are many colorspace standards (YUV, YIQ, YCrCb, HSV, etc..) and they
can be subject to some interpretation.  I've used the recommended RGB
coefficients from CCIR 601-1, which is the relevant uncompressed digital
video standard that applies to DVDs.

I think the most straightforward way to get what you want is to implement
a method to convert a 16 bit or 32 bit form to an 8 bit grayscale form -
something like the example below.  BTW, I have tested this method and it
does work.  It could certainly be optimized for performance as there are
a lot of color objects created, but it should work with any source Form
in Squeak.

And here's some test code for the method given below:

        | a b |
        a _ Form fromDisplay: ((0 @ 0) extent: (400 @ 300)).
        b _ a yComponent.
        Display restoreAfter:
        [
        b displayScaledOn: Display
        ].

You can paste this in a workspace and DoIt.

                -Dean Swan


Form>>yComponent

        "Returns an 8 bit form representing the Y component of self"

| sourceHeight sourceWidth sourceDepth sourcePeeker sourcePixelValue 
sourcePixelColor
  grayscaleForm grayscalePoker grayscaleDepth grayscalePixelColor yValue |

sourceHeight _ self height.
sourceWidth _ self width.
sourceDepth _ self depth.

grayscaleDepth _ 8.
grayscaleForm _ Form extent: (sourceWidth @ sourceHeight) depth: 
grayscaleDepth.

sourcePeeker _ BitBlt current bitPeekerFromForm: self.
grayscalePoker _ BitBlt current bitPokerToForm: grayscaleForm.

0 to: (sourceHeight - 1) do:
        [ :y |
                0 to: (sourceWidth - 1) do:
                        [ :x |
                                sourcePixelValue _ sourcePeeker pixelAt: ( 
x @ y).
                                sourcePixelColor _ Color 
colorFromPixelValue: sourcePixelValue depth: sourceDepth.

                                yValue _ 0.299 * (sourcePixelColor red) + 
0.587 * (sourcePixelColor green) + 0.114 * (sourcePixelColor blue).

                                grayscalePixelColor _ Color gray: yValue.

                                grayscalePoker pixelAt: (x @ y) put: 
(grayscaleForm pixelValueFor: grayscalePixelColor).
                        ]
        ].

 ^grayscaleForm.






"Rob Hensley" <HensleyRj at Rockhurst.edu>
Sent by: squeak-dev-bounces at lists.squeakfoundation.org
07/20/04 12:26 PM
Please respond to The general-purpose Squeak developers list

 
        To:     "The general-purpose Squeak developers list" 
<squeak-dev at lists.squeakfoundation.org>
        cc: 
        Subject:        RE: Using video input in Squeak


Ultimately, what I want to do is a) convert the color image to B/W, b)
set a threshold of what is "white" and what is "black" (that is,
everything over a certain value -- let's say 123 -- will be considered
white, and everything below will be considered black), and c) construct
a Morph object out of the "black" part of the image. This Morph object
will interact w/ the virtual balls (which are already done). 

-----Original Message-----
From: squeak-dev-bounces at lists.squeakfoundation.org
[mailto:squeak-dev-bounces at lists.squeakfoundation.org] On Behalf Of Bert
Freudenberg
Sent: Friday, July 16, 2004 6:31 PM
To: The general-purpose Squeak developers list
Subject: Re: Using video input in Squeak

Use BitBlt with a colorMap (see BitBlt's class comment).

- Bert -

Am 16.07.2004 um 20:31 schrieb Rob Hensley:

> I'm not sure who has been following my story, but I started this 
> project
> using a Mac, and then switched to a PC running Windows (at the urging 
> of
> my mentor).
>
> I am using the DVideoShowDecoderPlugin. I have tested it, and it
works.
> My next step is separating the live actor's shadow part of the video
> image from the white background: I need to convert the Form from color
> to black and white. Once I have the shadow only, I can make a Morph
out
> of it and instruct the virtual balls to react to it.
>
> Does anyone have any tips on the easiest way to convert the color
image
> to B/W?
>
> -----Original Message-----
> From: squeak-dev-bounces at lists.squeakfoundation.org
> [mailto:squeak-dev-bounces at lists.squeakfoundation.org] On Behalf Of 
> Bert
> Freudenberg
> Sent: Wednesday, July 14, 2004 6:55 AM
> To: The general-purpose Squeak developers list
> Subject: Re: Using video input in Squeak
>
> You would have to write a plugin similar to Diego's V4LPlugin that
uses
> Quicktime to read from the camera. To my knowledge, there is no such
> plugin, yet. It might be a good idea to use the same primitives to
> minimize platform-dependencies on the image side.
>
> - Bert -
>
> Am 12.07.2004 um 14:42 schrieb Rob Hensley:
>
>> Ah, I'm using a Mac for this project...
>>
>> Any ideas?
>>
>>               -----Original Message-----
>>               From: squeak-dev-bounces at lists.squeakfoundation.org on 
behalf of
> Ned
>> Konz
>>               Sent: Wed 7/7/2004 3:47 PM
>>               To: The general-purpose Squeak developers list
>>               Cc:
>>               Subject: Re: Using video input in Squeak
>> 
>> 
>>
>>               On Wednesday 07 July 2004 1:30 pm, Rob Hensley wrote:
>>               > That's exactly where I got the idea for the project!
>>               >
>>               > Do you have any suggestions as to where to start 
looking for
>> interface
>>               > code/demos?
>> 
>>               Diego Gomez Deck has been working on an interface from
> Video4Linux to
>> Squeak.
>> 
>>               Look at:
>>               http://minnow.cc.gatech.edu/squeak/3765
>> 
>>               Also:
>>               http://minnow.cc.gatech.edu/squeak/1853
>>               http://minnow.cc.gatech.edu/squeak/2411
>>               http://www.is.titech.ac.jp/~ohshima/squeak/DShowVideo/
>> 
>>               --
>>               Ned Konz
>>               http://bike-nomad.com/squeak/





-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20040720/88780b47/attachment.htm


More information about the Squeak-dev mailing list