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

No comments:

Post a Comment

Note: only a member of this blog may post a comment.