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!