Thursday, May 22, 2014

Stop Motion Movie

I have always loved these movies where you see the clouds rushing by. Where a scene is played fast forward through a day, seeing plants open up, are traffic increasing and decreasing over time.
The way that is done is usually taking a picture from the same position with some time interval and then putting them all in a movie. Or you can slowly change the position of your camera.

Anyway, I was working with the webcam last week for my object recognition project so I had the knowledge fresh in my head. I quickly made a program that finds your webcam(s) and then takes pictures with fixed time intervals (adjustable in whole minutes).
I tested it out on the view outside my window hoping for the bad weather that was said to be coming. It did not come. So here you can see a movie of pictures with a one minute interval of clouds (and sometimes a bird).

For those who are interested, I ran this program in Processing 2.2.1 (64 bit) and that worked fine, some earlier version were giving me problems.  My own program can be found here, but you'll have to change certain things to make it fit your own needs.


I attached an extra webcam to my laptop, so there is a choice of two. But besides that, the list Processing shows gives different sizes and frame rates for you webcam(s). The list starts with 0, but you can fill in any number from the list and you should get an image.
Find the line that says:

cam = new Capture(this, cameras[0]);

Maybe there's a different number then 0, but that's the place to change it. You should find it around line 26 in the code. I picked a very low resolution for testing, so the video looks bad in my opinion. For future video's I think the only limitation is the size of my hard drive.

Other tweeking

The time step, called 'step' in the code was set to 1, but you can change it to any whole positive number between 1 and 59 you like.
It could be necessary to change the screen size, because that is set to 'size(640,480)' at the moment.
Files are save as 'hour+minute.jpg' in a directory called '/images'. You could change that to your own preferences.

Making the movie

In the original pictures I had the camera wasn't positioned horizontally and I had to rotate them. I did this in Matlab because the rotation was the same for every image and using a script was a lot faster then using Photoshop or some other image program.
When my images where rotated correctly and square again I loaded everything in Windows Movie Maker, which is downloadable for free. Ones they were all loaded it selected them all and under 'Edit' you can set the 'Duration' of every frame. I set it to 0.25 seconds which gave me a smooth running video.

I saved the movie and uploaded it to YouTube. What I think is pretty neat about YouTube is that editing is really simple. There is music to pick from and it even suggests things like video stabilization.

So I hope this was useful for somebody. Nothing is stopping you from placing your webcam somewhere and making a cool movie.

After a request I have modified the program so you take pictures any multiple of seconds.
Click here to download.

Tuesday, May 20, 2014

Do It Yourself Tracking Software


In a preview I showed a radio controlled boat where I hacked the remote control. With an Arduino Uno you can send a signal to the remote which then sends it to the boat. A small video of my daughter trying it out to show it works.

My ultimate goal is to make the boat autonomous. I want to use a webcam to determine the position of the boat and then be able to steer in the right direction. So that could be making sure it doesn't crash into the wall or that it follows a certain route, I haven't made up my mind about that yet.

Choosing the system

When I started with this project I was thinking about all the obstacles and steps I would find along the way. But I soon realized that I was making the rules here. I could choose what system I was using and how difficult it was going to be. That sounds a bit vague so I guess I'll give an example. When I saw the remote control I immediately was thinking about the Arduino. Nowadays there are quite a few ways for computer-machine interaction but since I have some experience with the Arduino I knew I would have less problems implementing it.

One of the bigger challenges was using a webcam and it's images to send the right commands to the Arduino and so the boat. I followed part of a course last year on Coursera called: Creative Programming for Digital Media & Mobile Apps. All the apps they worked with were built in Processing which has a very low entry level and let's you make Android apps and JavaScript apps. It's really meant for fun user interfaces.
Bonus: It's really similar to the Arduino language and even the programs look similar. So if you know one, the other will be a cinch.
And yes you can access the webcam with processing and use the images to do whatever you want.

Object tracking

That was a long intro, bear with me.
Processing doesn't have object recognition functions built in and I figured any program you could find on the internet would not be suitable for what I wanted. So I decided to make it myself which is always more fun.
Accessing the webcam wasn't that hard (there are examples in Processing) but I needed some boundaries.
What did I want to recognize? The boat, but I didn't feel like building a Machine Learning algorithm that knows what the boat looks like and finds. Possible I guess, but pretty hard work. I wanted something simple.
I made the choice of placing a colored sticker (or maybe two) on the boat, let's say red. I intend to place the webcam above the water and I can choose what color bottom of the water will have. I could make sure that the only red thing in the webcam image is the red sticker. This is what I talked about earlier, I can make the choices of how difficult the problem is going to be. I have reduced the problem to finding a red dot in an image.

Finding the red dot

To give you a break from all this reading, here is a demo of my Processing program using the webcam on my laptop to follow a small red object.

In my hand I'm holding a small piece of red plastic. The program takes a webcam image and analyses it in two steps.
First it performs a threshold. Using the fact that red objects are red :) it assigns a '1' to pixels that have lot's of red and little green and blue (i.e. white pixels have lot's of red, green and blue) and it assigns a '0' to all the other pixels.
Next it calculates how many pixels in the neighborhood are found red and finds the pixel with the most neighboring red pixels. That neighborhood can be quite big, for instance this was done with 10 by 10 pixels.
The way these calculations are done can and will be topic of a separate blog entry.

