XBMC and Philips Hue ambilight, first steps

This post takes the first steps into creating an add-on for XBMC that controls your Philips Hue lights.

nemo

The add-on will consist of three parts:
1. Snapping pictures of the video
2. Calculating the most dominant/average color
3. Controlling the lights

I started with calculating the average color of an image. As a sample image I used the trailer for Finding Nemo, and snapped a picture every 10 seconds.

My first, naive, approach was to resize the image and iterate over every pixel in the image. For every pixel I updated a global “red”, “green” and “blue” counter, and devided the end result with the number of pixels. This works quite well, but the average colors are a bit dull. In a similar way I calculated average HSB values. There appears to be a small bug in my code (blue pictures get an average HSB of green-ish). The first results:

run_1

In a second attempt I still iterate over every pixel, but calculate how the pictures are aligned over the Hue spectrum. See the images below, where the right pictures shows the Hue spectrum from 0 to 360 degrees and how many pixels are in the image per Hue value.

sample_picspectrum

For the resulting value I divide the Hue in 10 degree sections, and use this as the most occuring Hue (+5). So in the example the most dominant section/”bin” would be 20-30, and the resulting Hue will be 20 + 5 = 25.

The results:

run_2

Next up: building a  XBMC package

Update 2013-03: The XBMC add-on is available, see https://meethue.wordpress.com/2013/02/22/xbmc-add-on-improved-ambilight/

Advertisements