AS3 WebCam LevelsCorrection (updated)

The labThe imageprocessing-lab yields fruits. What you can see here is automatic levelscorrection applied on a 160×120 image. The fps decrease a little bit because I have to calculate the histogram three times. Two times for the histogram images and one time for the levels filter. So there would be another speed boost if the histograms are not shown.

My webcam is doing the auto-levels correction already. This is shit, because it is hard to correct corrected levels. And you will not see a big difference. Maybe your webcam is not doing this and you will achieve the grateness of this filter.

After a lot of tests I have got my favorite image format now. It is fast and easy to create new filters. Basically the image consists of an array which holds a BitmapData for each channel. The BrightnessCorrection filter works for example like this:
- Create a look-up table for new brightness values. This is for every channel the same.
- Apply paletteMap to every channel of the image

Easy, huh? And fast. paletteMap is one of the fastest native functions. It is even faster to use BitmapData.paletteMap instead of BitmapData.threshold. The basic function to binarize a picture is done by applying paletteMap with a palette of 0 and 1. This is faster then using threshold!

One last thing. The histogram is way to slow in my eyes. I am still searching for a solution to speed this up. Currently I read the value of each pixel using getPixel. This makes width * height iterations. Then I tested BitmapData.threshold with the == operator. Of course only 256 (non native) iterations but totally 256 * width * height. It was very slow. Another test was using a combination of BitmapData.fillRect and BitmapData.compare. Also slower than reading every pixel.

Maybe another good solution? The comments function is working. I am waiting for your suggestions :-)

If you can not see any difference in the two pictures. Try holding your hand in front of the webcam. This will result in very dark input and you see the levels-correction working.

Update:
Compiled using Flex Builder 2.0 Beta 3 which Adobe released a few hours ago. Now there is a big performance boost in the whole ImageProcessing. I do not really know where it comes from. setPixel seems to be slower than in Beta 2 but since I do not use setPixel at all in the whole package everything is faster. I think that some native functions (also filters) work faster.

The example is doing the following:
- Webcam whith (some sort of) double framebuffer
- Convert BitmapData to Image class with grayscale conversion (using BT709 algorithm)
- Render original Image
- Calculate and draw(!) original histogram
- Apply automatic levels correction
- Apply sharpening
- Render filtered image
- Calculate and draw histogram of new image

This runs at ~20fps for me in the browser. Disabling both unneeded histograms increases fps in standalone player to ~50.

5 Responses to “AS3 WebCam LevelsCorrection (updated)”


  • Great stuff thank you Joa!

  • Hi Joa!

    This is great. And totally unique.

    Will it be possible to make it into a Flash AS3 class, to take level-corrected still shots from users’ webcams?

    Many users have web cams that can be put to good use, but my experience is most people surf the web from low-light rooms :-)

    If you will release some of the code, I could work on it.

  • How are you drawing out your histograms?

    I just learned how to create a histogram and I have a video with four histograms running and it looks juts fine.

    http://www.as3apex.com/as3/having-fun-with-runtime-color-histograms-and-video/

  • The LevelsCorrection class can take 4 arguments in its constructor:

    automatic : Boolean
    from : Array
    gamma : Array
    to : Array

    The reference says the Arrays have an element for each channel – but when I pass in an array for gamma such as:

    new LevelsCorrection( false, null, [ 4, 4, 4, 4 ] )
    or
    new LevelsCorrection( false, null, [ 4, 4, 4 ] )

    I get the following:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.

    I’m assuming each channel means ARGB or RGB?

  • Are you planning to use the library in an agency project?

Leave a Reply