Showing posts with label Processing. Show all posts
Showing posts with label Processing. Show all posts

Friday, January 15, 2016

Infrarood Camera met een Arduino

In de Maakbare Wereld van 16 januari staat mijn stukje over de infrarood camera die ik heb gemaakt.
De camera bestaat grofweg uit drie onderdelen: Een Arduino Uno, een Pan Tilt, en een infrarood sensor. Om een beeld te krijgen hoe het werkt een kort kort filmpje van de sensor die heen en weer beweegt.


Later heb ik een klein kartonnen buisje om de sensor geplaatst, zodat er geen licht van opzij valt.

Draadjes

De motoren van de pan tilt zijn redelijk eenvoudig aan te sluiten. Ze gebruiken allebei 5 Volt.  Het is nodig om een flinke condensator tussen de plus en min aansluitingen te plaatsen die spanningspieken op kan vangen bij het aanzetten van de motoren. Als je dit niet doet daalt de spanning zoveel dat de Arduino steeds opnieuw opstart.
De infrarood sensor heeft vier aansluitingen; er is een uitstekend stukje als oriëntatie punt. Aangezien er een duidelijk beschrijving hier staat ga ik dit niet herhalen. Er worden twee 10 kOhm weerstanden gebruikt die op de plus kant aangesloten worden. Ik vond dat niet heel duidelijk uit de foto. Let op dat deze sensoren in verschillende types bestaat. Ik heb de 3V versie gebruikt en daarom moest ik de 3.3 Volt aansluiting van de Arduino gebruiken, dus niet dezelfde als voor de motoren.

Het complete schema ziet er ongeveer zo uit. (hopelijk zonder fouten :) )


Voor de geïnteresseerde; deze afbeelding is gemaakt met Fritzing, een gratis programma om schakeling e.d. te tekenen.
Resultaten zijn natuurlijk ook leuk om te zien. Ik heb foto gemaakt van een glas bierglas gevuld met warm water.

Glas warm water scannen
Foto van het warme glas water
De kleuren zijn wat willekeurig gekozen, blauw is warm en rood is koud. Maar het idee is duidelijk lijkt me. Ik plaatste het glas toen de sensor al heen en weer bewoog, waardoor er strepen kwamen. Verder was ik erg tevreden of dit resultaat. De sensor heeft normaal een blikveld van ongeveer 90 graden, maar met het kokertje is het een aardig scherp beeld geworden. Het glas stond natuurlijk dichtbij. Misschien zou een lens kunnen helpen, maar de vraag is of een gewone lens wel goed werkt met infrarood licht.

Code

Motoren en sensoren gaan niet uit zichzelf werken en je computer maakt ook niet vanzelf afbeeldingen, dus kort daar wat over. De motoren kun je vertellen om naar een bepaalde stand te gaan van 0 tot 180 graden heen en weer en 0 tot 150 graden op en neer. Ik heb niet dat volledige gebied afgescand, maar van 0 tot 90 graden in beide richtingen. We gaan steeds één plekje opzij tot we de rand bereikt hebben, dan één plaatsje naar beneden en de andere kant weer op. 
Na elke beweging vragen we aan de infrarood sensor wat de gemeten temperatuur is. Deze sensor geeft omgevingstemperatuur en de voorwerpstemperatuur, Daarbinnen gebeurd dus ook al genoeg blijkbaar. 
De Arduino Uno is het hart van de operatie. Deze doet het aansturen van de motoren en het opvragen van de temperatuur. Als dat gebeurd is stuurt het een bericht naar de laptop via de USB kabel, een serial message. Op de laptop draait een Processing programma dat wacht op berichten, graden Celsius in dit geval. De graden worden omgezet in gehele waarden tussen de 0 en 255, waarbij 0 overkomt met bijv. 24 graden en 255 met 30 graden. Met deze waarde wordt een vierkantje ingekleurd, zo wordt 0 helemaal rood en 255 volledig blauw en alles er tussen in mooie mengkleuren.

Er zat wat verschil tussen de heen rijen en de terug rijen waardoor ze wat verschoven waren ten opzichte van elkaar en dus heb ik ze wat opgeschoven.
Beide programma's zijn hier te vinden. Je kunt ze ook openen in elk tekstverwekingsprogramma, zoals notepad.

Als iemand nog vragen heeft (zoals over code die niet werkt :) ) plaats ze hieronder.








Friday, January 30, 2015

Vogels spotten - bij de Maakbare Wereld in de Volkskrant van 7 februari 2015


Om te zien hoeveel vogels er nu zo langskomen op ons balkon heb ik een webcam geplaatst die alleen foto's neemt als er beweging is. Ik heb hiervoor Processing 2.2.1 gebruikt, omdat deze versie goed werkt met de webcam. Mijn code kunt u vinden onder Code Examples.

De foto's van de vogel bezoekjes zijn samengevoegd in een gif bestanden, zodat het filmpjes worden. Hieronder zijn wat voorbeelden.

Het kan misschien wat tijd kosten voordat ze geladen zijn.








Dingen om op te letten. 

Ik heb in mijn code de bestandsnamen laten afhangen van de tijd. En daarnaast was het niet mogelijk een bestand op te slaan als deze naam al bestond. Anders heb je de kans dat er 30 keer per seconde een nieuw bestand wordt opgeslagen wat het programma heel traag maakt.

Ik heb de bestanden direct naar mijn google drive laten opslaan, zodat ik ook op afstand kon zien of er nieuwe foto's waren. Maar met andere cloud oplossingen zoals Dropbox, Onedrive, iCloud werkt dat vast ook. 

De waardes waarbij het programma beslist of een pixel aanzienlijk verandert is en wanneer er genoeg pixels verandert zijn heb ik proefondervindelijk bepaald. Als je dus zelf gaat kijken of het programma bij jou werkt is dat een goeie plek om iets te veranderen.

Ik heb gekozen voor een venster formaat van 640 bij 480 pixels en een bijpassende webcam. Het programma geeft een lijst met alle mogelijke webcam formaten. Kijk dus eerst een keer welk nummer er voor de webcam staat die jij wilt gebruiken en verander dat nummer op de plek in de code.
Als het programma heel traag is, kun je ervoor proberen om een kleiner formaat webcam te kiezen.
Of als je dieper in de code wilt duiken, proberen om gebieden over te slaan als de huidige afbeelding met de vorige wordt vergeleken, door de for loop te veranderen. Een tip daarbij is dat afbeeldingen in Processing geen matrices zijn maar gewoon een vector van getallen. Een enkele for loop is eigenlijk al voldoende.



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.

Webcams

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.

EDIT:
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

Introduction

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