To show the result it draws a white circle at the found position.
You can vaguely see image it works with around the white circle. That represents the number of pixels around every pixel. If you would have a stepfunction in your image the result of counting the neighboring pixels would look like something below.

Sketch of how the counting of neighboring pixels would look like
It reminds me of a Gaussian blur. Most important is that you see that the middle of the original platform results in a high peak in the second image. Important is choosing the right neighborhood size as you can imagine.

In the near future

Keeping the main goal in sight, tracking and controlling the boat there is still some work to be done.
I intend to place two stickers one the boat, a blue one and a green one and track them both. With the position of these two stickers you can determine the position and orientation of the boat. If you use previous positions and orientations you can calculate the speed and direction of the boat.

Then I have to think of some rules the boat has to follow like: don't touch the walls, or go around in circles.

So far the program is perfectly capable of doing all the calculations in real time, and I hope that it still does so when all these features are added.

To be continued...

The boat

Thursday, May 1, 2014

Friend speed

How fast can I get Facebook Friends?

41,23 friends per hour!

Awesome! You must wonder what that means and where I get this from.

A while ago I registered for a Facebook profile because:
1) All the cool kids are doing it
2) I want to watch holiday and baby pictures from friends
3) I wanted to do an experiment

Everyone I spoke about opening a FB account told me you get friends really fast. So I set out to see if this was true.

The experiment

When you don't change anything on the original settings you get an e-mail message about everything that is happening. That's why people switch this off immediately. You're on FB 24/7, you don't need see the messages twice, right?
I left the settings as were and registered every friend request and all the request I sent myself. The graph below shows you how that looks like. Yes, this was tedious work.

Most of it is quite clear I guess. On the horizontal axes we see the time in hours, and on the vertical axes the total number of friends, and the number of requests I sent myself. The numbers in the top are just the days, so we start on day one somewhere in the morning.
Funny thing is, within one minute of registering I received two friend requests without any other action then signing up.That means that those people somehow saw that I had joined, not sure how that works.
I sent maybe two or three requests, but I waited to see what would happen. The number of friends is rising, what happened?

Well in this case one of my first friends made friend suggestions and since I was in need of friends I clicked them all I guess. During day two I didn't do anything and I noticed that the rate at which my number of friends was growing was slowing down.

Sending requests

Therefore I sent out a lot of requests on day three, which you can clearly see from the quick rise of the blue line. And yes! I got an immediate response and my total number of friends sky rocketed. Let's zoom in on that event.

Total number of friends after sending lots of requests.

In this picture we can see that the total number of friends start to grow quite fast until.... people go to sleep. You can see  it stops at about hour 72 which is the end of day 3. A bit more than eight hours later it picks up again.

Friend speed

So I mentioned speed, or 'friend speed' as I like to call it. How do we calculate that? There are a couple of ways to calculate that. You could for instance look in every hour how many friends were added. Downside is, that in most hours no friends were added at all and in some others maybe four of five. This would be a very non informative graph.
A second method is calculating the time between two confirmations. Plus, you get a value for all the data points. Minus, some friends sent their confirmation at the same time (the same minute). That means the time was zero between them and so the speed was infinite at that moments. Infinite works very bad in graphs.

To solve that problem I let R (used in the graph making) draw something of an average through the data points, which is called an LOESS fit as can be seen in blue in the image below.

Blue line depicts a LOESS fit on the friend datapoints.

I think you lose 90% of your crowd with every graph you use, so I'm down to 0.1% of my readers.
On this line I performed this calculation I just mentioned of calculating the time difference between two events which should be an approximation of the speed. In the image below you can see the calculated 'speed' compared with the total friends data.

As my number of friends is rising very promising we see a terrifying event. The speed of gaining friends is dropping. We see what we expect, at nightfall the friend speed drops to almost zero, and during day 4 the speed is very low. But there is something wrong in my opinion about the speed data. Of course we used this LOESS fit as an approximation of our data which will give rise to errors. But, in this graph it looks like during day 3 the friend speed is decreasing at a constant rate. And for those who love differentiating and integrating and all, that would mean that the total number of friend should be parabolic. I therefore don't trust this graph all too much and tried it in a third way.


Since some regions in the graph seem almost linear it's easy to draw a tangent line there and use that to estimate the speed.

Tangent lines to estimate the friend speed.

In day 3 it looked there were two areas. One in the beginning consisting of people who responded as soon they got the friend requests on their mobile device and some hours later people checking in later.
In the first section the climbed was huge and I estimated it to about 41,23 f/h !! (friends per hour of course). During the rest of the day it settled to a more moderate 3,66 f/h and the next day it dropped to a sluggish 0,70 f/h.


So what the message to take home? When you ask to become friends of FB, they respond pretty fast. The number of friends has to reach a maximum because you only know that many people and most friends you gain comes from the people you invite yourself. So automatically to speed slow down. Good thing too, if the speed stayed of about 40 friends per hour I would so may friends by now I wouldn't be able to keep up with the news feed.

Lesson two: you can't really say anything on such a small dataset and especially on something that is not reproducible. So there is no real science here, I just wanted to make pretty graphs.

(note 2 self: repeat experiment until friends no longer respond)