Wednesday 28 January 2015

Smooth Videos - AKA Correcting NASA

What makes a video look smooth? Your eye is extremely sensitive to problems with videos, and for any video to look smooth it has to have:

  • A high frame rate
  • A steady camera
  • Roughly even brightness each frame

Normally these are easy to get. Any modern camera will give a decent frame rate, and the exposure time for each shot will be accurate, giving an even brightness of images each frame. Camera steadiness is more difficult, but a basic tripod will solve that.

This is a lot harder in space! For a NASA space probe floating through deep space, keeping a steady orientation is a challenge. Spacecraft can do this well quite well, using thrusters and reaction wheels. They still make some small mistakes though. Getting an even exposure time for each frame of a video is also harder in deep space, especially as it might take minutes or hours for radio commands to reach the space probe so you have to trust its autoexposure. Luckily, given ok starting material, correcting camera shake and frame brightness problems by image processing is quite easy.

NASA's Dawn space probe is currently approaching Ceres, getting sharper pictures of this dwarf planet than ever before. A series of these pictures even shows this tiny world rotating. Unfortunately, they didn't correct the shake or brightness problems in the video released to the press:


A quick fix in ImageJ to remove the shake and even out the frame brightness makes a (dwarf) world of difference:


As the probe gets closer and closer to Ceres its shots are getting more and more spectacular, but the videos still need shake and brightness correction.


Interested in improving some NASA videos? I did the corrections using the free scientific image editing software ImageJ, and these are two handy macro scripts for video corrections in ImageJ:

Image stabilisation
//Stabilise based on signal intensity centroid (centre of gravity)
//Stabilises using translation only, using frame 1 as the reference location
//This method is suitable for stabilising videos of bright objects on a dark background
for (z=0; z<nSlices(); z++) {
 //For each slice
 setSlice(z+1);
 //Do a weighted sum of signal for centroid determination
 sxv=0;
 syv=0;
 s=0;
 for (x=0; x<getWidth(); x++) {
  for (y=0; y<getHeight(); y++) {
   v=getPixel(x, y);
   sxv+=v*x;
   syv+=v*y;
   s+=v;
  }
 }
 //Calculate the centroid location
 cx=sxv/s;
 cy=syv/s;
 if (z==0) {
  //If the first slice, record as the reference location
  rcx=cx;
  rcy=cy;
  print(rcx, rcy);
 } else {
  //Otherwise calculate the image shift and correct
  dx=cx-rcx;
  dy=cy-rcy;
  print(dx, dy);
  makeRectangle(0, 0, getWidth(), getHeight());
  run("Copy");
  makeRectangle(-dx, -dy, getWidth(), getHeight());
  run("Paste");
 }
}
Brightness normalisation
//Normalise image brightness to reduce video flicker
//Scales intensity based on the mean and standard deviation, using frame 1 as the reference frame
//This method is suitable for reducing flicker in most videos
for (z=0; z<nSlices(); z++) {
 //For each slice
 setSlice(z+1);
 //Find the signal mean and standard deviation
 run("Select All");
 getRawStatistics(area, mean, min, max, stdev);
 if (z==0) {
  //If the first slice, record as the reference signal mean and stdev
  rmean=mean;
  rstdev=stdev;
  print(rmean, rstdev);
 } else {
  //Otherwise calculate the brightness and scaling correction
  run("Macro...", "code=v="+rmean+"+"+rstdev+"*(v-"+mean+")/"+stdev);
  print(mean, stdev);
 }
}

Software used:
ImageJ: Image corrections
GIMP: Animated gif file size optimisation

Thursday 22 January 2015

Tengwar - Transliterating Font

This blog post is about a Tengwar font I designed. It automatically converts text as you type into accurate Elvish script. You can download it for free here.  Just make sure you enable ligatures, contextual alternates and kerning for best results!






While writing his Middle Earth books, JRR Tolkein invented an entire alphabet for the elves called Tengwar. His attention to detail was incredible, Tengwar is a fully functioning writing system. This is the famous Elvish writing seen all through Lord of The Rings and the Hobbit.

