Sunday, September 20, 2015

How to write a multipage TIFF file

Today, I just realized that it is a pain to write multipage TIFF images with OpenCV. I assume that it is not officially supported?

I wrote this simple sample code for all medical students with almost no programming experience! This code will allow you to dump your image sequence into a multipage tiff (or tiff stack?) that is readable by any machine or tools such as Matlab.

Here is a link to the code:

And you can preview it here. But I don't recommend copying directly here just in case the symbols or spacing got messed up.

#include "tiffio.h"
#include "stdio.h"
#include "stdlib.h"
#include <string.h> /* memset */
#include <unistd.h> /* close */

int main(int argc, char* argv[])
    TIFF *out = TIFFOpen("new.tiff","w") ;
    if (out)
        const int NPAGES = 10;
        int page;
        for (page = 0; page < NPAGES; page++){
            uint32 imagelength = 100;
            uint32 imagewidth=200;
            uint8 * buf;
            uint32 row, col, n;
            uint16 config, nsamples = 3;
            config = PLANARCONFIG_CONTIG ;

            TIFFSetField(out, TIFFTAG_IMAGELENGTH, imagelength);
            TIFFSetField(out, TIFFTAG_IMAGEWIDTH, imagewidth);
            TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nsamples);
            TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8) ;
            TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, imagewidth*nsamples));

            /* We are writing single page of the multipage file */
            TIFFSetField(out, TIFFTAG_PAGENUMBER, page, NPAGES);
            printf("writing %d x %d, nsamples %d", imagewidth, imagelength, nsamples);

            buf = new uint8 [imagewidth*nsamples] ;

            for (row = 0; row < imagelength; row++){

                for(col=0; col < imagewidth; col++){
                    for(n = 0 ; n < nsamples ; ++n)
                        //writing data -- replace with your own data
                        buf[col*nsamples+n] = page*row % 255 ;
                if (TIFFWriteScanline(out, buf, row) != 1 )
                    printf("Unable to write a row\n") ;
                    break ;
    return 0;

To compile it in Mac (assuming you have Macport), just run this in command line...

g++ main.cpp -I /opt/local/include -L/opt/local/lib -ltiff

... Enjoy! 

Wednesday, August 26, 2015

Book is out :)

You can buy a copy at:

The book comes with all source code that you would need to build applications using OpenGL in Windows, Linux, Mac OS X or Android! I've also put in some effort to link OpenCV and the Android Sensor Manager in Android, and so others who are interested in building interactive application have a simple-to-use code to get started.

With a little bit of hack, you can connect the code base to any depth sensor and OpenCV! That allows you to build lots of applications easily.

Although the book is out, there are still lots I would like to cover, e.g., OpenGL lighting techniques, Compute Shader, and OpenCL/CUDA! Those are super useful for data visualization! and will be covered if there is a great sale to this book this time.

Wednesday, March 25, 2015

Meta Update & New Book

Augmented Reality eyeglasses is definitely a hot topic these days. At Meta, there are lots of press, and I will start posting ones that I had interview with next.

Also, now my new book is ready for pre-order. You can find the latest work here, especially on the OpenGL + OpenCV side. This book is aimed for beginners who want to know how to get started. I can always answer more difficult problem here if anyone asks.

More to come! Stay tuned...