我正在尝试读取 TIFF 图像以执行处理。理想的情况是能够将此图像导入 OpenCV 结构,但即使以不同的方式访问它也很棒。
如果我对我得到的图像运行 tiffinfo
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
我想访问单个像素值。图片为灰度图,包含的数据范围为0.0到10372.471680。
我对 LibTIFF、Magick++ 进行了一些试验,但无法访问单个像素值(我尝试对像素进行循环并在屏幕上打印这些值)。
这是我尝试使用的一段代码,我是从一个在线示例中获得的:
#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);
}
}
我用它编译
gcc test.c -ltiff -o test
当我运行它时,我得到
test.c: In function ‘main’:
test.c:24: warning: dereferencing ‘void *’ pointer
test.c:24: error: invalid use of void expression
有什么提示吗?感谢您的宝贵时间。
最佳答案
查看函数 _TIFFmalloc() 的文档。如果它像标准 malloc 一样工作,它会返回一个 void 指针,如果第 24 行中的 buf[col] 语句希望正确工作,则需要将其强制转换为特定类型。
关于c++ - 访问 TIFF 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6532042/