Tuesday, 29 December 2015

making gifs from video files in Matlab

This is an update to a previous post. The previous post is below, edited to suit the new code which runs in matlab. there is a link here to the new code which was co-written with David Solletti.


How to convert and resize an AVI to gif in matlab

If you are giving a presentation, and you don't have awesome videos in it, you are either a 60 year old well renown expert in your field, or you are just plain mean. Videos are the highlight of any talk, the trouble is, they are often large, slow and have unpredictable issues when it come to finding the correct path file, since they always seem to be looking for some inaccessible folder, on a different computer, often hundreds of miles away. Then there you are, onstage, the video doesn't work, suddenly you have an angry crowd of caffeine fueled scientists wanting your open your skull to feast on the gooey stuff inside....

"Oh no, Dr Christofer" you say, "how do i overcome these problems?"

The answer is you use gifs! Gifs are without a doubt my new favourite format for showing moving images. So much better than videos, and only second to interpretive dance, which infact i did see one jerboa expert perform infront of a crowd of scientists. But if, like me, your interpretive dance skills are somewhat lacking you need the power of gif
How can you harness this power? Well, they can be made quite simply using the free ware program GIMP, but i had a few issues with this, particularly when it came to resizing my images. Plus it also meant getting a second program to extract individual frames from the movie. A better way would be to do both steps in matlab. 
I looked on file exchange, and found some code called avi2gif.m but this seemed to use the 'aviread' function which didnt work in my version of matlab 2012a. Plus it also didnt let you resized your image or make it faster (you can only increase the delay between frames). Both are important if you want to get the file size down really low (for example imgur.com only takes gifs < 2mb)

the new code can be found here

one important feature of the code, is the ability to reduce the frame number. Below is an example of this process. 

anyway, that is my cheap and nasty code which worked for me. An important point to remember is the line "k = 1:2:nFrames etc" cuts out every other frame. I found if i did not do this, then the gif would run really slow. This might be an effect of using high speed film to make the videos. For example, this gif below was made using frameskip = 1

if it was any slower, we would have to use some sort of geological clock to time it by. Seriously it seems like that slow kid at school, who sat around eating glue all day.

so i used frameskip = 2

bit quicker but still its gonna run for a painfully long time when you are standing infront of a crowd trying to imagine yourself in everyones underwear

frameskip = 4

works much better - seems smoother

Well let me know if you have any improvements comments or suggestions, and i look forward to seeing many gifs in the upcoming conference in portand!

Sunday, 29 November 2015

Echidna Biomechanics

Ever wonder what an echidna does with all its time? 

The echidna (Tachyglossus aculeatus) is a spiny ball of monotreme that looks something like this. Those spines are sharp mind you, and often leave a weird irritating/itchy marks on your skin after they stab into you. Given that its close relative the Platypus (Ornithorhynchus anatinus) has poisonous spurs on its hind limbs makes me wonder whether those spines aren't filled with something nasty. My point is that pretty much nothing is going to try to eat an echidna. Some germans once wrote a paper on what happens when you do. They came across the body of one of Australia's top predators, and one of my favourite animals, the Perentie (Varanus giganteus) which had the brave, yet somehow transparently stupid idea, to attempt eat an echidna whole and had in doing so met its own demise.

The photo is from Kirschner et al. (1996),
 Darren Naish's blog . 
The perentie had come across the echidna (dead or alive?) and had tried to eat it but the spikes had pierced its throat, and it wasn't able to neither swallow, nor eject the echidna from its mouth. Its not clear what actually killed the lizard in the end (starvation maybe?) but its likely it had some time to reflect on this, and no doubt many other decisions it had made throughout its long life of being lizardy and awesome. I actually think this specimen is on display in the Queensland Museum.

So the echidna is pretty much invenerable to predation, and presumably also asteroid strikes, lasers, paper cuts and anything less than a direct thermonuclear attack. So the question then arises, if you have nothing to fear what do you spend you afternoons doing? Hanging out by the local tree hollow? finding only the finest and tastiest termites available? The question, I am sure, has sometimes kept you up at night. Infact the question is all the more important when you consider that these little critters, with an Australia wide distribution, spend much of their time digging up the ground after termites, and in doing so move quite a bit of dirt. How much? No idea, but enough that these guys can start to change the profile of the landscape, putting them in that neat category of animals called ecosystem engineers. So if they are moving a bunch of dirt around, we ought to know about it.

To answer this question I teamed up with two echidna researchers, Christine Cooper (Curtin), and my old PhD supervisor Phil Withers (UWA). They were looking at the thermoregulation in echidnas (which is interesting in itself, since they are half mammal half lizard, white hot balls of spikey terror) - which was a great opportunity to test out some sensors I had been working on. When I say I was working on them, I mean another academic Phil Terrill from the school of engineering at UQ was working on them. I had originally anticipated using these sensors on some large varanid lizards, but it turns out that lizards are a giant pain in the ass to work with since they seem to travel forever, in no apparent direction, and bury themselves under piles of trees, rocks and dirt which make retrieving the sensors a little harder. Echidnas would surely be easier.

So I set off back to WA, to a small patch of bush called Dryandra Woodland, which was known for two things. Spikey trees called Dryandra, and spikey monotremes called Echidnas. They certainly are easier to catch than lizards, and to my best knowledge nobody in the history of the universe has been bitten by one. That seemed all well and good, so we strapped the accelerometer sensors, some temperature sensors, a GPS and a radio tracker onto the back of these echidnas, and let them go again, safe in the knowledge that we could retrieve them at our hearts desire, gather the data, replace any batteries, and set them off again.

It would be a week long peek into the private life of an echidna. 

Turns out life is of course not that simple, our echidnas, likely pissed at our decorating them with sensors, antennas, and what not, retreated to the deepest and darkest caves humanly possible, only to emerge in the hours of the darkest nights, where they would attempt to evade capture by three sleepy
and exhausted scientists.

They were very nearly successful and many nights we were forced to climb into their lairs to change the batteries on their back, or change sensors over. But in the end we did get some data, and since this is a biomechanics blog we are going to focus on the biomechanics data. The first and most important thing i did before releasing the echidnas was to perform the sacred ritual among biomechanists, the 'Calibration dance'. This dance has many forms, each unique to the scientist that devises them. They have, in my observations as a biomechanist, two equally important and fundamental functions. Firstly, they must relate the position and movement reference frame to a recording cameras, this would also be important for synchronising accelerometer signals to the cameras later. And secondly, some might argue more importantly, they must make the person performing the calibration dance look as ridiculous as possible. And so it was with great fortune that I was able to convince my co-researcher Christine Cooper to perform this sacred dance with the echidnas.

Click to make Christine bigger

From this I could synchronise the camera to other activities, which we observed as the echidna performed, during its daring, yet slow and indecisive attempts, to escape from us once re-released. We got walking




And in rare cases climbing

We then divided the echidnas day in 30 second chunks, and used these signature accelerometer traces to assign to each short interval of time to a particular activity and in doing so work out exactly what the echidna was doing with its day.

And what does an echidna do with its day?? 

It turns out not much. Many of our echidnas spend as much as 80% of their day hiding in rock caves, thinking about whatever it is that echidnas think about, ants probably.

But from these short periods of activity we were able to get some interesting biomechanical data. Analysing the chunks of time when it was moving, and running a simple fourier analysis allowed us to determine the stride frequency of the echidna at different periods of the day. With some knowledge of the stride length of the echidna, we should be able to work out things like speed during different activities, and more. This will all help us figure out the private life of echidnas.