Archive for April, 2006

AS3 Imageprocessing library

Various filters

Continuing with my work on some “research” releated to webcam and object recognitionI got a little bit addicted to imageprocessing. You can see some of a lot of working filters here. The latest beauty is the automatic levels correction by stretching the histogram. The auto-levels takes 250ms for a 640×480 px image which is not bad. Most filters are look-up based.

Thr problem is that I can never decide what seems to be the best and fastest method. An image is of course built from a BitmapData input. First I started with a BitmapData for every color-channel (4 bitmaps for a RGBA image). I am currently using an Array for every channel, because looping through the whole image is faster and I get rid of the get-/setPixel method.

But some new ideas using native methods could speed up everything again. I am not sure, but palletteMap has the potential to make all look-up-table based filters incredible fast. I will give it a try…tomorrow maybe.

AS3 PerlinSound - color journey

PerlinSound This comes out when combining particles, a forcefield, SoundMixer.computeSpectrum and some eye-candy. The forcefield is generated by perlinNoise so the particles refer to Andres sweet lightning particles. The idea of using a forcefield is still the same.

There is a fixed amount of 5000 particles in this scene. I want to add here that even 10.000 particles are possible but the creation of the stripes you see is interrupted by the number of particles that are used for the spectrum. The stripes are also built using the perlinNoise with a simple trick. If you build a perlin noise on a 200*200px image and scale it back to 400*400px you have nice rasters. I use a transformation from 100*100px to 400*400px in this case.

The color-change is done using a simple ColorTransform and also changing the additive color behaviour of new particles. Another thing is the blendmode of colors. I really like to add colors together. If you combine this with a BlurFilter you have a really nice glow effect.

In the background you can hear a very nice deep house track from MC Carlos.

flIRC and Flex 2 Beta2

Updated flIRC to work with Flex 2 Beta 2 again.
Enjoy.

Throw is (a little bit) slow!

Yesterday I read some oldschool articles about heavy optimization techniques from the demo-scene. One thing that has been mentioned was the use of throw. Throw slows your code down since there is an overhead for every function.

I do not want to go into detail but you can imagine that the AVM2 has to know that a function can throw. The problem is that I can not look into something like the FLASM instructions (which was possible for AS2). But the tests tell me that throw is slow! But not that slow.
It is about ~70ms at 10^6 iterations.

[as] var t0: Number;
var t1: Number;
var stop: Boolean;
stop = ( t0 && t1 );

var f0: Function = function( v: int ): int
{
if ( stop )
return 0;
return v++;
}

var f1: Function = function( v: int ): int
{
if ( stop )
throw new Error(’The error is that throw is slow.’);
return v++;
}

var i: int;
var j: int;

for ( var k: int = 0; k < 3; k++ )
{

i = 0;
j = 0;
t0 = (new Date()).getTime();
for ( i = 0; i < 1000000; i++ )
j = f0( j );
t1 = (new Date()).getTime();
trace( ‘No throw: ‘ + ( t1 - t0 ) + ‘ms’ );

i = 0;
j = 0;
t0 = (new Date()).getTime();
for ( i = 0; i < 1000000; i++ )
j = f1( j );
t1 = (new Date()).getTime();
trace( ‘With throw: ‘ + ( t1 - t0 ) + ‘ms’ );
}[/as]

So what I get from this is that throw costs only a little bit of time. If you want to optimize your code hardcore you have to kick out all the throw statements. You would also not use all the possible OOP features.

I will continue using throw. Sometimes a throw is not important but in some cases it is very handy. The speed gain without throw is not that much.

AS3 Retinex implementation

Retinex in actionHere an implementation of the holy Retinex-filter. The idea behind it is splitting up the digital image into two parts. The luminance and the reflection image. With both parts you can for example increase the brightness without loosing color information. The retinex filter creates images that look more like the reality which is very interesting if you use a camera.

Imagine this in real-time on a webcam. All your problems with the scene appereance would be kicked away since you could level all images to the same brightness. EyeToy-like games could be played at night without even turning the light on.

This is a conversion from the MSCR (multiscale retinex with color restoration) implementation in gimp-2.3.7.
Have a look at the example picture and /as3/ip/filter/rgb/Retinex.as.

Steam Half-Life PHP util

Here a free-to-use Half-Life server util that allows you to read players, rules and server-info from a server using the “new” Steam protocol. It was the request of a friend and I had my fun with PHP since I really hate it but this is the first time I used PHP5 (which seems to be a little bit more interesting).

The protocol is based on UDP. So there can be packages in wrong order or even never reaching the target. As you can see the class should handle all possible problems with UDP. Features are for example merging of splitted packages and all that stuff.

Simple Auto-Levels

Just a quick note on auto-levels. You may know about that function in various graphics software like Photoshop. There was a simple question about how to implement auto-levels in Flash (especially AS3).

Therefore I googled around searching for an algorithm that fits my needs but I had no success. There was also no article in my beloved image-processign bible. What I found out is that the levels is about brightness, contrast and midtones. I am just using brightness and contrast here. You can find the whole source in /as3/ip/img/Image.as in my file-browser.

Now about the auto-levels with are not as good as the Photoshop results but it is a fast way to do it in Flash.

[as]
//– create an image
var img: Image = new Image( width, height );
img.setType( Image.IMAGE_GRAY );
img.loadBitmapData( bmp );

//– grayscale auto-levels
var a: int = img.getAvgValue();
img.adjustBrightness( Math.exp( (0×80 - a) / 0×80 ) * 0×10 );
img.adjustContrast( Math.exp( (0×80 - a) / 0×80 ) );
[/as]

What’s next?

After 13 years I had my last day in school on last friday. It was a very sentimental moment when I stood with all my friends together and celebrated our last day at school. A strange feeling when there is nothing to do instead of writing the exams and waiting for the next term to begin studying.

As I wrote some weeks ago I also did some extra exams in object recognition. I think the paper I had to wrote is very nice but I am not sure if I may publish it here since my teacher and professor have not looked over it. The whole thing is about object recognition with motion detection. I used a lot of filters like Dilation, Erosion, Closing, Convolution, Retinex and others. I also did some tests in Flash. You can find them in the directory /as3/ip/. But I have to say that most of them are not very up to date.
I also finished a project for school which was about motion-detection and response. Therefore I developed an eyetoy-like game. It is fun to play around with Flash and a webcam.
I will have to update the project for Flex Builder 2 Beta 2 to release it here. But now I have holidays and time to learn for my final exams. And of course enough time to start with a lot of funny experiments :o)




Close
E-mail It