Tengwar is an alphabet, not a language, and can be used to write many languages. This is like, for example, Latin and Greek alphabets; the word English word “ring” is normally written in the Latin alphabet but could also be written in the Greek alphabet as “ρινγ”. The two sound the same, it is just a different way of writing the sounds of the word “ring”. The process of transferring a word between two different alphabets is called transliteration.

In Middle Earth, Tengwar is one of the major ways of writing. Many languages were written in Tengwar: two Elvish languages called Sindarin and Quenya, the Black Speech of Mordor (on the One Ring), and the language of men (English). Tolkein gave detailed notes on how to write English in the Tengwar alphabet. In Tengwar “ring” is written:


Writing in Tengwar follows simple rules but quickly gets complicated, so I designed a font that does it automatically! You can download it for free here.  As far as I know this font is unique, all other Tengwar fonts are just collections of symbols you have to manually mix and match.

To use this font you just need to download and install it. Once it is installed, just select it as the font and start typing as normal. The font will automatically transliterate the text you type into accurate Tengwar, based on Tolkein’s rules about writing English in Tengwar.

To make sure the font is working accurately you need to make sure three settings are enabled: kerning, contextual alternates and ligatures. For example, in Microsoft Word you can do this through the advanced font settings:


So how does it work? Basic Tengwar is similar to the Latin alphabet, with two classes of symbols representing the sounds of different consonants and different vowels. At the simplest level, to write the word “ring” the font just selects the four symbols for “r”, “i”, “n” and “g”:


Unlike the Latin alphabet, there are special rules for how vowels are written. Instead of always being a separate letter, if a vowel comes immediately before a consonant it is written as an accent on that consonant. In “ring” the “i” comes immediately before the “n” so the font writes it as an accent on the “n”:


There are some special rules to use for some consonants, depending on where they are in a word. “r” is one of these letters. If it is followed by a vowel then it should have a different symbol, which the font automatically selects:


Finally, some common combinations of consonants that have a single sound (like “th” as in “the”, “ch” as in “church” and “gh” as in “ghost”) have their own single symbol. “ng” is one of these pairs and, again, the font automatically makes this substitution:


And that is how the font automatically writes “ring” in Tengwar. These are not the only rules though, there are also other ones built into the font that involve double vowels, double consonants, the letter “n” preceding another consonant, whether a “y” is used as a vowel or a consonant, whether an “e” is voiced in a word or is silent at the end of a word, etc.

The key feature of my font is that it takes all of these rules into account automatically and lets you simply type away as normal and get an accurate, readable result in Tengwar. You can also just select an existing chunk of text and apply the font to it to transliterate it to Tengwar, but make sure the text is all lower case for best effect. It does make a few very small mistakes, but Tolkein would understand it!

Tengwar is a beautiful and concise alphabet. The way vowels, double letters and letter pairs combine make many words very short and elegant:


The overall flow of a paragraph is also excellent, with the letters falling into self-symmetric curves and alignments.

(This is the first paragraph of Lord of The Rings, converted to Tengwar by just changing the font to my Tengwar Transliteral font.)

If you are interested in playing with Tengwar text for any kind of design please consider downloading the italic and script versions of the font here. These cost a few pounds/dollars/euros.

If you are interested in reading Tengwar, or manually translating it, then the excellent “Tengwar Textbook” Chris McKay is available online for free: Tengwar Textbook.

There are also excellent simple guides on writing in Tengwar (like this one), but why do that when you could just download my font and type your name?

Software used:
Inkscape: Glyph design
Fontforge: Font design

Wednesday 7 January 2015

Trypanosome Lego

Trypanosomes and Leishmania are the two tropical parasites that I do most of my research on. These cells seem to have a lot of modularity in controlling their shape, and have quite a lot of flexibility in reshuffling where particular structures (made up of many organelles) sit within the cell.

The base of the flagellum, the whip-like tail which the cell uses to swim, is also the site where the cell takes up material from its environment (essentially its mouth) and is linked with the Golgi apparatus (an important organelle in protein processing) and the mitochondrion (the powerhouse of the cell) and links to the mitochondrial DNA. It turns out reducing the level of just one protein in the cell can cause this entire complex structure to shift its position.

Cells are not quite as flexible as Lego, but it is still impressive that a single protein can have such a large effect on the organisation of a cell.