Patrick Bader's Blog a blog about software development

21May/118

ScreenBrightness V2.1

I updated my Screen Brightness Tool to version 2.1. You can now specify a command-line parameter to set the brightness directly. When a parameter is specified the tool will be closed immediately after setting the brightness. Any previously running instance of the tool will not be closed.

You can download the new version here or from the projects page.

Have fun with the new feature! Feedback is welcome.

Filed under: Uncategorized 8 Comments
9Dec/100

foreach and lambdas in C#

I recently encountered a bug in a C# application I was writing and it took me quite a while to fix. The situation is as follows: I had an array with some kind of data I wanted to be processed by a thread pool. So my first approach was iterating over the array with a foreach loop and passing a lambda to the thread pool as argument. The code looked like this:

1
2
3
4
5
6
7
8
9
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
	ThreadPool.QueueUserWorkItem((unused) => {
		System.Console.WriteLine(item);
	});
}

so, what's wrong with this code? Without the thread pool, nothing at all, it runs just fine, but with the thread pool the following text was written to the console:

5
5
5
5
5

When I saw the output, I was like: "what the hell is going wrong here?". The problem with the code above is: lambdas in C# capture their variables by-reference. For each iteration of the loop, a new value will be assigned to the item variable, an since this variable is captured by the lambda by-reference, all WorkItems in the thread pool will be influenced by the assignment of the loop. In my case the loop run completely, before the first WorkItem was executed. So the value of item was always the value which was last assigned by the loop.
A quick fix for this was assigning the loop variable to a local variable in the for loop:

1
2
3
4
5
6
7
8
9
10
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
        int localItem = item;
	ThreadPool.QueueUserWorkItem((unused) => {
		System.Console.WriteLine(localItem);
	});
}

so, each lambda references its own local variable. Another solution is to pass the item explicitly to each WorkItem:

1
2
3
4
5
6
7
8
9
10
object someState;
int items[] = {1, 2, 3, 4, 5};
 
foreach(int item in items)
{
	ThreadPool.QueueUserWorkItem((localItem) => {
		System.Console.WriteLine(localItem);
	},
        localItem);
}

The disadvantage here is: You have to make the variable explicitly available in the lambda, by passing it as a parameter.

Side note:
The upcoming C++0x release will also contain lambda expressions but it will allow you to specify whether a variable shall be bound by value or reference.

Filed under: C#, Uncategorized No Comments
12Sep/103

Master Thesis Presentation [2nd Update]

This time a posting in German since the presentation will be held in German:
Am Dienstag den 14.09.2010 werde ich eine Präsentation über meine Master Thesis mit dem Titel: "GPU-unterstützte Bildverarbeitung und Bilderkennung
im Kontext einer Multi-Touch-Anwendung" halten.
Der Vortrag findet an der Hochschule der Medien in Stuttgart statt.
Beginn ist 10:30 Uhr und der Vortrag wird ca. 45 Minuten dauern.
[Update]Das ganze findet in Raum 135 statt.[/Update]
Interessierte sind herzlich eingeladen teilzunehmen.

[2. Update]Der Vortrag ist als Videostream hier abrufbar.[/2. Update]

7Sep/102

it’s about time…

I finally finished my Master's Thesis about GPU based image processing in the context of a multi-touch application. As I wrote in older posts I have built a prototype of an LCD based multi-touch screen. The PlayStation Eye camera is used to track blobs and fiducials using IR light and the images from the cam are processed on the GPU to extract positions, IDs, etc. For the implementation which will possibly be published in a later post, I used OpenCL, which allows programming on various heterogenous hardware. The visualization is done using solely OpenGL 3.2 Core Profile.

The thesis in in german and can be downloaded here. Feel free to read and comment it.

29Jul/100

ScreenBrightness v2

I finally updated my ScreenBrightness tool to fix some crashes when running at startup.
The new version can be found in the Projects page.
Feel free to give some feedback!

Filed under: Uncategorized No Comments
2Dec/090

Bandpass Filtering and LCD Testing

As promised here are some results of a test session with the LCD. I ordered a bandpass filter for 850 nm near-IR light for the PS3 camera a while ago at ebay. The filter blocks all light except in a small range around 850 nm wavelength.

I am trying to build a multi-touch screen with the DSI technique, explained here but instead of using a projector and some special projection surface I will be using the recently disassembled LCD.

So why not using a projector? The answer is quite simple: I don't own one 😉 But there are some other facts that lead me to try it with an LCD:

  • Projectors need quite a lot of distance from the projection surface but I am not intending to build a multi-touch table.
  • Even HD projectors which are quite expensive do have very limited resolution in relation to the size of the projection surface. So small text can hardly be read on such a table.
  • I wanted to build something smaller so one person is able to reach the whole screen from one location.

There are basically two issues with the LCD approach:

  1. The camera is located behind the LC-panel thus has to track blobs through the panel which normally is covered by the backlight (with a white opaque layer) and some metal. This directly leads to the second problem.
  2. The backlight has to be replaced with one that is itself transparent.

My first approach was to use Plexiglass Endlighten for both backlight and IR illumination but I was not able to detect any blobs with the Plexiglass behind the LC-panel. So I bought another Plexiglass Endlighten which is located in front of the LC-panel for blob tracking purposes only.

I took some picture to test whether IR light is blocked on dark regions on the LCD image as visible light is. The results were quite surprising, the visibility of IR light is not affected by the picture shown by the LC-panel at all as can be seen below.

Display with normal Backlight on

Display with backlight on.

Backlight and IR lighting on

Backlight and IR lighting on

Only the IR lighting is on.

Only IR lighting on.

Both backlight and IR lighting on but with bandpass filter on top of the camera.

Both backlight and IR lighting on. Bandpass filter on top of the camera.

My next step will be to build a prototype and experiment with different types of LEDs for a brighter backlight.

Filed under: Uncategorized No Comments
11Aug/092

Hello Web2.0!

Now, I've got my own domain and blog running. Setting up everything was easier than I thought...

I know this site surely needs some customization and content so stay tuned for some news about projects that I am working on, my studies and other things I'm spending time on.

Filed under: Uncategorized 2 Comments