Image transparency overlay with GNU Octave using YCbCr

So, let’s say someone sends you a Matlab file displaying interesting image regions using the AlphaData figure property. You, a free software hippie, are using Debian 7.5 and Octave 3.6.2.

Trying to run the file with Octave, it spits out this message:

error: get: unknown image property AlphaData

You spend the next 15 mins Googling and find that there are some references to alphadata in Octave’s source code and documentation. However, after examining the figure struct, you conclude that your version is probably too old and that you have to be smart.

A nice workaround for this problem is to convert the two images from RGB to the YCbCR color space. And, if you’re familiar with the YCbCr color space, you probably understand what you need to do next and can stop reading at this point.

For the readers that don’t know the YCbCr color space, by doing the above we separate the color components from the luma component, i.e., the black-and-white (gray scale) version of the image. And so, by combining the color components from one image and the luma component from the other image, you get a nice transparent overlay effect.

Convert the YCbCr image back to RGB and et voilà, right? Nope, there’s yet another problem. Octave 3.6.2 only has rgb2ycbcr() and not ycbcr2rgb(). Quick fix, grab some new files (ycbcr2rgb.m, rgb2ycbcr.m, ycbcrfunc.m) from Octave’s repository, prefix them to not shadow system functions, and included them in your project. Slow fix, read the YCbCr Wikipedia article and write your own YCbCr to RGB function.

You can find example source code for the quick fix at Bitbucket.