Access TIFF image
I am trying to read a TIFF image to perform processing. The ideal would be to be able to import this image in a OpenCV structure, but even having it accessible in a different way would be great.
If I run tiffinfo on the image I get
TIFF Directory at offset 0x2bb00 (178944)
Subfile Type: (0 = 0x0)
Image Width: 208 Image Length: 213
Resolution: 1, 1
Bits/Sample: 32
Sample Format: IEEE floating point
Compression Scheme: None
Photometric Interpretation: min-is-black
Orientation: row 0 top, col 0 lhs
Samples/Pixel: 1
Rows/Strip: 1
Planar Configuration: single image plane
I want to access single pixel values. The image is gray sc开发者_如何学Cale, data contained there ranges from 0.0 to 10372.471680.
I make some trials with LibTIFF, Magick++ but was not able to access single pixel values (I tried to make a loop over the pixels and to print these values on screen).
Here is a piece of code I am trying to use, I got it from an online example:
#include "tiffio.h"
#include "stdio.h"
int main()
{
TIFF* tif = TIFFOpen("test.tif", "r");
if (tif) {
uint32 imagelength;
tsize_t scanline;
tdata_t buf;
uint32 row;
uint32 col;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
scanline = TIFFScanlineSize(tif);
buf = _TIFFmalloc(scanline);
for (row = 0; row < imagelength; row++)
{
int n = TIFFReadScanline(tif, buf, row, 0);
if(n==-1){
printf("Error");
return 0;
}
for (col = 0; col < scanline; col++)
printf("%f\n", buf[col]);
printf("\n");
}
printf("ScanLineSize: %d\n",scanline);
_TIFFfree(buf);
TIFFClose(tif);
}
}
I compile it with
gcc test.c -ltiff -o test
when I run it, I get
test.c: In function ‘main’:
test.c:24: warning: dereferencing ‘void *’ pointer
test.c:24: error: invalid use of void expression
Any hints? Thanks for your time.
Look at the documentation for the function _TIFFmalloc(). If it works like the standard malloc, it returns a void pointer, which needs to be casted to a specific type if the buf[col] statement in line 24 is expected to work correctly.
You have to fix this with:
tdata_t *buf;
buf =(tdata_t*) _TIFFmalloc(scanline);
精彩评论