Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For GeoTIFF images it might be better to use directly libtiff. It's very robust and can handle any TIFF image. Most general purpose libraries or software are having difficulties when loading multispectral or large images.

If you built OpenCV for yourself, you might have libtiff already, otherwise download it here.

Here's how to use it:

#include "tiffio.h"
....

TIFF *tif=TIFFOpen(filename, "r");
uint32 W,H;
uint16 Bands,bps,sf;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &W);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &H);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Bands);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sf);

The size of your matrix is WHspp(bands). The type is determined using sf and bpp: sf=1:unsigned; sf=2:signed;sf=3:floating point (e.g. if sf=1 and bpp=32 then type=CV_32U; for sf=3 and bpp=64 you have type=CV_64F)

long sls=TIFFScanlineSize(tif);
void *raster;
raster=_TIFFmalloc(sls);
int sizes[3]={H,W,Bands};
Mat geotiff(3,sizes,type);
char *scanline;
for(int i=0;i<H;i++){
    TIFFReadScanline(tif,raster,i,0);
    scanline=geotiff.ptr(i);
    memcpy(raster,scanline,sls);
}
_TIFFfree(raster);
TIFFClose(tif);

This should probably work, however the code is untested (I adapted an old code I had). Note that this works for any number of bands; for panchromatic, RGB or RGBA images the code is much simpler (use TIFFReadRGBAImageOriented or similar functions). For more information, read the documentation.

Finally, don't forget to add the libtiff.lib to the QT project libs.

Hope this helps!

For GeoTIFF images it might be better to use directly libtiff. It's very robust and can handle any TIFF image. Most general purpose libraries or software are having difficulties when loading multispectral or large images.

If you built OpenCV for yourself, you might have libtiff already, otherwise download it here.

Here's how to use it:

#include "tiffio.h"
....

TIFF *tif=TIFFOpen(filename, "r");
uint32 W,H;
uint16 Bands,bps,sf;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &W);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &H);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Bands);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sf);

The size of your matrix is WHspp(bands). W * H * Bands. The type is determined using sf and bpp: sf=1:unsigned; sf=2:signed;sf=3:floating point (e.g. if sf=1 and bpp=32 then type=CV_32U; for sf=3 and bpp=64 you have type=CV_64F)

long sls=TIFFScanlineSize(tif);
void *raster;
raster=_TIFFmalloc(sls);
int sizes[3]={H,W,Bands};
Mat geotiff(3,sizes,type);
char *scanline;
for(int i=0;i<H;i++){
    TIFFReadScanline(tif,raster,i,0);
    scanline=geotiff.ptr(i);
    memcpy(raster,scanline,sls);
}
_TIFFfree(raster);
TIFFClose(tif);

This should probably work, however the code is untested (I adapted an old code I had). Note that this works for any number of bands; for panchromatic, RGB or RGBA images the code is much simpler (use TIFFReadRGBAImageOriented or similar functions). For more information, read the documentation.

Finally, don't forget to add the libtiff.lib to the QT project libs.

Hope this helps!